Class: RDF::Literal::Time
- Inherits:
-
RDF::Literal
- Object
- RDF::Literal
- RDF::Literal::Time
- Defined in:
- vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/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 =
RDF::URI("http://www.w3.org/2001/XMLSchema#time")
- GRAMMAR =
%r(\A(\d{2}:\d{2}:\d{2}(?:\.\d+)?)((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z).freeze
- FORMAT =
'%H:%M:%S.%L%:z'.freeze
Constants inherited from RDF::Literal
Constants included from N3::Terminals
N3::Terminals::ANON, N3::Terminals::BASE, N3::Terminals::BLANK_NODE_LABEL, N3::Terminals::DECIMAL, N3::Terminals::DOUBLE, N3::Terminals::ECHAR, N3::Terminals::ESCAPE_CHAR4, N3::Terminals::ESCAPE_CHAR8, N3::Terminals::EXPONENT, N3::Terminals::FORALL, N3::Terminals::INTEGER, N3::Terminals::IRIREF, N3::Terminals::IRI_RANGE, N3::Terminals::LANGTAG, N3::Terminals::PERCENT, N3::Terminals::PLX, N3::Terminals::PNAME_LN, N3::Terminals::PNAME_NS, N3::Terminals::PN_CHARS, N3::Terminals::PN_CHARS_BASE, N3::Terminals::PN_CHARS_BODY, N3::Terminals::PN_CHARS_U, N3::Terminals::PN_LOCAL, N3::Terminals::PN_LOCAL_BODY, N3::Terminals::PN_LOCAL_ESC, N3::Terminals::PN_PREFIX, N3::Terminals::PREFIX, N3::Terminals::QUICK_VAR_NAME, N3::Terminals::STRING_LITERAL_LONG_QUOTE, N3::Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, N3::Terminals::STRING_LITERAL_QUOTE, N3::Terminals::STRING_LITERAL_SINGLE_QUOTE, N3::Terminals::UCHAR, N3::Terminals::U_CHARS1, N3::Terminals::U_CHARS2, N3::Terminals::WS
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.
-
#humanize(lang = :en) ⇒ String
Returns a human-readable value for the literal.
-
#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Time
constructor
A new instance of Time.
-
#timezone? ⇒ Boolean
(also: #tz?, #has_tz?, #has_timezone?)
Does the literal representation include a timezone? Note that this is only possible if initialized using a string, or
:lexical
option. -
#to_s ⇒ String
Returns the value as a string.
-
#tz ⇒ RDF::Literal
Returns the timezone part of arg as a simple literal.
-
#valid? ⇒ Boolean
Returns
true
if the value adheres to the defined grammar of the datatype.
Methods inherited from RDF::Literal
#as_datetime, #as_number, #compatible?, #comperable_datatype?, #datatype?, #eql?, #escape, #hash, #inspect, #language?, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #valid_extended?, #validate!, #value, #value_hash
Methods included from Term
#<=>, #aggregate?, #as_datetime, #as_number, #compatible?, #eql?, #escape, #evaluate, #ndvars, #optimize, #sameTerm?, #term?, #terms, #to_base, #to_term, #vars
Methods included from SPARQL::Algebra::Expression
cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #validate!, #variable?
Methods included from Value
#anonymous?, #canonicalize, #constant?, #formula?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_ndvar, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!, #variable?
Constructor Details
#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Time
Returns a new instance of Time.
20 21 22 23 24 25 26 27 28 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 20 def initialize(value, datatype: nil, lexical: nil, **) @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 rescue ::DateTime.parse(value.to_s) else ::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
#==(other) ⇒ Object
Equal compares as Time objects
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 118 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.new_offset.strftime('%H%M%S.%L') == other.object.new_offset.strftime('%H%M%S.%L') 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 47 48 49 50 51 52 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 43 def canonicalize! if self.valid? @string = if timezone? @object.new_offset.new_offset.strftime(FORMAT[0..-4] + 'Z').sub('.000', '') else @object.strftime(FORMAT[0..-4]).sub('.000', '') end end self end |
#humanize(lang = :en) ⇒ String
Returns a human-readable value for the literal
104 105 106 107 108 109 110 111 112 113 114 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 104 def humanize(lang = :en) t = object.strftime("%r") if timezone? t += if self.tz == 'Z' " UTC" else " #{self.tz}" end end t end |
#timezone? ⇒ Boolean Also known as: tz?, has_tz?, has_timezone?
Does the literal representation include a timezone? Note that this is only possible if initialized using a string, or :lexical
option.
82 83 84 85 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 82 def timezone? md = self.to_s.match(GRAMMAR) md && !!md[2] end |
#to_s ⇒ String
Returns the value as a string. Does not normalize timezone
95 96 97 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 95 def to_s @string || @object.strftime(FORMAT).sub("+00:00", 'Z').sub('.000', '') end |
#tz ⇒ RDF::Literal
Returns the timezone part of arg as a simple literal. Returns the empty string if there is no timezone.
59 60 61 62 63 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 59 def tz zone = 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
73 74 75 |
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-c7ceb71ef751/lib/rdf/model/literal/time.rb', line 73 def valid? super && !object.nil? end |