Sha256: 0252736bf5f50a7d87be7a3831d8addfe6a8dc76820c57ab624cd6e22210223a

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

require "time"

module Lutaml
  module Model
    module Type
      class TimeWithoutDate < Value
        # TODO: we probably want to do something like this because using
        # Time.parse will set the date to today.
        #
        # time = ::Time.parse(value.to_s)
        # ::Time.new(1, 1, 1, time.hour, time.min, time.sec)

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

          case value
          when ::Time then value
          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("%H:%M:%S") # Format as HH:MM:SS
        end

        # Instance methods for format-specific serialization
        def to_xml
          self.class.serialize(value)
        end

        def to_json(*_args)
          self.class.serialize(value)
        end

        def to_yaml
          self.class.serialize(value)
        end

        def to_toml
          value.strftime("%H:%M:%S.%L") # Include milliseconds for TOML
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lutaml-model-0.3.28 lib/lutaml/model/type/time_without_date.rb
lutaml-model-0.3.27 lib/lutaml/model/type/time_without_date.rb
lutaml-model-0.3.26 lib/lutaml/model/type/time_without_date.rb
lutaml-model-0.3.25 lib/lutaml/model/type/time_without_date.rb