getting a timestamp

Discussion in 'Ruby' started by jp, Nov 19, 2009.

  1. jp

    jp Guest

    Hi,

    I would like to get a timestamp from a Date object. What is the best
    aproach for this?

    thank you
    jp, Nov 19, 2009
    #1
    1. Advertising

  2. jp wrote:
    > Hi,
    >
    > I would like to get a timestamp from a Date object. What is the best
    > aproach for this?
    >
    > thank you
    >

    I think you want a DateTime or Time object, I believe a Date object only represents a date, not a date and
    time unless perhaps 'date at midnight' counts.
    Reid Thompson, Nov 19, 2009
    #2
    1. Advertising

  3. jp

    Mike Stok Guest

    On Nov 18, 2009, at 10:10 PM, jp wrote:

    > Hi,
    >
    > I would like to get a timestamp from a Date object. What is the best
    > aproach for this?
    >
    > thank you


    I'm not sure if this is the most effective, but

    d = Date.new(2009, 11, 18)
    ts = d.strftime('%s').to_i

    seems to work for me, giving the timestamp for 2009-11-10 00:00:00 GMT

    Hope this helps,

    Mike

    --

    Mike Stok <>
    http://www.stok.ca/~mike/

    The "`Stok' disclaimers" apply.
    Mike Stok, Nov 19, 2009
    #3
  4. jp

    Sven Schott Guest

    [Note: parts of this message were removed to make it a legal post.]

    Don't know if this is what you're looking for but whenever I need a
    serial-style timestamp (like for a filename). I just do this:

    Time.now.strftime("%Y%m%d%H%M%S")

    On Thu, Nov 19, 2009 at 2:19 PM, Reid Thompson <>wrote:

    > jp wrote:
    >
    >> Hi,
    >>
    >> I would like to get a timestamp from a Date object. What is the best
    >> aproach for this?
    >>
    >> thank you
    >>
    >> I think you want a DateTime or Time object, I believe a Date object only

    > represents a date, not a date and time unless perhaps 'date at midnight'
    > counts.
    >
    >
    >
    >
    Sven Schott, Nov 19, 2009
    #4
  5. jp

    jp Guest

    On 19 Nov, 03:31, Sven Schott <> wrote:
    > [Note:  parts of this message were removed to make it a legal post.]
    >
    > Don't know if this is what you're looking for but whenever I need a
    > serial-style timestamp (like for a filename). I just do this:
    >
    > Time.now.strftime("%Y%m%d%H%M%S")
    >
    > On Thu, Nov 19, 2009 at 2:19 PM, Reid Thompson <>wrote:
    >
    >
    >
    > > jp wrote:

    >
    > >> Hi,

    >
    > >> I would like to get a timestamp from a Date object. What is the best
    > >> aproach for this?

    >
    > >> thank you

    >
    > >>  I think you want a DateTime or Time object, I believe a Date objectonly

    > > represents a date, not a date and time unless perhaps 'date at midnight'
    > > counts.


    Hi,

    It's enough to have the milliseconds since a given date in the past,
    for example java has this:
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime()

    It's there anything comparable in Ruby?

    Thanks
    jp, Nov 19, 2009
    #5
  6. jp wrote:
    > On 19 Nov, 03:31, Sven Schott <> wrote:
    >> [Note: parts of this message were removed to make it a legal post.]
    >>
    >> Don't know if this is what you're looking for but whenever I need a
    >> serial-style timestamp (like for a filename). I just do this:
    >>
    >> Time.now.strftime("%Y%m%d%H%M%S")
    >>
    >> On Thu, Nov 19, 2009 at 2:19 PM, Reid Thompson <>wrote:
    >>
    >>
    >>
    >>> jp wrote:
    >>>> Hi,
    >>>> I would like to get a timestamp from a Date object. What is the best
    >>>> aproach for this?
    >>>> thank you
    >>>> I think you want a DateTime or Time object, I believe a Date object only
    >>> represents a date, not a date and time unless perhaps 'date at midnight'
    >>> counts.

    >
    > Hi,
    >
    > It's enough to have the milliseconds since a given date in the past,
    > for example java has this:
    > http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime()
    >
    > It's there anything comparable in Ruby?
    >
    > Thanks
    >
    >

    maybe...
    http://www.ruby-forum.com/topic/78420
    Reid Thompson, Nov 19, 2009
    #6
  7. 2009/11/19 jp <>:
    > On 19 Nov, 03:31, Sven Schott <> wrote:
    >> [Note: =A0parts of this message were removed to make it a legal post.]
    >>
    >> Don't know if this is what you're looking for but whenever I need a
    >> serial-style timestamp (like for a filename). I just do this:
    >>
    >> Time.now.strftime("%Y%m%d%H%M%S")
    >>
    >> On Thu, Nov 19, 2009 at 2:19 PM, Reid Thompson <>w=

    rote:
    >>
    >>
    >>
    >> > jp wrote:

    >>
    >> >> Hi,

    >>
    >> >> I would like to get a timestamp from a Date object. What is the best
    >> >> aproach for this?

    >>
    >> >> thank you

    >>
    >> >> =A0I think you want a DateTime or Time object, I believe a Date objec=

    t only
    >> > represents a date, not a date and time unless perhaps 'date at midnigh=

    t'
    >> > counts.

    >
    > Hi,
    >
    > It's enough to have the milliseconds since a given date in the past,
    > for example java has this:
    > http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime()
    >
    > It's there anything comparable in Ruby?


    Hint: there is documentation...

    irb(main):001:0> Time.now.to_i
    =3D> 1258647502
    irb(main):002:0> Time.at(0)
    =3D> 1970-01-01 01:00:00 +0100
    irb(main):003:0> Time.at(0).to_i
    =3D> 0

    Cheers

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 19, 2009
    #7
  8. 2009/11/19 Robert Klemme <>:
    > 2009/11/19 jp <>:
    >> On 19 Nov, 03:31, Sven Schott <> wrote:
    >>> [Note: =A0parts of this message were removed to make it a legal post.]
    >>>
    >>> Don't know if this is what you're looking for but whenever I need a
    >>> serial-style timestamp (like for a filename). I just do this:
    >>>
    >>> Time.now.strftime("%Y%m%d%H%M%S")
    >>>
    >>> On Thu, Nov 19, 2009 at 2:19 PM, Reid Thompson <>=

    wrote:
    >>>
    >>>
    >>>
    >>> > jp wrote:
    >>>
    >>> >> Hi,
    >>>
    >>> >> I would like to get a timestamp from a Date object. What is the best
    >>> >> aproach for this?
    >>>
    >>> >> thank you
    >>>
    >>> >> =A0I think you want a DateTime or Time object, I believe a Date obje=

    ct only
    >>> > represents a date, not a date and time unless perhaps 'date at midnig=

    ht'
    >>> > counts.

    >>
    >> Hi,
    >>
    >> It's enough to have the milliseconds since a given date in the past,
    >> for example java has this:
    >> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#getTime()
    >>
    >> It's there anything comparable in Ruby?

    >
    > Hint: there is documentation...
    >
    > irb(main):001:0> Time.now.to_i
    > =3D> 1258647502
    > irb(main):002:0> Time.at(0)
    > =3D> 1970-01-01 01:00:00 +0100
    > irb(main):003:0> Time.at(0).to_i
    > =3D> 0


    PS: I forgot

    irb(main):007:0> t=3DTime.now
    =3D> 2009-11-19 17:20:25 +0100
    irb(main):008:0> t.to_i
    =3D> 1258647625
    irb(main):009:0> t.to_f
    =3D> 1258647625.44141
    irb(main):010:0> t.usec
    =3D> 441411

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 19, 2009
    #8
    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. mhearne808[insert-at-sign-here]gmail[insert-dot-he

    Getting file timestamp from url

    mhearne808[insert-at-sign-here]gmail[insert-dot-he, Nov 17, 2007, in forum: Python
    Replies:
    2
    Views:
    472
    Tim Roberts
    Nov 18, 2007
  2. Neel
    Replies:
    2
    Views:
    348
    Alexander Dong Back Kim
    Sep 19, 2008
  3. Ian Collins

    Re: getting current timestamp in micro secs

    Ian Collins, Sep 18, 2008, in forum: C Programming
    Replies:
    20
    Views:
    720
    Ben Bacarisse
    Sep 19, 2008
  4. Richard Bos

    Re: getting current timestamp in micro secs

    Richard Bos, Sep 18, 2008, in forum: C Programming
    Replies:
    3
    Views:
    556
    Keith Thompson
    Sep 18, 2008
  5. Guest

    Getting a precise timestamp

    Guest, Jul 31, 2006, in forum: Ruby
    Replies:
    2
    Views:
    95
Loading...

Share This Page