Class: RDF::Literal::Time

Inherits:
RDF::Literal show all
Defined in:
lib/rdf/model/literal/time.rb

Overview

A time literal.

The lexical representation for time is the left truncated lexical representation for ‘xsd:dateTime`: “hh:mm:ss.sss” with an optional following time zone indicator.

Constant Summary collapse

DATATYPE =

Since:

  • 0.2.1

RDF::XSD.time
GRAMMAR =

Since:

  • 0.2.1

%r(\A(\d{2}:\d{2}:\d{2}(?:\.\d+)?)((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z).freeze
FORMAT =

Since:

  • 0.2.1

'%H:%M:%S%:z'.freeze

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Attribute Summary

Attributes inherited from RDF::Literal

#datatype, #language

Instance Method Summary collapse

Methods inherited from RDF::Literal

#compatible?, #comperable_datatype?, datatype_map, datatyped_class, #eql?, #escape, #freeze, #has_datatype?, #has_language?, #hash, #inspect, #literal?, new, #object, #plain?, #simple?, #validate!, #value, #value_hash

Methods included from Term

#<=>, #compatible?, #eql?, #term?, #to_base, #to_term

Methods included from Value

#anonymous?, #canonicalize, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!, #variable?

Constructor Details

#initialize(value, options = {}) ⇒ Time

Returns a new instance of Time.

Parameters:

  • value (Time)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :lexical (String) — default: nil

Since:

  • 0.2.1



20
21
22
23
24
25
26
27
28
29
# File 'lib/rdf/model/literal/time.rb', line 20

def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
  @string   = options[:lexical] if options.has_key?(:lexical)
  @string   ||= value if value.is_a?(String)
  @object   = case
    when value.is_a?(::DateTime)         then value
    when value.respond_to?(:to_datetime) then value.to_datetime rescue ::DateTime.parse(value.to_s)
    else ::DateTime.parse(value.to_s)
  end rescue nil
end

Instance Method Details

#==(other) ⇒ Object

Equal compares as Time objects

Since:

  • 0.2.1



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rdf/model/literal/time.rb', line 117

def ==(other)
  # If lexically invalid, use regular literal testing
  return super unless self.valid?

  case other
  when Literal::Time
    return super unless other.valid?
    # Compare as strings, as time includes a date portion, and adjusting for UTC
    # can create a mismatch in the date portion.
    self.object.new_offset.strftime('%H%M%S') == other.object.new_offset.strftime('%H%M%S')
  when Literal::DateTime, Literal::Date
    false
  else
    super
  end
end

#canonicalize!RDF::Literal

Converts this literal into its canonical lexical representation.

§3.2.8.2 Canonical representation

The canonical representation for time is defined by prohibiting certain options from the Lexical representation (§3.2.8.1). Specifically, either the time zone must be omitted or, if present, the time zone must be Coordinated Universal Time (UTC) indicated by a “Z”. Additionally, the canonical representation for midnight is 00:00:00.

Returns:

See Also:

Since:

  • 0.2.1



44
45
46
47
48
49
50
51
52
53
# File 'lib/rdf/model/literal/time.rb', line 44

def canonicalize!
  if self.valid?
    @string = if has_timezone?
      @object.new_offset.new_offset.strftime(FORMAT[0..-4] + 'Z')
    else
      @object.strftime(FORMAT[0..-4])
    end
  end
  self
end

#has_timezone?Boolean Also known as: has_tz?

Does the literal representation include a timezone? Note that this is only possible if initialized using a string, or ‘:lexical` option.

Returns:

Since:

  • 1.1.6



83
84
85
86
# File 'lib/rdf/model/literal/time.rb', line 83

def has_timezone?
  md = self.to_s.match(GRAMMAR)
  md && !!md[2]
end

#humanize(lang = :en) ⇒ String

Returns a human-readable value for the literal

Returns:

  • (String)

Since:

  • 1.1.6



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rdf/model/literal/time.rb', line 103

def humanize(lang = :en)
  t = object.strftime("%r")
  if has_timezone?
    t += if self.tz == 'Z'
      " UTC"
    else
      " #{self.tz}"
    end
  end
  t
end

#to_sString

Returns the value as a string. Does not normalize timezone

Returns:

  • (String)

Since:

  • 0.2.1



94
95
96
# File 'lib/rdf/model/literal/time.rb', line 94

def to_s
  @string || @object.strftime(FORMAT).sub("+00:00", 'Z')
end

#tzRDF::Literal

Returns the timezone part of arg as a simple literal. Returns the empty string if there is no timezone.



60
61
62
63
64
# File 'lib/rdf/model/literal/time.rb', line 60

def tz
  zone =  has_timezone? ? object.zone : ""
  zone = "Z" if zone == "+00:00"
  RDF::Literal(zone)
end

#valid?Boolean

Returns ‘true` if the value adheres to the defined grammar of the datatype.

Special case for date and dateTime, for which ‘0000’ is not a valid year

Returns:

Since:

  • 0.2.1



74
75
76
# File 'lib/rdf/model/literal/time.rb', line 74

def valid?
  super && !object.nil?
end