date of 1 year ago

Z

zuerrong

Hello,

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

date -d '1 year ago'

Thanks.
 
S

Stu

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
 
S

Stu

sorry try this one

class Integer

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

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

def ago
return( Time.now - self)
end

end
 
O

OZAWA Sakuro

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

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]
 
Z

zuerrong

2010/11/25 Stu said:
=C2=A0def years
=C2=A0 =C2=A0year =3D 365


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

Klaus Stein

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

date -d '1 year ago'
Lets use the Date class:

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

puts onyearago.asctime

Have fun!

Klaus
 
S

Stu

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 =3D)
 
R

rlf

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 =)

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.
 
R

Reid Thompson

Hello,

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

date -d '1 year ago'

Thanks.
google ruby + chronic

sudo gem install chroni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top