Class: ParameterSubstitution::Formatters::DateTimeFormat

Inherits:
Base
  • Object
show all
Defined in:
lib/parameter_substitution/formatters/date_time_format.rb

Constant Summary collapse

MINIMUM_INTEGER_TIME =
Time.new(2000, 0o1, 0o1).to_i

Class Method Summary collapse

Methods inherited from Base

description, encoding, format, has_parameters?, key, parse_duration

Class Method Details

.from_custom_time(value, parse_options) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/parameter_substitution/formatters/date_time_format.rb', line 35

def from_custom_time(value, parse_options)
  # parse_options = { from_formatting: "%m-%d-%Y %h %m %z", from_time_zone: "Pacific Time (US & Canada)" }
  if parse_options && parse_options[:from_formatting]
    from_time_zone_with_dst = parse_options[:from_time_zone].presence && parse_time_zone_offset(parse_options[:from_time_zone])
    DateTime.strptime("#{value} #{from_time_zone_with_dst}", parse_options[:from_formatting])
  end
end

.from_parse(value) ⇒ Object



31
32
33
# File 'lib/parameter_substitution/formatters/date_time_format.rb', line 31

def from_parse(value)
  Time.zone.parse(value.to_s)
end

.from_unix_time_ms(value) ⇒ Object



27
28
29
# File 'lib/parameter_substitution/formatters/date_time_format.rb', line 27

def from_unix_time_ms(value)
  (value.to_i / 1000 > MINIMUM_INTEGER_TIME) && Time.zone.at(value.to_f / 1000)
end

.from_unix_time_sec(value) ⇒ Object



23
24
25
# File 'lib/parameter_substitution/formatters/date_time_format.rb', line 23

def from_unix_time_sec(value)
  (value.to_f > MINIMUM_INTEGER_TIME) && Time.zone.at(value.to_f)
end

.from_yyyymmddhhmmssss(value) ⇒ Object



18
19
20
21
# File 'lib/parameter_substitution/formatters/date_time_format.rb', line 18

def from_yyyymmddhhmmssss(value)
  candidate = value.to_s.strip
  candidate =~ /\d{14}/ && DateTime.strptime(candidate[0, 14], '%Y%m%d%H%M%S')
end

.parse_to_time(value, parse_options = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/parameter_substitution/formatters/date_time_format.rb', line 7

def parse_to_time(value, parse_options = nil)
  from_custom_time(value, parse_options) ||
    from_yyyymmddhhmmssss(value) ||
    from_unix_time_ms(value) ||
    from_unix_time_sec(value) ||
    from_parse(value)
rescue ArgumentError => ex
  ex.message =~ /invalid date/ || ex.message =~ /argument out of range/ or raise
  nil
end