Class: Lutaml::Model::Type::DateTime
- Defined in:
- lib/lutaml/model/type/date_time.rb
Overview
Date and time representation
Constant Summary
Constants inherited from Value
Instance Attribute Summary
Attributes inherited from Value
Class Method Summary collapse
- .cast(value, _options = {}) ⇒ Object
-
.default_xsd_type ⇒ String
Default XSD type for DateTime.
-
.format_datetime_iso8601(datetime) ⇒ String
Format DateTime as ISO8601 string preserving fractional seconds and timezone offset.
- .serialize(value) ⇒ Object
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
Methods included from UninitializedClassGuard
Constructor Details
This class inherits a constructor from Lutaml::Model::Type::Value
Class Method Details
.cast(value, _options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/lutaml/model/type/date_time.rb', line 10 def self.cast(value, = {}) return super if Utils.uninitialized?(value) return nil if value.nil? # If already a DateTime type wrapper, return as-is return value if value.is_a?(self) case value when ::DateTime then value when ::Time then value.to_datetime else ::DateTime.parse(value.to_s) end rescue ArgumentError nil end |
.default_xsd_type ⇒ String
Default XSD type for DateTime
54 55 56 |
# File 'lib/lutaml/model/type/date_time.rb', line 54 def self.default_xsd_type "xs:dateTime" end |
.format_datetime_iso8601(datetime) ⇒ String
Format DateTime as ISO8601 string preserving fractional seconds and timezone offset. Keeps up to 6 decimal places, strips trailing zeros beyond 3.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lutaml/model/type/date_time.rb', line 40 def self.format_datetime_iso8601(datetime) if datetime.sec_fraction.zero? datetime.iso8601 else # iso8601(6) produces exactly 6 decimal places # e.g. 0.5s -> "0.500000", 0.123456s -> "0.123456" # Strip trailing zeros beyond 3 decimal places: ".500000" -> ".500" datetime.iso8601(6).sub(/(\.\d{3})0{3}([+-])/, '\1\2') end end |
.serialize(value) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/lutaml/model/type/date_time.rb', line 26 def self.serialize(value) return nil if value.nil? dt = cast(value) return nil unless dt format_datetime_iso8601(dt) end |