Forum: Ruby-core [Bug #3737] DateTime#to_time doesn't preserve zone

Posted by Jan Friedrich (Guest)
on 2010-08-23 20:30
(Received via mailing list)
Bug #3737: DateTime#to_time doesn't preserve zone
http://redmine.ruby-lang.org/issues/show/3737

Author: Jan Friedrich
Status: Open, Priority: Normal
ruby -v: ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]

Why does DateTime#to_time doesn't preserve the time zone but convert the 
timestamp to local time zone?

d = DateTime.new(2010, 8, 23, 20, 14, 10, '+08:00')
puts d # => 2010-08-23T20:14:10+08:00

t = d.to_time
puts t # => 2010-08-23 14:14:10 +0200

I would expect that t is in time zone +08:00 not +02:00. If I want the 
time in local time zone I can call t.localtime, but I have no chance to 
preserve the zone information. If this behavior is intentional the the 
DateTime#to_time method is useless for most of my use cases. My actual 
workaround is following code:

t = Time.new(d.year, d.month, d.day, d.hour, d.minute, d.second, d.zone)
puts t #= => 2010-08-23 20:14:10 +0800

Best regards
Jan
Posted by tadayoshi funaba (Guest)
on 2010-08-25 16:55
(Received via mailing list)
Issue #3737 has been updated by tadayoshi funaba.

Status changed from Open to Rejected
Assigned to set to tadayoshi funaba

not a bug

this is same as Time.parse:

Time.parse('2010-08-23T20:14:10+08:00')
#=> 2010-08-23 21:14:10 +0900

----------------------------------------
http://redmine.ruby-lang.org/issues/show/3737
Posted by Bernard Duchesne (Guest)
on 2010-08-31 02:54
(Received via mailing list)
Issue #3737 has been updated by Bernard Duchesne.


Both Time.parse and DateTime.to_time have the same problem. They convert 
into the local timezone. The original timezone info is lost. In your 
example it went from +8:00 to +9:00. There is no way to retrieve the 
original +8:00 timezone after conversion.

Keeping the original timezone can be important. They represent a context 
for the time. For instance,  when times are entered on a client 
application (not a server), they include the local timezone as set on 
the computer. It is then easy to see which times corresponds to local 
business hours (anything between 9am and 5pm) irrespective of the 
timezone. If however all times are converted into a different timezone, 
it becomes hard to see which times were entered before 5pm.

It is probably too late to change the behaviour of a published API like 
DateTime.to_time but maybe a new function could be added that retains 
the timezone. At the minimum, this loss of information (timezone) should 
be documented in both Time.parse and DateTime.to_time.

----------------------------------------
http://redmine.ruby-lang.org/issues/show/3737
Posted by Diogo Almeida (Guest)
on 2010-12-23 16:01
(Received via mailing list)
Issue #3737 has been updated by Diogo Almeida.


I'm suffering from this same issue, as described here: 
http://stackoverflow.com/questions/4518967/making-sense-of-rails-timezone.

If this is not a bug then what's the best way to go around this issue?

Best regards,
DBA
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.