Sha256: a7cadcdf5f90835e76d3a62c14cc737a008f0334961c5645da9c00ffa8a381b4

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

module RDF; class Literal
  ##
  # A date/time literal.
  #
  # @see   http://www.w3.org/TR/xmlschema11-2/#dateTime
  # @since 0.2.1
  class DateTime < Temporal
    DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")
    GRAMMAR  = %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   = '%Y-%m-%dT%H:%M:%S.%L'.freeze

    ##
    # 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.
    #
    # @param  [DateTime] value
    # @option options [String] :lexical (nil)
    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

    ##
    # Returns a human-readable value for the literal
    #
    # @return [String]
    # @since 1.1.6
    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
  end # DateTime
end; end # RDF::Literal

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rdf-3.2.8 lib/rdf/model/literal/datetime.rb
rdf-3.2.7 lib/rdf/model/literal/datetime.rb
rdf-3.2.6 lib/rdf/model/literal/datetime.rb
rdf-3.2.5 lib/rdf/model/literal/datetime.rb