From: Yaxm Yaxm Date: 2007-05-01T11:59:17+09:00 Subject: Re: how to represent Date before epoch as a integer Rober, converting the date object to be a time can cause the exception: >> birth_date(50) => # >> 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 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 > => # > 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/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 -- Posted via http://www.ruby-forum.com/.