Date.to_time error

Hi,

I'm messing with some dates and (being new to ruby), I've come across an interesting problem

I have a new date object

start_date = Date.new 1901, 01, 01

now I can call

start_date.respond_to?(:to_time)

and everything is fine, and I get true back

assert_equal true, start_date.respond_to?(:to_time)

however

time = start_date.to_time

breaks with an Argument out of range error

This is on the version of Ruby on my WinXP machine here at work, (one click installer - cheers for that, so much easier than the hell that is installing on MacOSX)

Any ideas what I should be doing? Looking at the API docs, a Date object shouldn't respond to to_time - indeed I can't find the to_time method - but maybe I'm looking in the wrong place

Sorry if this is a ridiculous message, but I'm kinda interested in how I should approach this - it's someone elses code and I don't want to use a Time object instead of the original date, I think it's a bug in the code (that I found - w00t!), but I want to be sure before I post bug reports etc etc

Kev

Kev Jackson wrote:

Hi,

I'm messing with some dates and (being new to ruby), I've come across an interesting problem

I have a new date object

start_date = Date.new 1901, 01, 01

Interestingly it seems to be a real bug in the code (yes respond to myself but whatever).

I've now discovered that any date prior to 1970 breaks when to_time is called on it.

Nice, so I have to update the code (just unit tests) to use at a minimum 1970 for this test case

Kev

Kev Jackson wrote:

This is on the version of Ruby on my WinXP machine here at work, (one
click installer - cheers for that, so much easier than the hell that is
installing on MacOSX)

What do you mean? Ruby is included in the Mac OS X standard install.
Can't get any easier.

Hi Kev,

···

On 7/20/05, Kev Jackson <kevin.jackson@it.fts-vn.com> wrote:

I've now discovered that any date prior to 1970 breaks when to_time is
called on it.

That would make sense, since Time represents time since the Epoch.
See http://www.rubycentral.com/ref/ref_c_time.html for further
details.

- Dimitri

Charles Steinman wrote:

Kev Jackson wrote:

This is on the version of Ruby on my WinXP machine here at work, (one
click installer - cheers for that, so much easier than the hell that is
installing on MacOSX)
   
What do you mean? Ruby is included in the Mac OS X standard install.
Can't get any easier.

ruby 1.6 is installed as standard, installing ruby 1.8 over a modem so that I can download rails and play with that is not as easy as I'd like. There is an installer now, but it's for Tiger and I'm stuck on Panther :frowning:

Kev

Kev Jackson <kevin.jackson@it.fts-vn.com> writes:

Kev Jackson wrote:

I've now discovered that any date prior to 1970 breaks when to_time is
called on it.

... on win32. Complain to your vendor for having an unsigned time format.

···

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Tiger also ships with Ruby 1.8.2 installed, just FYI.

James Edward Gray II

···

On Jul 20, 2005, at 9:24 PM, Kev Jackson wrote:

ruby 1.6 is installed as standard, installing ruby 1.8 over a modem so that I can download rails and play with that is not as easy as I'd like. There is an installer now, but it's for Tiger and I'm stuck on Panther :frowning:

But what's wrong with negative time values?

Unix supports this AFAIK. This with FreeBSD:

$ date -r -1000000
Sat Dec 20 11:13:20 BST 1969

And the big time wraparound occurs at t = 2^31, not t = 2^32:

$ date -r 2147483647
Tue Jan 19 03:14:07 GMT 2038

Regards,

Brian.

···

On Wed, Jul 20, 2005 at 11:46:42PM +0900, Dimitri Aivaliotis wrote:

On 7/20/05, Kev Jackson <kevin.jackson@it.fts-vn.com> wrote:

> I've now discovered that any date prior to 1970 breaks when to_time is
> called on it.
>

That would make sense, since Time represents time since the Epoch.
See http://www.rubycentral.com/ref/ref_c_time.html for further
details.

Christian Neukirchen wrote:

Kev Jackson <kevin.jackson@it.fts-vn.com> writes:

Kev Jackson wrote:

I've now discovered that any date prior to 1970 breaks when to_time is
called on it.
   
... on win32. Complain to your vendor for having an unsigned time format.

yeah because complaining to Microsoft about their crappy software has always been a winning strategy! I have to use Windows at work, it suprised me that the 1970 date would be significant to Windows in anyway. Presumambly this is something to do with the half-arsed not-quite posix that Windows attempts to be.

Kev

Hmm...never thought of that.

Time.at(-2147483648)
=> Fri Dec 13 21:45:52 CET 1901

It does work...

On my machine (Linux, Ruby 1.8.2), the to_time method isn't in the
standard library: it's a part of facets (I've got version 0.7.2), and
it's defined as follows:

class Date
  def to_time(form = :local)
    ::Time.send(form, year, month, day)
  end
end

It also seems to work for me:

start_date = Date.new(1901, 01, 01)
start_date.to_time
=> Fri Dec 13 21:45:52 CET 1901

Maybe the OP could post a few more details about his set-up. Which
version of the one-click installer is it? Or maybe it's a platform
problem (WinXP not supporting negative times)?

- Dimitri

···

On 7/21/05, Brian Candler <B.Candler@pobox.com> wrote:

But what's wrong with negative time values?

Unix supports this AFAIK. This with FreeBSD:

$ date -r -1000000
Sat Dec 20 11:13:20 BST 1969

And the big time wraparound occurs at t = 2^31, not t = 2^32:

$ date -r 2147483647
Tue Jan 19 03:14:07 GMT 2038