Date.to_time error

K

Kev Jackson

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
 
K

Kev Jackson

Kev said:
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
 
C

Charles Steinman

Kev said:
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.
 
K

Kev Jackson

Charles said:
Kev Jackson wrote:



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 :(

Kev
 
J

James Edward Gray II

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 :(

Tiger also ships with Ruby 1.8.2 installed, just FYI.

James Edward Gray II
 
B

Brian Candler

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

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.
 
D

Dimitri Aivaliotis

But what's wrong with negative time values?
=20
Unix supports this AFAIK. This with FreeBSD:
=20
$ date -r -1000000
Sat Dec 20 11:13:20 BST 1969
=20
And the big time wraparound occurs at t =3D 2^31, not t =3D 2^32:
=20
$ date -r 2147483647
Tue Jan 19 03:14:07 GMT 2038
=20

Hmm...never thought of that.

Time.at(-2147483648)
=3D> 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 =3D :local)
::Time.send(form, year, month, day)
end
end

It also seems to work for me:

start_date =3D Date.new(1901, 01, 01)
start_date.to_time
=3D> 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
 
K

Kev Jackson

Christian said:
... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top