E
easleydp
I don't seem to be able to correctly calculate the time offset between
two time zones when daylight saving is in effect. The code below
demonstrates that Ruby knows about daylight saving, but I get the wrong
answer when I calculate the offset.
BTW, yes, I know I can use Time#utc_offset, but I believe that depends
on me actually being in that timezone. I'm looking for a way to
calculate the offset between two arbitrary timezones, neither of which
is my local timezone.
Thanks for any help.
David
--
# Compute the time offset between two timezones (UTC and PST)
# in winter
t1 = Time.parse "2006-01-29 00:00:00 UTC"
t2 = Time.parse "2006-01-29 00:00:00 PST"
# Check no daylight saving
puts t1.isdst.to_s + " / " + t2.isdst.to_s # false / false
# Show time offset in hours
puts ((t1 - t2)/(60*60)).to_s # -8.0
# So far so good.
# Now repeat for a day in July. Because the clocks have gone forward
# in PST but not in UTC the time difference should now be -7 hours
t1 = Time.parse "2006-07-29 00:00:00 UTC"
t2 = Time.parse "2006-07-29 00:00:00 PST"
# Check daylight saving is being used only in PST
puts t1.isdst.to_s + " / " + t2.isdst.to_s # false / true
# Show time offset in hours
puts ((t1 - t2)/(60*60)).to_s # -8.0 [Why?!]
two time zones when daylight saving is in effect. The code below
demonstrates that Ruby knows about daylight saving, but I get the wrong
answer when I calculate the offset.
BTW, yes, I know I can use Time#utc_offset, but I believe that depends
on me actually being in that timezone. I'm looking for a way to
calculate the offset between two arbitrary timezones, neither of which
is my local timezone.
Thanks for any help.
David
--
# Compute the time offset between two timezones (UTC and PST)
# in winter
t1 = Time.parse "2006-01-29 00:00:00 UTC"
t2 = Time.parse "2006-01-29 00:00:00 PST"
# Check no daylight saving
puts t1.isdst.to_s + " / " + t2.isdst.to_s # false / false
# Show time offset in hours
puts ((t1 - t2)/(60*60)).to_s # -8.0
# So far so good.
# Now repeat for a day in July. Because the clocks have gone forward
# in PST but not in UTC the time difference should now be -7 hours
t1 = Time.parse "2006-07-29 00:00:00 UTC"
t2 = Time.parse "2006-07-29 00:00:00 PST"
# Check daylight saving is being used only in PST
puts t1.isdst.to_s + " / " + t2.isdst.to_s # false / true
# Show time offset in hours
puts ((t1 - t2)/(60*60)).to_s # -8.0 [Why?!]