Date.new.to_time.to_i

What’s going on here!?

Date.new.to_time.to_i
NoMethodError: undefined method `to_i’ for Mon, 01 Jan -4712 00:00:00
-0800:DateTime
from (irb):25

Date.today.to_time.to_i
=> 1245308400

RUBY_VERSION
=> “1.8.7”

Rails.version
=> “2.1.1”

(I’m not sure if this is a rails thing, or just a ruby thing)

If you look at the classes returned, Date.new.to_time returns a
DateTime,
while Date.today.to_time returns a Time

Date.new.to_time.class --> DateTime
Date.today.to_time.class --> Time

Not sure why that is the case, but obviously DateTime doesn’t have a
to_i
method.

Simon

On Fri, 19 Jun 2009 07:37:21 +0800, Michael Economy

On Jun 19, 12:45 am, “Simon M.” [email protected] wrote:

Not sure why that is the case, but obviously DateTime doesn’t have a to_i
method.

Date::new() returns Julian day 0, which is Mon, 01 Jan -4712

to_time() is a CoreExtensions method which uses Time::utc_time() →
Time::time_with_datetime_fallback() from CoreExtensions to generate
the Time object. From the documentation:

Returns a new Time if requested year can be accommodated by Ruby‘s
Time class (i.e., if year is within either 1970…2038 or 1902…2038,
depending on system architecture); otherwise returns a DateTime

Because the year is out of range, you’ll get a DateTime back, which
you can’t call to_i on.

This is actually quite silly, when you think about it.

-Matt

Ok, now it makes sense. :slight_smile:

(I so hope i can retire before 2038)