Class: QuickAndRuby::Time::EpochConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_and_ruby/time/epoch_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(millis:, utc:) ⇒ EpochConverter

Returns a new instance of EpochConverter.



10
11
12
13
# File 'lib/quick_and_ruby/time/epoch_converter.rb', line 10

def initialize(millis:, utc:)
  @millis = millis
  @utc = utc
end

Instance Attribute Details

#millisObject (readonly)

Returns the value of attribute millis.



8
9
10
# File 'lib/quick_and_ruby/time/epoch_converter.rb', line 8

def millis
  @millis
end

#utcObject (readonly)

Returns the value of attribute utc.



8
9
10
# File 'lib/quick_and_ruby/time/epoch_converter.rb', line 8

def utc
  @utc
end

Instance Method Details

#convert(value_str) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/quick_and_ruby/time/epoch_converter.rb', line 19

def convert(value_str)
  if value_str.match(/\D/)
    convert_time(value_str)
  else
    convert_epoch(value_str)
  end
end

#convert_all(*values) ⇒ Object



15
16
17
# File 'lib/quick_and_ruby/time/epoch_converter.rb', line 15

def convert_all(*values)
  values.map { |value_str| convert(value_str) }
end

#convert_epoch(epoch_str) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/quick_and_ruby/time/epoch_converter.rb', line 34

def convert_epoch(epoch_str)
  epoch = epoch_str.to_i
  epoch /= 1000.0 if epoch_str.size > 10
  time = ::Time.at(epoch)
  time = time.utc if utc
  time.iso8601(3)
end

#convert_time(time_str) ⇒ Object



27
28
29
30
31
32
# File 'lib/quick_and_ruby/time/epoch_converter.rb', line 27

def convert_time(time_str)
  time = ::Time.parse(time_str)
  epoch = time.to_f
  epoch *= 1000 if millis
  epoch.to_i
end