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

Also available in: Atom PDF

Like0
Like0Like0Like0