Class: Lutaml::Model::Type::TimeWithoutDate

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/time_without_date.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

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)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lutaml/model/type/time_without_date.rb', line 15

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

  case value
  when ::Time then value
  else
    # Check if input has invalid hour format (24:00:00 or higher)
    if value.to_s =~ /^(\d+):/
      hour = ::Regexp.last_match(1).to_i
      return nil if hour >= 24
    end

    ::Time.parse(value.to_s)
  end
rescue ArgumentError
  nil
end

.default_xsd_typeString

XSD type for TimeWithoutDate

Returns:



44
45
46
# File 'lib/lutaml/model/type/time_without_date.rb', line 44

def self.default_xsd_type
  "xs:time"
end

.serialize(value) ⇒ Object



34
35
36
37
38
39
# File 'lib/lutaml/model/type/time_without_date.rb', line 34

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

  value = cast(value)
  value.strftime("%H:%M:%S") # Format as HH:MM:SS
end