Class: Reqif::HighPrecisionDateTime

Inherits:
Lutaml::Model::Type::DateTime
  • Object
show all
Defined in:
lib/reqif/high_precision_date_time.rb

Overview

High precision DateTime type that preserves fractional seconds precision

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ HighPrecisionDateTime

Returns a new instance of HighPrecisionDateTime.



10
11
12
13
# File 'lib/reqif/high_precision_date_time.rb', line 10

def initialize(value = nil)
  super
  @precision = 0
end

Instance Attribute Details

#has_explicit_timezoneObject

Returns the value of attribute has_explicit_timezone.



8
9
10
# File 'lib/reqif/high_precision_date_time.rb', line 8

def has_explicit_timezone
  @has_explicit_timezone
end

#precisionObject

Returns the value of attribute precision.



8
9
10
# File 'lib/reqif/high_precision_date_time.rb', line 8

def precision
  @precision
end

Class Method Details

.extract_precision(xml_string) ⇒ Object

Extract the precision of the milliseconds from the XML string



25
26
27
28
29
30
31
32
# File 'lib/reqif/high_precision_date_time.rb', line 25

def self.extract_precision(xml_string)
  if xml_string.include?(".")
    milliseconds_part = xml_string.split(".").last.split(/Z|\+|-/).first
    milliseconds_part.length
  else
    0
  end
end

.from_xml(xml_string) ⇒ Object

The format looks like this ‘2012-04-07T01:51:37.112+02:00` or this `2021-07-01T01:12:06.749Z`



17
18
19
20
21
22
# File 'lib/reqif/high_precision_date_time.rb', line 17

def self.from_xml(xml_string)
  new(::DateTime.parse(xml_string)).tap do |date_time|
    date_time.precision = extract_precision(xml_string)
    date_time.has_explicit_timezone = xml_string.match?(/[Zz]|[+-]\d{2}:\d{2}$/)
  end
end

Instance Method Details

#iso8601(precision = nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/reqif/high_precision_date_time.rb', line 44

def iso8601(precision = nil)
  if precision.nil? || precision.zero?
    value.iso8601
  else
    value.strftime("%Y-%m-%dT%H:%M:%S.%#{precision}N%z")
  end
end

#to_xmlObject



34
35
36
37
38
39
40
41
42
# File 'lib/reqif/high_precision_date_time.rb', line 34

def to_xml
  base = "%Y-%m-%dT%H:%M:%S"
  pattern = precision.positive? ? "#{base}.%#{precision}N" : base
  if has_explicit_timezone
    value.offset.zero? ? value.strftime("#{pattern}Z") : value.strftime("#{pattern}%:z")
  else
    value.strftime(pattern)
  end
end