Class: RDF::Literal::DateTime

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

Overview

A date/time literal.

See Also:

Since:

Constant Summary

DATATYPE =

Since:

  • 0.2.1

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|Z)?\Z).freeze

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Method Summary (collapse)

Methods inherited from RDF::Literal

#==, #anonymous?, #canonicalize, #eql?, #has_datatype?, #has_language?, #hash, #inspect, #invalid?, #literal?, #object, #validate!, #value

Methods included from Term

#<=>, #constant?, #variable?

Methods included from Value

#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_rdf, #uri?, #variable?

Constructor Details

- (DateTime) initialize(value, options = {})

A new instance of DateTime

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (String) :lexical — default: nil

Since:

  • 0.2.1



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

def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || DATATYPE)
  @string   = options[:lexical] if options.has_key?(:lexical)
  @string   = value if !defined?(@string) && value.is_a?(String)
  @object   = case
    when value.is_a?(::DateTime)         then value
    when value.respond_to?(:to_datetime) then value.to_datetime # Ruby 1.9+
    else ::DateTime.parse(value.to_s)
  end
end

Instance Method Details

- (RDF::Literal) canonicalize!

Converts this literal into its canonical lexical representation.

Returns:

See Also:

Since:

  • 0.2.1



30
31
32
33
# File 'lib/rdf/model/literal/datetime.rb', line 30

def canonicalize!
  @string = @object.new_offset(0).strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
  self
end

- (String) to_s

Returns the value as a string.

Returns:

  • (String)

Since:

  • 0.2.1



51
52
53
# File 'lib/rdf/model/literal/datetime.rb', line 51

def to_s
  @string || @object.strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
end

- (Boolean) valid?

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



43
44
45
# File 'lib/rdf/model/literal/datetime.rb', line 43

def valid?
  !!(value =~ GRAMMAR) && value !~ %r(\A0000)
end