Created
November 29, 2012 04:17
-
-
Save jgn/4166781 to your computer and use it in GitHub Desktop.
Doing the "right thing" over DST shifts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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