Skip to content

Instantly share code, notes, and snippets.

@jgn
Created November 29, 2012 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgn/4166781 to your computer and use it in GitHub Desktop.
Save jgn/4166781 to your computer and use it in GitHub Desktop.
Doing the "right thing" over DST shifts
require 'active_support/version'
require 'active_support/core_ext/time/zones'
require 'active_support/core_ext/time/calculations'
require 'active_support/time_with_zone'
puts "ActiveSupport: #{ActiveSupport::VERSION::STRING}"
Time.zone = 'Eastern Time (US & Canada)'
def show_time user_time
local = Time.zone.parse user_time
utc = local.utc
puts "user: #{user_time} local: #{local}; utc: #{utc}"
end
def show_utc_recording_start_finish(user_start, user_finish)
local_start = Time.zone.parse(user_start)
local_finish = Time.zone.parse(user_finish)
local_delta = local_finish - local_start
fixed_delta = Time.parse("#{user_finish} UTC") - Time.zone.parse("#{user_start} UTC")
puts "local delta: #{local_delta}"
puts "fixed delta: #{fixed_delta}"
delta = [local_delta, fixed_delta].max
puts "Delta to use: #{delta}"
start_utc = local_start.utc
finish_utc = start_utc + delta
duration_minutes = ((finish_utc - start_utc) / 60).round
puts "User #{user_start} - #{user_finish}"
puts "Local #{local_start} - #{local_start}"
puts "UTC #{start_utc} - #{finish_utc} (Duration in minutes: #{duration_minutes})"
end
show_time '2013-03-10 01:00:00'
show_time '2013-03-10 02:00:00'
show_time '2013-03-10 03:00:00'
puts
show_time '2013-11-03 01:00:00'
show_time '2013-11-03 02:00:00'
show_time '2013-11-03 03:00:00'
puts
show_utc_recording_start_finish '2013-03-10 01:00:00', '2013-03-10 03:00:00'
puts
show_utc_recording_start_finish '2013-11-03 01:00:00', '2013-11-03 03:00:00'
puts
show_utc_recording_start_finish '2013-12-25 06:00:00', '2013-12-25 07:00:00'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment