Sha256: 6fd7fdf60665c6aa8f6aec61044032f3d35407e8673ec04ce635be9ca0257ce0

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

require "time"

module Lutaml
  module Model
    module Type
      class Time < Value
        def self.cast(value)
          return nil if value.nil?

          case value
          when ::Time then value
          when ::DateTime then value.to_time
          else ::Time.parse(value.to_s)
          end
        rescue ArgumentError
          nil
        end

        def self.serialize(value)
          return nil if value.nil?

          value = cast(value)
          # value&.strftime("%Y-%m-%dT%H:%M:%S%:z")
          value&.iso8601
        end

        # # xs:time format (HH:MM:SS.mmm±HH:MM)
        # def to_xml
        #   value&.strftime("%H:%M:%S%:z")
        # end

        # # ISO8601 time format
        # def to_json
        #   value&.iso8601
        # end

        # YAML timestamp format (native)
        def to_yaml
          value&.iso8601
        end

        # # TOML time format (HH:MM:SS.mmm)
        # def to_toml
        #   value&.strftime("%H:%M:%S.%L")
        # end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lutaml-model-0.5.4 lib/lutaml/model/type/time.rb
lutaml-model-0.5.3 lib/lutaml/model/type/time.rb
lutaml-model-0.5.2 lib/lutaml/model/type/time.rb
lutaml-model-0.5.1 lib/lutaml/model/type/time.rb
lutaml-model-0.5.0 lib/lutaml/model/type/time.rb
lutaml-model-0.4.0 lib/lutaml/model/type/time.rb