DateTime#to_time doesn't work as I expect

require ‘date’

utc_time = Time.now.utc
p utc_time #=>2014-07-30 19:23:33 UTC

dt = DateTime.parse(utc_time.to_s)
p dt
#=> #<DateTime: 2014-07-30T19:23:33+00:00
((2456869j,69813s,0n),+0s,2299161j)>

ny = dt.new_offset(‘-05:00’)
p ny
#<DateTime: 2014-07-30T14:23:33-05:00
((2456869j,69813s,0n),-18000s,2299161j)>
#Okay, so far, so good, now…

puts ny.to_time #=>2014-07-30 13:23:33 -1100
#???

p Time.now #=>2014-07-30 13:23:33 -1100

The results show that to_time() is using my local offset, yet the docs
say this about to_time:

Datetime#to_time
Returns a Time object which denotes self.

In my opinion, the Time object does not denote self at all–self has an
offset of -0500.

Additional observations:

  1. new_offset() is not even included in the list of methods on the left
    of the page here:
  1. The Time class fails to mention that there is a Ruby STL package
    named ‘time’ that adds additional methods to the Time class.

No, there was a heated discussion about some DateTime feature that
needed patching awhile ago, and I thought maybe those in the know might
explain the reason for the results I’m seeing.

I’d really like to hear at least one opinion why would DateTime#to_time
clear offset and do localtime conversion. If nobody throws-in any
counter-argument in a week I may fill the bugreport myself.

I think you’ve just found a bug.

In the implementation of DateTime#to_time there are some unfathomable
operations:

  1. DateTime’s offset value is deliberately reset to 0

  2. resulted Time object is converted to a local time

            static VALUE
    

datetime_to_time(VALUE self)
{
volatile VALUE dup = dup_obj_with_new_offset(self, 0);
// ^^^^^^ ^^^ ?!
{
VALUE t;

    get_d1(dup);

    t = f_utc6(rb_cTime,
               m_real_year(dat),
               INT2FIX(m_mon(dat)),
               INT2FIX(m_mday(dat)),
               INT2FIX(m_hour(dat)),
               INT2FIX(m_min(dat)),
               f_add(INT2FIX(m_sec(dat)),
                     m_sf_in_sec(dat)));
    return f_getlocal(t);
    //     ^^^^^^^^^^  ?!
}

}

I see no logical reason DateTime#to_time won’t return corresponding
time.

Are you going to fill a bug report ?

Take care

David

David U. wrote in post #1153968:

I’d really like to hear at least one opinion why would DateTime#to_time
clear offset and do localtime conversion. If nobody throws-in any
counter-argument in a week I may fill the bugreport myself.

Okay, that will at least generate an explanation. From that previous
discussion, I gather that changes are not accepted too readily.

I didn’t say it was related to this issue; it was some other issue
related to Date, Time, DateTime handling in ruby:

https://www.ruby-forum.com/topic/4570470

Nope. I don’t find mentioned 60+ posts long discussion anyhow related to
this issue. I’ve quite an opposite experience with ruby core devs, esp.
Sasada (ko1) and Nakada. Obliging and fast with bugs fixing.