Hello,
How does ruby get the date of 1 year ago?
like the result of with shell command:
date -d ‘1 year ago’
Thanks.
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
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:36 PM, OZAWA Sakuro [email protected]
wrote:
On Thu, Nov 25, 2010 at 12:14, Stu [email protected] wrote:
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:18 PM, zuerrong [email protected] 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.
On Thu, Nov 25, 2010 at 12:14, Stu [email protected] 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
zuerrong [email protected] 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
–
The Answer is 42. And I am the Answer. Now I am looking for the
Question.
On Wed, Nov 24, 2010 at 10:05 PM, zuerrong [email protected] wrote:
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 =)
2010/11/25 Stu [email protected]:
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.
On Nov 25, 3:11am, Stu [email protected] wrote:
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.
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.
google ruby + chronic
sudo gem install chroni
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs