Class: RDF::Literal::DateTime

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

Overview

A date/time literal.

Constant Summary

DATATYPE =

Since:

  • 0.2.1

RDF::XSD.dateTime
GRAMMAR =

Since:

  • 0.2.1

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

Since:

  • 0.2.1

'%Y-%m-%dT%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?, #eql?, #escape, #has_datatype?, #has_language?, #hash, #inspect, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #validate!, #value, #value_hash

Methods included from Term

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

Methods included from Value

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

Constructor Details

#initialize(value, datatype: nil, lexical: nil, **options) ⇒ DateTime

Returns a new instance of DateTime

Parameters:

  • value (DateTime)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :lexical (String) — default: nil

Since:

  • 0.2.1



15
16
17
18
19
20
21
22
23
# File 'lib/rdf/model/literal/datetime.rb', line 15

def initialize(value, datatype: nil, lexical: nil, **options)
  @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
  @string   = lexical || (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
    else ::DateTime.parse(value.to_s)
  end rescue nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Literal

Instance Method Details

#==(other) ⇒ Object

Equal compares as DateTime objects

Since:

  • 0.2.1



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rdf/model/literal/datetime.rb', line 124

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

  case other
  when Literal::DateTime
    return super unless other.valid?
    self.object == other.object
  when Literal::Time, Literal::Date
    false
  else
    super
  end
end

#canonicalize!RDF::Literal

Converts this literal into its canonical lexical representation. with date and time normalized to UTC.

Returns:

See Also:

Since:

  • 0.2.1



31
32
33
34
35
36
37
38
39
40
# File 'lib/rdf/model/literal/datetime.rb', line 31

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



88
89
90
91
# File 'lib/rdf/model/literal/datetime.rb', line 88

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



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rdf/model/literal/datetime.rb', line 109

def humanize(lang = :en)
  d = object.strftime("%r on %A, %d %B %Y")
  if has_timezone?
    zone = if self.tz == 'Z'
      "UTC"
    else
      self.tz
    end
    d.sub!(" on ", " #{zone} on ")
  end
  d
end

#timezoneRDF::Literal

Returns the timezone part of arg as an xsd:dayTimeDuration, or nil if lexical form of literal does not include a timezone.

Returns:

Since:

  • 0.2.1



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rdf/model/literal/datetime.rb', line 58

def timezone
  if tz == 'Z'
    RDF::Literal("PT0S", datatype: RDF::XSD.dayTimeDuration)
  elsif md = tz.to_s.match(/^([+-])?(\d+):(\d+)?$/)
    plus_minus, hour, min = md[1,3]
    plus_minus = nil unless plus_minus == "-"
    hour = hour.to_i
    min = min.to_i
    res = "#{plus_minus}PT#{hour}H#{"#{min}M" if min > 0}"
    RDF::Literal(res, datatype: RDF::XSD.dayTimeDuration)
  end
end

#to_sString

Returns the timezone of the literal. If the

Returns the value as a string.

Returns:

  • (String)

Since:

  • 0.2.1



100
101
102
# File 'lib/rdf/model/literal/datetime.rb', line 100

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.



47
48
49
50
51
# File 'lib/rdf/model/literal/datetime.rb', line 47

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



79
80
81
# File 'lib/rdf/model/literal/datetime.rb', line 79

def valid?
  super && object && value !~ %r(\A0000)
end