Time zone offset and daylight saving

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?!]
 
G

Gene Tani

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
--

i have a note to check out this Olson tz dtabase but haven't gotten
round to it

http://python.codezoo.com/pub/component/3848?category=193
 
T

Trevor Squires

Hi,

Regarding "good luck figuring out when you're supposed to switch" -
Phil Ross has released a ruby library that uses the Olson Timezone
database (considered the definitive source for world timezones).

You're right that it's a pain (it's a political issue after all) but
it's not insurmountable if you use the Olson data.

Phil's library is available at:

http://tzinfo.rubyforge.org/

HTH,
Trevor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top