Class: EML::UK::ParseDate

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/eml/uk/lib/parse_date.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date_string) ⇒ ParseDate

Returns a new instance of ParseDate.



15
16
17
# File 'lib/eml/uk/lib/parse_date.rb', line 15

def initialize(date_string)
  @date_string = date_string
end

Class Method Details

.call(date_string) ⇒ Object



11
12
13
# File 'lib/eml/uk/lib/parse_date.rb', line 11

def self.call(date_string)
  new(date_string).call
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eml/uk/lib/parse_date.rb', line 19

def call
  return if @date_string.nil?

  matches = @date_string.match(%r(/Date\(((\d{13})([\+\-]\d{4})?)\)/))
  return if matches.nil?

  milliseconds = BigDecimal(matches[2])
  timezone_offset = matches[3]

  time = Time.at(milliseconds / 1000).utc
  return time if timezone_offset.nil?

  time.localtime("#{timezone_offset[0, 3]}:#{timezone_offset[3, 4]}")
end