TimeZone daylight savings time problems

Here is the following from my console:

Time.now
=> Mon Sep 22 11:33:34 +0200 2008
Time.now.utc_offset
=> 7200
TimeZone[Time.now.utc_offset]
=> #<TimeZone:0x11aea90 @tzinfo=nil, @utc_offset=7200, @name=“Athens”>
Time.now.in_time_zone(TimeZone[Time.now.utc_offset])
=> Mon, 22 Sep 2008 12:34:22 EEST +03:00

(I am in Spain - CET - and running on OS X with the correct timezone)

The Time.now correctly gives me the time, and the correct UTC offset
of 2 hours. It is the summer, so we are 2 hours ahead of UTC. We are
still one hour ahead of London, but London itself is an hour ahead as
they too switch to a summer time (BST).

ruby tells me that my utc_offset is 7200 - which is correct. But…
when I pull out the timezone it gives me the wrong one. Athens is 3
hours ahead of UTC, not 2 (at this time of year). So, when I convert a
time using the timezone it gives me it is an hour out!

I had hoped that time zone woes were behind me after switching to RoR
from Java. But it still seems to be a problem.

How can I get the correct Time Zone?

There is a bug tracked (http://dev.rubyonrails.org/ticket/4551) but it
seems to be set to closed… but it definitely isn’t fixed!

Right now I am doing this:

offset = Time.now.dst? ? 60 : 0

Anyone have any better solutions?

Time.zone will give you the time zone currently set in Rails 2.1

You should set the time zone of your environment in the
environment.db:
config.time_zone = ‘Amsterdam’

You can ask the users of your application to select their time zone
(that’s something they’ll have to tell you, it’s not possible for
Rails to guess :-)), save it in the user table as time_zone variable
and set it for your current user in a before_filter: Time.zone =
@current_user.time_zone.

More details are found here:

Hi,
Thanks for that, but it completely misses the point in that I can not
even get the proper time zone conversion inside the server.
I am not bringing a user’s timezone into the equation!

I can’t go back and forth inside the same instance and get reliable
results:
Time.now and Time.now.in_time_zone(TimeZone[Time.now.utc_offset])
should give me the same times back! it doesn’t.

Anyway, I think I found another bug listed for this, so I will trust
that someone more clever than I will figure it out.

phil wrote:

Time.now and Time.now.in_time_zone(TimeZone[Time.now.utc_offset])
should give me the same times back! it doesn’t.

It looks like its working for me. I’m on Rails 2.1.1 on Mac OS X 10.5.5:

Loading development environment (Rails 2.1.1)

Time.now
=> Tue Sep 23 11:13:02 -0500 2008

Time.now.utc_offset
=> -18000

Time.now.in_time_zone(TimeZone[Time.now.utc_offset])
=> Tue, 23 Sep 2008 11:13:28 COT -05:00

TimeZone[Time.now.utc_offset]
=> #<ActiveSupport::TimeZone:0x5cb1b0 @utc_offset=-18000,
@name=“Bogota”, @tzinfo=#<TZInfo::DataTimezone: America/Bogota

Although the TimeZone[Time.now.utc_offset] line didn’t return the
correct time zone for me, as I’m in Central (US & Canada).

Peace.

Interesting… I am going to guess that you are not in a place that
uses daylight savings time?

On Sep 23, 6:15 pm, Phillip K. [email protected]

Ok phil, I see the point you’re trying to make.

The thing I was trying to say is that it’s just not possible for Rails
to really know in which time zone you are. So when you call
TimeZone[some_offset], it can only make a guess about the time zone,
because it doesn’t know if you’re in a time zone with or without DST.

Again, in Rails 2.1, you should set your time zone via the
config.time_zone command so that Rails knows in which time zone is
your server. And you retrieve your time zone (that was your question,
right?) using
Time.zone
So not using TimeZone.[] class method.

Thanks,
You are correct in that I should use Time.zone for server time, but I
have a value coming from the client that is there offset to gmt (from
a javascript call)… oh well!

thanks though

phil wrote:

Interesting… I am going to guess that you are not in a place that
uses daylight savings time?

On Sep 23, 6:15 pm, Phillip K. [email protected]

No, I am in a place that uses DST.

Peace.

phil wrote:

Thanks,
You are correct in that I should use Time.zone for server time, but I
have a value coming from the client that is there offset to gmt (from
a javascript call)… oh well!

Hello Phil,

Having quite a similar problem. Any idea how could I get a TimeZone
instance for a given offset with disabled DST? Did you solve your
problem with the offset?

Regards,
KIR