date of 1 year ago

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

  1. zuerrong

    zuerrong Guest

    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
    #1
    1. Advertising

  2. zuerrong

    Stu Guest

    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
    #2
    1. Advertising

  3. zuerrong

    zuerrong Guest

    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
    #3
  4. zuerrong

    Stu Guest

    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
    #4
  5. zuerrong

    OZAWA Sakuro Guest

    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
    #5
  6. zuerrong

    Stu Guest

    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
    #6
  7. zuerrong

    zuerrong Guest

    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
    #7
  8. zuerrong

    Klaus Stein Guest

    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
    #8
  9. zuerrong

    Stu Guest

    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
    #9
  10. zuerrong

    rlf Guest

    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
    #10
  11. 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
     
    Reid Thompson, Nov 26, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Otuatail

    Week of year to full Year

    Otuatail, Dec 8, 2003, in forum: HTML
    Replies:
    4
    Views:
    987
    Toby A Inkster
    Dec 8, 2003
  2. gabor
    Replies:
    4
    Views:
    632
    Christos Georgiou
    Apr 12, 2006
  3. Veerle
    Replies:
    2
    Views:
    429
    niceguy
    Aug 20, 2003
  4. David Woodward
    Replies:
    5
    Views:
    443
    Dr John Stockton
    Feb 2, 2004
  5. joe shaboo

    calculate date 4 days ago

    joe shaboo, May 4, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    324
    Jim Cochrane
    May 5, 2004
Loading...

Share This Page