Class: Lutaml::Model::Type::Time

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/time.rb

Constant Summary

Constants inherited from Value

Value::EMPTY_OPTIONS

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Methods inherited from Value

format_type_serializer_for, from_format, #initialize, #initialized?, register_format_to_from_methods, register_format_type_serializer, #to_s

Methods included from Xml::Type::Configurable

included

Methods included from UninitializedClassGuard

#cast, #serialize

Constructor Details

This class inherits a constructor from Lutaml::Model::Type::Value

Class Method Details

.cast(value, _options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lutaml/model/type/time.rb', line 9

def self.cast(value, _options = {})
  return super if Utils.uninitialized?(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

.default_xsd_typeString

XSD type for Time

Returns:



40
41
42
# File 'lib/lutaml/model/type/time.rb', line 40

def self.default_xsd_type
  "xs:time"
end

.serialize(value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lutaml/model/type/time.rb', line 22

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

  time = cast(value)
  return nil unless time

  # Only include fractional seconds if they exist
  if time.subsec.zero?
    time.iso8601
  else
    # Keep minimum 3 decimal places, remove last 3 zeros if present
    time.iso8601(6).sub(/(\.\d{3})0{3}([+-])/, '\1\2')
  end
end