[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Time zone offset and daylight saving

easleydp

1/29/2006 8:17:00 PM

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

5 Answers

Gene Tani

1/29/2006 10:25:00 PM

0


easleydp@gmail.com wrote:
> 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?ca...

Ceol

1/30/2006 8:55:00 AM

0

My understanding is that a time change is implemented by switching time
zones; you change from Pacific Standard Time to Pacific Daylight Time.
Pacific Standard Time would continue to have the same offset in July as it
would in January; it's just that in July no clocks are set to Pacific
Standard. (Except the clock on my kitchen scale, of course...)

And good luck figuring out when you're supposed to switch; it's arbitary,
set by legislation, not by something you can figure out. And the US at
least will be changing in the near future. And there are places that never
make the switch, too. Google for things like 'daylight savings time zone'
and you'll get an idea of the pain involved.

- James

<easleydp@gmail.com> wrote in message
news:1138565845.900427.111410@g14g2000cwa.googlegroups.com...
>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


easleydp

1/30/2006 8:48:00 PM

0

Thanks for the help, guys.

Took a look at the Python port of the tz database, then discovered
there's a Ruby version! Check out these links:

Rubyforge: http://tzinfo.ruby...
How to use with Rails:
http://lunchroom.lunchboxsoftware.com/pages/tz...

Jamis Buck

2/1/2006 10:23:00 PM

0

The TZInfo lib for Ruby does a pretty good job of encapsulating all
those rules:

http://tzinfo.ruby...

- Jamis

On Feb 1, 2006, at 2:38 PM, JM wrote:

> My understanding is that a time change is implemented by switching
> time
> zones; you change from Pacific Standard Time to Pacific Daylight Time.
> Pacific Standard Time would continue to have the same offset in
> July as it
> would in January; it's just that in July no clocks are set to Pacific
> Standard. (Except the clock on my kitchen scale, of course...)
>
> And good luck figuring out when you're supposed to switch; it's
> arbitary,
> set by legislation, not by something you can figure out. And the
> US at
> least will be changing in the near future. And there are places
> that never
> make the switch, too. Google for things like 'daylight savings
> time zone'
> and you'll get an idea of the pain involved.
>
> - James
>
> <easleydp@gmail.com> wrote in message
> news:1138565845.900427.111410@g14g2000cwa.googlegroups.com...
>> 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
>
>
>



Trevor Squires

2/1/2006 10:36:00 PM

0

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

HTH,
Trevor

On 1-Feb-06, at 1:38 PM, JM wrote:

> My understanding is that a time change is implemented by switching
> time
> zones; you change from Pacific Standard Time to Pacific Daylight Time.
> Pacific Standard Time would continue to have the same offset in
> July as it
> would in January; it's just that in July no clocks are set to Pacific
> Standard. (Except the clock on my kitchen scale, of course...)
>
> And good luck figuring out when you're supposed to switch; it's
> arbitary,
> set by legislation, not by something you can figure out. And the
> US at
> least will be changing in the near future. And there are places
> that never
> make the switch, too. Google for things like 'daylight savings
> time zone'
> and you'll get an idea of the pain involved.
>
> - James
>
> <easleydp@gmail.com> wrote in message
> news:1138565845.900427.111410@g14g2000cwa.googlegroups.com...
>> 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
>
>



--
Trevor Squires
http://somethingl...