Module: Ozone::Formatter

Defined in:
lib/ozone.rb

Class Method Summary collapse

Class Method Details

.call(time:, offset:, observes_dst: true, format: '%Y-%m-%d %H:%M') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ozone.rb', line 9

def call(time:, offset:, observes_dst: true, format: '%Y-%m-%d %H:%M')
  timezone = ActiveSupport::TimeZone[offset/60]
  time_with_zone = time.in_time_zone(timezone)
  if time_with_zone.dst? && !observes_dst
    # If we're during daylight savings time, but
    # we are not observing daylight savings,
    # there is a one-hour window where there is not
    # an equivalent time between standard and daylight
    # savings times. In order to deal with this,
    # we subtract two hours from the offset and then
    # take 2 hours from the given time. This frees
    # us to string format the time without ruby
    # making adjustments internally.
    offset -= 120
    timezone = ActiveSupport::TimeZone[offset/60]
    2.hours.since(time.in_time_zone(timezone)).strftime(format)
  else
    time_with_zone.strftime(format)
  end
end