Class: RDF::Literal::DateTime
- Inherits:
-
RDF::Literal
- Object
- RDF::Literal
- RDF::Literal::DateTime
- Defined in:
- lib/rdf/model/literal/datetime.rb
Overview
A date/time literal.
Constant Summary collapse
- DATATYPE =
XSD.dateTime
- GRAMMAR =
%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
Instance Attribute Summary
Attributes inherited from RDF::Literal
Instance Method Summary collapse
-
#==(other) ⇒ Object
Equal compares as DateTime objects.
-
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
-
#initialize(value, options = {}) ⇒ DateTime
constructor
A new instance of DateTime.
-
#to_s ⇒ String
Returns the value as a string.
-
#valid? ⇒ Boolean
Returns ‘true` if the value adheres to the defined grammar of the datatype.
Methods inherited from RDF::Literal
#anonymous?, #canonicalize, #comperable_datatype?, datatyped_class, #eql?, #has_datatype?, #has_language?, #hash, #inspect, #invalid?, #literal?, new, #object, #plain?, #validate!, #value
Methods included from Term
#<=>, #constant?, #eql?, #variable?
Methods included from Value
#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?
Constructor Details
#initialize(value, options = {}) ⇒ DateTime
Returns a new instance of DateTime.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rdf/model/literal/datetime.rb', line 14 def initialize(value, = {}) @datatype = RDF::URI([:datatype] || self.class.const_get(:DATATYPE)) @string = [:lexical] if .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 # Ruby 1.9+ else ::DateTime.parse(value.to_s) end rescue nil end |
Instance Method Details
#==(other) ⇒ Object
Equal compares as DateTime objects
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rdf/model/literal/datetime.rb', line 57 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.
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') if self.valid? self end |
#to_s ⇒ String
Returns the value as a string.
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 |
#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
43 44 45 |
# File 'lib/rdf/model/literal/datetime.rb', line 43 def valid? super && object && value !~ %r(\A0000) end |