[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to represent Date before epoch as a integer

Yaxm Yaxm

4/30/2007 12:35:00 AM

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(http://en.wikipedia.org/wiki/... 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

--
Posted via http://www.ruby-....

6 Answers

Robert Dober

4/30/2007 8:26:00 AM

0

On 4/30/07, Yaxm Yaxm <snowstorm+rubyforum@gmail.com> wrote:
> 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
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

>
> # 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'

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(http://en.wikipedia.org/wiki/... 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
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Nobuyoshi Nakada

4/30/2007 8:42:00 AM

0

Hi,

At Mon, 30 Apr 2007 09:35:06 +0900,
Yaxm Yaxm 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

--
Nobu Nakada

Yaxm Yaxm

5/1/2007 2:59:00 AM

0

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 Dober wrote:
> On 4/30/07, Yaxm Yaxm <snowstorm+rubyforum@gmail.com> 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(http://en.wikipedia.org/wiki/... 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


--
Posted via http://www.ruby-....

Yaxm Yaxm

5/1/2007 3:02:00 AM

0

yeah. using Julian Day number works.

But there's no years_ago method in date class.

guess I need to write one.

Thanks.

Nobuyoshi Nakada wrote:
> Hi,
>
> At Mon, 30 Apr 2007 09:35:06 +0900,
> Yaxm Yaxm 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


--
Posted via http://www.ruby-....

Nobuyoshi Nakada

5/1/2007 3:54:00 AM

0

Hi,

At Tue, 1 May 2007 12:01:51 +0900,
Yaxm Yaxm 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

--
Nobu Nakada

Robert Dober

5/1/2007 7:15:00 AM

0

On 5/1/07, Yaxm Yaxm <snowstorm+rubyforum@gmail.com> 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
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw