# How to represent Date before epoch as a integer

**URL:** <https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251>\
**Category:** Ruby\
**Created:** [April 30, 2007, 2:35am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251 "2007-04-30T02:35:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaxm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/54ee81/32.png) [@yaxm](https://www.ruby-forum.com/u/yaxm)\
**Post date:** [April 30, 2007, 2:35am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251/1 "2007-04-30T02:35:04Z")

</div>

Hi,

I provide a people search function that a user can specify a age range.  
I use acts\_as\_ferret for the search.

I’d like to represent a user’s birth date as a integer number, so that I  
can tell Ferret to do the comparision easily.

#calculate birth date based on the given age  
def self.birth\_date(age)  
born = Date.today  
Date.new(born.year - age, born.month, born.day)  
end

# get the birth date of the user that’s 50 years old

User.birth\_date(50).to\_time

But I got this error:  
ArgumentError: argument out of range  
from  
C:/InstantRails-1.6-win/ruby/lib/ruby/gems/1.8/gems/activesupport-1  
.4.1/lib/active\_support/core\_ext/date/conversions.rb:29:in `local' from C:/InstantRails-1.6-win/ruby/lib/ruby/gems/1.8/gems/activesupport-1 .4.1/lib/active_support/core_ext/date/conversions.rb:29:in `send’  
from  
C:/InstantRails-1.6-win/ruby/lib/ruby/gems/1.8/gems/activesupport-1  
.4.1/lib/active\_support/core\_ext/date/conversions.rb:29:in `to\_time’

I think the time can’t be before unix epoch  
time([Unix time - Wikipedia](http://en.wikipedia.org/wiki/Unix_epoch) it is the number of seconds  
elapsed since midnight UTC of January 1, 1970, not counting leap  
seconds. )

Please advice how I can get around this limitation.

Thanks.  
Yaxm

---

<div class="post-metadata">

**Author:** ![yaxm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/54ee81/32.png) [@yaxm](https://www.ruby-forum.com/u/yaxm)\
**Post date:** [April 30, 2007, 10:26am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251/2 "2007-04-30T10:26:16Z")

</div>

On 4/30/07, Yaxm Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def self.birth\_date(age)  
> born = Date.today  
> Date.new(born.year - age, born.month, born.day)  
> end  
> Strange this works perfectly on my box  
> irb(main):027:0\> def birth\_date(age)  
> irb(main):028:1\> born = Date.today  
> irb(main):029:1\> Date.new( born.year - age, born.month, born.day )  
> irb(main):030:1\> end  
> =\> nil  
> irb(main):031:0\> birth\_date 50  
> =\> #\<Date: 4871917/2,0,2299161\>  
> irb(main):032:0\> birth\_date( 50 ).year  
> =\> 1957

_However_ this is code that does not work for all values on Feb 29th,  
be careful about this

irb(main):033:0\> Date.new( 2007, 2, 29 )  
ArgumentError: invalid date  
from /usr/local/lib/ruby/1.8/date.rb:727:in `new’  
from (irb):33

> C:/InstantRails-1.6-win/ruby/lib/ruby/gems/1.8/gems/activesupport-1  
> .4.1/lib/active\_support/core\_ext/date/conversions.rb:29:in `send' from C:/InstantRails-1.6-win/ruby/lib/ruby/gems/1.8/gems/activesupport-1 .4.1/lib/active_support/core_ext/date/conversions.rb:29:in `to\_time’

Form my expample and the stacktrace one can conclude that this is an  
ActiveSupport issue. If I am not mistaken they have a method like  
#years\_ago  
somewhere (probably adressing the Feb, 29 issue), sorry never used that  
stuff.

> I think the time can’t be before unix epoch  
> time([Unix time - Wikipedia](http://en.wikipedia.org/wiki/Unix_epoch) it is the number of seconds  
> elapsed since midnight UTC of January 1, 1970, not counting leap  
> seconds. )  
> Hopefully you can, sure somebody knowing ActiveSupport will tell us  
> about it

Robert

---

<div class="post-metadata">

**Author:** ![yaxm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/54ee81/32.png) [@yaxm](https://www.ruby-forum.com/u/yaxm)\
**Post date:** [April 30, 2007, 10:43am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251/3 "2007-04-30T10:43:18Z")

</div>

Hi,

At Mon, 30 Apr 2007 09:35:06 +0900,  
Yaxm Y. wrote in [ruby-talk:249588]:

> I’d like to represent a user’s birth date as a integer number, so that I  
> can tell Ferret to do the comparision easily.

Date supports Julian Day Number.

Date.today.jd #=\> 2454221

---

<div class="post-metadata">

**Author:** ![yaxm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/54ee81/32.png) [@yaxm](https://www.ruby-forum.com/u/yaxm)\
**Post date:** [May 1, 2007, 5:01am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251/4 "2007-05-01T05:01:50Z")

</div>

yeah. using Julian Day number works.

But there’s no years\_ago method in date class.

guess I need to write one.

Thanks.

Nobuyoshi N. wrote:

> Hi,
> 
> At Mon, 30 Apr 2007 09:35:06 +0900,  
> Yaxm Y. wrote in [ruby-talk:249588]:
> 
> > I’d like to represent a user’s birth date as a integer number, so that I  
> > can tell Ferret to do the comparision easily.
> 
> Date supports Julian Day Number.
> 
> Date.today.jd #=\> 2454221

---

<div class="post-metadata">

**Author:** ![yaxm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/54ee81/32.png) [@yaxm](https://www.ruby-forum.com/u/yaxm)\
**Post date:** [May 1, 2007, 4:59am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251/5 "2007-05-01T04:59:15Z")

</div>

Rober,  
converting the date object to be a time can cause the exception:

> > birth\_date(50)  
> > =\> #\<Date: 4871917/2,0,2299161\>  
> > birth\_date(50).to\_time  
> > ArgumentError: argument out of range

Time.new().years\_ago(50) causes the same exception

Robert D. wrote:

> On 4/30/07, Yaxm Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > def self.birth\_date(age)  
> > born = Date.today  
> > Date.new(born.year - age, born.month, born.day)  
> > end  
> > Strange this works perfectly on my box  
> > irb(main):027:0\> def birth\_date(age)  
> > irb(main):028:1\> born = Date.today  
> > irb(main):029:1\> Date.new( born.year - age, born.month, born.day )  
> > irb(main):030:1\> end  
> > =\> nil  
> > irb(main):031:0\> birth\_date 50  
> > =\> #\<Date: 4871917/2,0,2299161\>  
> > irb(main):032:0\> birth\_date( 50 ).year  
> > =\> 1957
> 
> _However_ this is code that does not work for all values on Feb 29th,  
> be careful about this
> 
> irb(main):033:0\> Date.new( 2007, 2, 29 )  
> ArgumentError: invalid date  
> from /usr/local/lib/ruby/1.8/date.rb:727:in `new’  
> from (irb):33
> 
> > C:/InstantRails-1.6-win/ruby/lib/ruby/gems/1.8/gems/activesupport-1  
> > .4.1/lib/active\_support/core\_ext/date/conversions.rb:29:in `send' from C:/InstantRails-1.6-win/ruby/lib/ruby/gems/1.8/gems/activesupport-1 .4.1/lib/active_support/core_ext/date/conversions.rb:29:in `to\_time’
> 
> Form my expample and the stacktrace one can conclude that this is an  
> ActiveSupport issue. If I am not mistaken they have a method like  
> #years\_ago  
> somewhere (probably adressing the Feb, 29 issue), sorry never used that  
> stuff.
> 
> > I think the time can’t be before unix epoch  
> > time([Unix time - Wikipedia](http://en.wikipedia.org/wiki/Unix_epoch) it is the number of seconds  
> > elapsed since midnight UTC of January 1, 1970, not counting leap  
> > seconds. )  
> > Hopefully you can, sure somebody knowing ActiveSupport will tell us  
> > about it
> 
> Robert

---

<div class="post-metadata">

**Author:** ![yaxm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/54ee81/32.png) [@yaxm](https://www.ruby-forum.com/u/yaxm)\
**Post date:** [May 1, 2007, 9:15am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251/6 "2007-05-01T09:15:34Z")

</div>

On 5/1/07, Yaxm Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rober,  
> converting the date object to be a time can cause the exception:
> 
> > > birth\_date(50)  
> > > =\> #\<Date: 4871917/2,0,2299161\>  
> > > birth\_date(50).to\_time  
> > > ArgumentError: argument out of range
> 
> Time.new().years\_ago(50) causes the same exception

Why do you use Time? Does Date not do the job?  
#years\_ago is a method of the ActiveRecord extension of Date.  
I guess we might not be talking about the same thing, maybe if you  
post more code?  
But look at Nobu’s suggestion, seems to work just fine.

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![yaxm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/54ee81/32.png) [@yaxm](https://www.ruby-forum.com/u/yaxm)\
**Post date:** [May 1, 2007, 5:54am UTC](https://www.ruby-forum.com/t/how-to-represent-date-before-epoch-as-a-integer/93251/7 "2007-05-01T05:54:24Z")

</div>

Hi,

At Tue, 1 May 2007 12:01:51 +0900,  
Yaxm Y. wrote in [ruby-talk:249770]:

> yeah. using Julian Day number works.
> 
> But there’s no years\_ago method in date class.

$ ruby -rdate -e ‘puts Date.today \<\< 50 \* 12’  
1957-05-01
