Class: RDF::Literal::Time
- Inherits:
-
RDF::Literal
- Object
- RDF::Literal
- RDF::Literal::Time
- 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 =
XSD.time
- GRAMMAR =
%r(\A\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 Time objects.
-
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
-
#initialize(value, options = {}) ⇒ Time
constructor
A new instance of Time.
-
#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 = {}) ⇒ Time
Returns a new instance of Time.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rdf/model/literal/time.rb', line 19 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?(::Time) then value when value.respond_to?(:to_time) then value.to_time # Ruby 1.9+ else ::Time.parse(value.to_s) end rescue nil end |
Instance Method Details
#==(other) ⇒ Object
Equal compares as Time objects
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rdf/model/literal/time.rb', line 70 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.utc.strftime('%H%M%S') == other.object.utc.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.
43 44 45 46 |
# File 'lib/rdf/model/literal/time.rb', line 43 def canonicalize! @string = @object.utc.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z') if self.valid? self end |
#to_s ⇒ String
Returns the value as a string.
64 65 66 |
# File 'lib/rdf/model/literal/time.rb', line 64 def to_s @string || @object.strftime('%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
56 57 58 |
# File 'lib/rdf/model/literal/time.rb', line 56 def valid? super && object end |