Date of 1 year ago

Hello,

How does ruby get the date of 1 year ago?
like the result of with shell command:

date -d '1 year ago'

Thanks.

nice. never knew you could do that with the shell command date.

Here is a simple metaprogramming example

class Integer

  def days
    sec = min = 60
    day = 24
    return( self * ( day * (sec * min)))
  end

  def years
    return( 365.days)
  end

  def ago
    return( Time.now - self)
  end

end

···

On Wed, Nov 24, 2010 at 8:49 PM, zuerrong <zuerrong@gmail.com> wrote:

Hello,

How does ruby get the date of 1 year ago?
like the result of with shell command:

date -d '1 year ago'

Thanks.

Lets use the Date class:

d = Date.today
onyearago = Date.civil(d.year-1, d.month, d.day)

puts onyearago.asctime

Have fun!

Klaus

···

zuerrong <zuerrong@gmail.com> wrote:

How does ruby get the date of 1 year ago?
like the result of with shell command:

date -d '1 year ago'

--

The Answer is 42. And I am the Answer. Now I am looking for the Question.

google ruby + chronic

sudo gem install chroni

···

On 11/24/2010 9:49 PM, zuerrong wrote:

Hello,

How does ruby get the date of 1 year ago?
like the result of with shell command:

date -d '1 year ago'

Thanks.

does every year have the exact days of 365? no, IMO.

···

2010/11/25 Stu <stu@rubyprogrammer.net>:

nice. never knew you could do that with the shell command date.

Here is a simple metaprogramming example

class Integer

def days
sec = min = 60
day = 24
return( self * ( day * (sec * min)))
end

def years
return( 365.days)
end

def ago
return( Time.now - self)
end

end

On Wed, Nov 24, 2010 at 8:49 PM, zuerrong <zuerrong@gmail.com> wrote:

Hello,

How does ruby get the date of 1 year ago?
like the result of with shell command:

date -d '1 year ago'

Thanks.

Remember it is one of GNU date's extension and is not portable.
http://www.opengroup.org/onlinepubs/009695399/utilities/date.html

$ uname -a
Darwin pisces.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15
16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 i386 iMac9,1
Darwin

$ /opt/local/bin/gdate -d '1 year ago'
Wed Nov 25 12:35:00 JST 2009

$ /bin/date -d '1 year ago'
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

···

On Thu, Nov 25, 2010 at 12:14, Stu <stu@rubyprogrammer.net> wrote:

nice. never knew you could do that with the shell command date.

--
OZAWA Sakuro

"I think we can agree, the past is over." - George W. Bush

sorry try this one

class Integer

  def days
    sec = min = 60
    day = 24
    return( self * ( day * (sec * min)))
  end

  def years
    year = 365
    return( year * self.days)
  end

  def ago
    return( Time.now - self)
  end

end

···

On Wed, Nov 24, 2010 at 9:18 PM, zuerrong <zuerrong@gmail.com> wrote:

does every year have the exact days of 365? no, IMO.

2010/11/25 Stu <stu@rubyprogrammer.net>:

nice. never knew you could do that with the shell command date.

Here is a simple metaprogramming example

class Integer

def days
sec = min = 60
day = 24
return( self * ( day * (sec * min)))
end

def years
return( 365.days)
end

def ago
return( Time.now - self)
end

end

On Wed, Nov 24, 2010 at 8:49 PM, zuerrong <zuerrong@gmail.com> wrote:

Hello,

How does ruby get the date of 1 year ago?
like the result of with shell command:

date -d '1 year ago'

Thanks.

Remember it is one of GNU date's extension and is not portable.
date

Thank you for the heads up. I'm on FreeBSD which should be the same
`date` as Darwin. I'm gonna look at my funtoo box later and read the
man page.

···

On Wed, Nov 24, 2010 at 9:36 PM, OZAWA Sakuro <sakuro@2238club.org> wrote:

On Thu, Nov 25, 2010 at 12:14, Stu <stu@rubyprogrammer.net> wrote:

On Wed, Nov 24, 2010 at 9:18 PM, zuerrong <zuerrong@gmail.com> wrote:

does every year have the exact days of 365? no, IMO.

Date has leap years if your looking for that. Time can do epoch,utc etc.

Well, how do you know every year has exactly 365 days?
I don't think this is a standard method for date calc.

···

2010/11/25 Stu <stu@rubyprogrammer.net>:

def years
year = 365

I was just trying to get you to closer to the syntax of your gnu date
program. As I mentioned Date has it's own leap year boolean object. Of
course you can always program your own leap year algorithm with a
couple modulo operations. This way year can return the extra day you
get every four years.

Good luck and happy hacking =)

···

On Wed, Nov 24, 2010 at 10:05 PM, zuerrong <zuerrong@gmail.com> wrote:

2010/11/25 Stu <stu@rubyprogrammer.net>:

def years
year = 365

Well, how do you know every year has exactly 365 days?
I don't think this is a standard method for date calc.

Besides overriding the plus and minus operators to deal with days, the
Date class also employs the shift operators to deal with months. So
try:

Date.today << 12

It deals with the leap year issue as well.

···

On Nov 25, 3:11 am, Stu <s...@rubyprogrammer.net> wrote:

On Wed, Nov 24, 2010 at 10:05 PM, zuerrong <zuerr...@gmail.com> wrote:
> 2010/11/25 Stu <s...@rubyprogrammer.net>:

>> def years
>> year = 365

> Well, how do you know every year has exactly 365 days?
> I don't think this is a standard method for date calc.

I was just trying to get you to closer to the syntax of your gnu date
program. As I mentioned Date has it's own leap year boolean object. Of
course you can always program your own leap year algorithm with a
couple modulo operations. This way year can return the extra day you
get every four years.

Good luck and happy hacking =)