Project

General

Profile

Actions

Bug #3737

closed

DateTime#to_time doesn't preserve zone

Added by janfri (Jan Friedrich) over 14 years ago. Updated over 13 years ago.

Status:
Rejected
Target version:
-
ruby -v:
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
Backport:
[ruby-core:31833]

Description

=begin
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
=end

Actions #1

Updated by tadf (tadayoshi funaba) over 14 years ago

  • Status changed from Open to Rejected
  • Assignee set to tadf (tadayoshi funaba)

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

=end

Actions #2

Updated by bernard (Bernard Duchesne) over 14 years ago

=begin
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. Also, I think it would be important to document this limitation in both Time.parse and DateTime.to_time.

=end

Actions #3

Updated by DBA (Diogo Almeida) almost 14 years ago

=begin
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
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0