Rails 2.3.5 parses date inconsistently
To start, I am using an old rails version because of Redmine.
I am trying to write a script for updating Redmine's database dump timestamps' to a specific timezone, therefore I need ruby to parse them and change the timezone, assuming rails can handle dst boundaries.
Nevertheless, I found an inconsistency in (or misunderstood) how Time.parse works.
Here's the script I executed on my Rails 2.3.5 raw application:
impulse@ImpulseServer:~/pgtz_converter$ script/console
Loading development environment (Rails 2.3.5)
>> Time.zone
=> #<ActiveSupport::TimeZone:0x7fa94a57b9a8 @tzinfo=nil, @name="UTC", @utc_offset=0>
>> Time.parse('2011-02-19 23:00:00')
=> Sat Feb 19 23:00:00 -0200 2011
>> Time.zone
=> #<ActiveSupport::TimeZone:0x7fa94a57b9a8 @tzinfo=nil, @name="UTC", @utc_offset=0>
>> Time.parse('2011-02-20 00:00:00')
=> Sun Feb 20 00:00:00 -0300 2011
>> Time.zone
=> #<ActiveSupport::TimeZone:0x7fa94a57b9a8 @tzinfo=nil, @name="UTC", @utc_offset=0>
>>开发者_开发知识库; Time.parse('2011-02-19 23:00:00')
=> Sat Feb 19 23:00:00 -0300 2011
>> Time.zone
=> #<ActiveSupport::TimeZone:0x7fa94a57b9a8 @tzinfo=nil, @name="UTC", @utc_offset=0>
>> exit
Here's what is going on above:
The server local datetime zone is BRT.
impulse@ImpulseServer:~/pgtz_converter$ date
Fri Apr 8 19:17:34 BRT 2011
First I check Time.zone (which is UTC), then try to parse a DST date. The output is fine, DST is on (I presume Time.zone = UTC means Ruby checked the server zone configuration and used it). Then I try another date, in which DST was not active anymore (DST ended on 02/20/2011 00:00 here). This date is correctly parsed (GMT-3).
Now the problem begins.
I try to parse again the old DST date, and the result is wrong! It wasn't read as a DST date!
I thought perhaps the parsing changed the Time.zone, but as it is pasted it is UTC all the time.
What is causing this ?
Thank you for reading.
Nilo
You might want to change to use the ActiveSupport::TimeWithZone class in rails.
精彩评论