# Date.to\_time error

**URL:** <https://rubytalk.org/t/date-to-time-error/19644>\
**Category:** ruby-talk\
**Created:** [20 July 2005 11:29 UTC](https://rubytalk.org/t/date-to-time-error/19644 "2005-07-20T11:29:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kev\_Jackson2](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Kev\_Jackson2](https://rubytalk.org/u/Kev_Jackson2)\
**Post date:** [20 July 2005 11:29 UTC](https://rubytalk.org/t/date-to-time-error/19644/1 "2005-07-20T11:29:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kev\_Jackson2](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Kev\_Jackson2](https://rubytalk.org/u/Kev_Jackson2)\
**Post date:** [20 July 2005 11:34 UTC](https://rubytalk.org/t/date-to-time-error/19644/2 "2005-07-20T11:34:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Charles\_Steinman](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@Charles\_Steinman](https://rubytalk.org/u/Charles_Steinman)\
**Post date:** [20 July 2005 21:45 UTC](https://rubytalk.org/t/date-to-time-error/19644/3 "2005-07-20T21:45:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dimitri\_Aivaliotis](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Dimitri\_Aivaliotis](https://rubytalk.org/u/Dimitri_Aivaliotis)\
**Post date:** [20 July 2005 14:46 UTC](https://rubytalk.org/t/date-to-time-error/19644/4 "2005-07-20T14:46:42Z")

</div>

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](http://www.rubycentral.com/ref/ref_c_time.html) for further  
> details.
> 
> - Dimitri

---

<div class="post-metadata">

**Author:** ![Kev\_Jackson2](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Kev\_Jackson2](https://rubytalk.org/u/Kev_Jackson2)\
**Post date:** [21 July 2005 02:24 UTC](https://rubytalk.org/t/date-to-time-error/19644/5 "2005-07-21T02:24:47Z")

</div>

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)  
> > &nbsp;&nbsp;&nbsp;  
> > 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

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [21 July 2005 15:44 UTC](https://rubytalk.org/t/date-to-time-error/19644/6 "2005-07-21T15:44:50Z")

</div>

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](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [21 July 2005 02:47 UTC](https://rubytalk.org/t/date-to-time-error/19644/7 "2005-07-21T02:47:31Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [21 July 2005 09:02 UTC](https://rubytalk.org/t/date-to-time-error/19644/8 "2005-07-21T09:02:27Z")

</div>

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](http://www.rubycentral.com/ref/ref_c_time.html) for further  
> > details.

---

<div class="post-metadata">

**Author:** ![Kev\_Jackson2](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Kev\_Jackson2](https://rubytalk.org/u/Kev_Jackson2)\
**Post date:** [22 July 2005 02:29 UTC](https://rubytalk.org/t/date-to-time-error/19644/9 "2005-07-22T02:29:14Z")

</div>

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.  
> > &nbsp;&nbsp;&nbsp;  
> > ... 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

---

<div class="post-metadata">

**Author:** ![Dimitri\_Aivaliotis](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Dimitri\_Aivaliotis](https://rubytalk.org/u/Dimitri_Aivaliotis)\
**Post date:** [21 July 2005 11:57 UTC](https://rubytalk.org/t/date-to-time-error/19644/10 "2005-07-21T11:57:35Z")

</div>

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  
&nbsp;&nbsp;def to\_time(form = :local)  
&nbsp;&nbsp;&nbsp;&nbsp;::Time.send(form, year, month, day)  
&nbsp;&nbsp;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
