Class: DynamicTimeZone::TimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_time_zone/time_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(time_string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dynamic_time_zone/time_parser.rb', line 5

def parse(time_string)
  raw_parts = time_string.split(' ')
  return DateTime.parse(time_string) unless raw_parts.length == 3 && (raw_parts.last.start_with?('+') || raw_parts.last.start_with?('-'))

  date_part = raw_parts[0]
  time_part = raw_parts[1]
  offset_part = raw_parts[2]
  offset_sign = offset_part[0]
  offset_amount_hour_minutes = offset_part[1..].to_i
  offset_seconds = DynamicTimeZone::OffsetFormatConvertor.new.from_hour_minutes(offset_amount_hour_minutes)

  ActiveSupport::TimeZone.new("DynamicTimeZone/#{offset_sign}#{offset_seconds}")
    .parse("#{date_part} #{time_part}")
end