Class: Lutaml::Model::Type::DateTime

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

Overview

Date and time representation

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

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

Constructor Details

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

Class Method Details

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



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

def self.cast(value, _options = {})
  return value if value.nil? || Utils.uninitialized?(value)

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

.serialize(value) ⇒ Object



20
21
22
23
24
# File 'lib/lutaml/model/type/date_time.rb', line 20

def self.serialize(value)
  return value if value.nil? || Utils.uninitialized?(value)

  cast(value)&.iso8601
end

Instance Method Details

#to_json(*_args) ⇒ Object

RFC3339 (ISO8601 with timezone)



32
33
34
# File 'lib/lutaml/model/type/date_time.rb', line 32

def to_json(*_args)
  value&.iso8601
end

#to_tomlObject

TOML datetime format (RFC3339)



42
43
44
# File 'lib/lutaml/model/type/date_time.rb', line 42

def to_toml
  value&.iso8601
end

#to_xmlObject

xs:dateTime format (ISO8601 with timezone)



27
28
29
# File 'lib/lutaml/model/type/date_time.rb', line 27

def to_xml
  value&.iso8601
end

#to_yamlObject

YAML timestamp format (native)



37
38
39
# File 'lib/lutaml/model/type/date_time.rb', line 37

def to_yaml
  value&.iso8601
end