# date of 1 year ago

Discussion in 'Ruby' started by zuerrong, Nov 25, 2010.

1. ### zuerrongGuest

Hello,

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

date -d '1 year ago'

Thanks.

zuerrong, Nov 25, 2010

2. ### StuGuest

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

Stu, Nov 25, 2010

3. ### zuerrongGuest

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

2010/11/25 Stu <>:
> nice. never knew you could do that with the shell command date.
>
> Here is a simple metaprogramming example
>
> class Integer
>
> =C2=A0def days
> =C2=A0 =C2=A0sec =3D min =3D 60
> =C2=A0 =C2=A0day =3D =C2=A024
> =C2=A0 =C2=A0return( self * ( day * (sec * min)))
> =C2=A0end
>
> =C2=A0def years
> =C2=A0 =C2=A0return( 365.days)
> =C2=A0end
>
> =C2=A0def ago
> =C2=A0 =C2=A0return( Time.now - self)
> =C2=A0end
>
> end
>
> On Wed, Nov 24, 2010 at 8: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.
>>
>>

>
>

zuerrong, Nov 25, 2010
4. ### StuGuest

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

On Wed, Nov 24, 2010 at 9:18 PM, zuerrong <> wrote:
> does every year have the exact days of 365? no, IMO.
>
> 2010/11/25 Stu <>:
>> nice. never knew you could do that with the shell command date.
>>
>> Here is a simple metaprogramming example
>>
>> class Integer
>>
>> =A0def days
>> =A0 =A0sec =3D min =3D 60
>> =A0 =A0day =3D =A024
>> =A0 =A0return( self * ( day * (sec * min)))
>> =A0end
>>
>> =A0def years
>> =A0 =A0return( 365.days)
>> =A0end
>>
>> =A0def ago
>> =A0 =A0return( Time.now - self)
>> =A0end
>>
>> end
>>
>> On Wed, Nov 24, 2010 at 8: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.
>>>
>>>

>>
>>

>
>

Stu, Nov 25, 2010
5. ### OZAWA SakuroGuest

On Thu, Nov 25, 2010 at 12:14, Stu <> wrote:

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

--
OZAWA Sakuro

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

OZAWA Sakuro, Nov 25, 2010
6. ### StuGuest

On Wed, Nov 24, 2010 at 9:36 PM, OZAWA Sakuro <> wrote:
> On Thu, Nov 25, 2010 at 12:14, Stu <> wrote:
> Remember it is one of GNU date's extension and is not portable.
> http://www.opengroup.org/onlinepubs/009695399/utilities/date.html

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:18 PM, zuerrong <> 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.

Stu, Nov 25, 2010
7. ### zuerrongGuest

2010/11/25 Stu <>:

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

zuerrong, Nov 25, 2010
8. ### Klaus SteinGuest

zuerrong <> wrote:
>
> 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

--
http://lapiz.istik.de/

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

Klaus Stein, Nov 25, 2010
9. ### StuGuest

On Wed, Nov 24, 2010 at 10:05 PM, zuerrong <> wrote:
> 2010/11/25 Stu <>:
>
>>
>> =A0def years
>> =A0 =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.
>
>

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)

Stu, Nov 25, 2010
10. ### rlfGuest

On Nov 25, 3:11 am, Stu <> wrote:
> On Wed, Nov 24, 2010 at 10:05 PM, zuerrong <> wrote:
> > 2010/11/25 Stu <>:

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

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.

rlf, Nov 25, 2010
11. ### Reid ThompsonGuest

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