Class: RDF::Literal::DateTime

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

Overview

A date/time literal.

Constant Summary collapse

DATATYPE =

Since:

  • 0.2.1

RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")
GRAMMAR =

Since:

  • 0.2.1

%r(\A(-?(?:\d{4}|[1-9]\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.%L'.freeze

Constants inherited from Temporal

Temporal::ZONE_GRAMMAR

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 Temporal

#+, #-, #<=>, #==, #adjust_to_timezone, #adjust_to_timezone!, #canonicalize!, #day, #hours, #milliseconds?, #minutes, #month, #seconds, #timezone, #timezone?, #to_s, #tz, #valid?, #year

Methods inherited from RDF::Literal

#<=>, #==, #canonicalize!, #compatible?, #comperable_datatype2?, #comperable_datatype?, #datatype?, #eql?, #escape, #hash, #inspect, #language?, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #to_s, #valid?, #validate!, #value, #value_hash

Methods included from Term

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

Methods included from Value

#anonymous?, #canonicalize, #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?, #valid?, #validate!, #variable?

Constructor Details

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

Internally, a DateTime is represented using a native ::DateTime. If initialized from a ::Date, there is no timezone component, If initialized from a ::DateTime, the timezone is taken from that native object, otherwise, a timezone (or no timezone) is taken from the string representation having a matching zzzzzz component.

Parameters:

  • value (DateTime)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :lexical (String) — default: nil

Since:

  • 0.2.1


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rdf/model/literal/datetime.rb', line 17

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)
      @zone = value.zone
      value
    when value.respond_to?(:to_datetime) 
      @zone = value.to_datetime.zone
      value.to_datetime
    else
      md = value.to_s.match(GRAMMAR)
      _, _, tz = Array(md)
      if tz
        @zone = tz == 'Z' ? '+00:00' : tz
      else
        @zone = nil # No timezone
      end
      ::DateTime.parse(value.to_s)
  end rescue ::DateTime.new
end

Dynamic Method Handling

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

Instance Method Details

#humanize(lang = :en) ⇒ String

Returns a human-readable value for the literal

Returns:

Since:

  • 1.1.6


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

def humanize(lang = :en)
  d = object.strftime("%r on %A, %d %B %Y")
  if timezone?
    z = @zone == '+00:00' ? "UTC" : @zone
    d.sub!(" on ", " #{z} on ")
  end
  d
end