Get the beginning of the hour and the end of the hour

Discussion in 'Ruby' started by Chris Gunnels, Sep 15, 2010.

  1. I would like to Get the beginning of the current hour and the end of the
    current hour in the regular time format Wed Sep 15 15:45:47 -0600 2010

    I would like to get Wed Sep 15 15:00:00 -0600 2010 and Wed Sep 15
    15:59:59 -0600 2010

    I have tried a few things and have search google for
    Time.now.beginning_of_hour and Time.now.end_of_hour, but no luck

    Any ideas?

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gunnels, Sep 15, 2010
    #1
    1. Advertising

  2. Chris Gunnels

    Jeremy Bopp Guest

    On 9/15/2010 4:47 PM, Chris Gunnels wrote:
    > I would like to Get the beginning of the current hour and the end of the
    > current hour in the regular time format Wed Sep 15 15:45:47 -0600 2010
    >
    > I would like to get Wed Sep 15 15:00:00 -0600 2010 and Wed Sep 15
    > 15:59:59 -0600 2010
    >
    > I have tried a few things and have search google for
    > Time.now.beginning_of_hour and Time.now.end_of_hour, but no luck


    Here's a quickie for the beginning of the current hour:

    Time.at(Time.now.to_i / 3600 * 3600)

    and for the end of the current hour:

    Time.at(Time.now.to_i / 3600 * 3600 + 59 * 60 + 59)

    You can simplify that last one a bit, but it's a little more obvious
    what's going on this way.

    -Jeremy
     
    Jeremy Bopp, Sep 15, 2010
    #2
    1. Advertising

  3. On 10-09-15 02:47 PM, Chris Gunnels wrote:
    > I would like to Get the beginning of the current hour and the end of the
    > current hour in the regular time format Wed Sep 15 15:45:47 -0600 2010
    >
    > I would like to get Wed Sep 15 15:00:00 -0600 2010 and Wed Sep 15
    > 15:59:59 -0600 2010
    >
    > I have tried a few things and have search google for
    > Time.now.beginning_of_hour and Time.now.end_of_hour, but no luck
    >
    > Any ideas?
    >
    > Thanks


    Time.local instantiation method?

    t = Time.now
    bt = Time.local(t.year,t.mon,t.day,t.hour,0,0)
    et = Time.local(t.year,t.mon,t.day.d.hour,59,0)

    xc

    --
    "It's the preponderance, stupid!" - Professor Stephen Schneider, IPCC member
     
    Xeno Campanoli / Eskimo North and Gmail, Sep 15, 2010
    #3
  4. Xeno Campanoli / Eskimo North and Gmail wrote:
    > On 10-09-15 02:47 PM, Chris Gunnels wrote:
    >>
    >> Thanks

    >
    > Time.local instantiation method?
    >
    > t = Time.now
    > bt = Time.local(t.year,t.mon,t.day,t.hour,0,0)
    > et = Time.local(t.year,t.mon,t.day.d.hour,59,0)
    >
    > xc


    awesome...thats what I wanted
    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gunnels, Sep 16, 2010
    #4
  5. On Thu, Sep 16, 2010 at 2:03 AM, Chris Gunnels <> wrote:
    > Xeno Campanoli / Eskimo North and Gmail wrote:
    >> On 10-09-15 02:47 PM, Chris Gunnels wrote:
    >>>
    >>> Thanks

    >>
    >> Time.local instantiation method?
    >>
    >> t = Time.now
    >> bt = Time.local(t.year,t.mon,t.day,t.hour,0,0)
    >> et = Time.local(t.year,t.mon,t.day.d.hour,59,0)
    >>
    >> xc

    >
    > awesome...thats what I wanted


    Not so fast! Is this really the end? Which hour does
    Time.local(t.year,t.mon,t.day.d.hour,59,17) belong to?

    I would probably rather do

    et = Time.local(t.year,t.mon,t.day.d.hour + 1,0,0)

    and treat the hour as half open interval (i.e. beginning is included,
    end is excluded). Note that Time also has #usec!

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Sep 16, 2010
    #5
    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. Replies:
    1
    Views:
    363
    Peter Flynn
    Dec 19, 2005
  2. Replies:
    2
    Views:
    911
    Hal Rosser
    Jun 1, 2007
  3. ssylee
    Replies:
    4
    Views:
    539
    Thad Smith
    Jan 1, 2008
  4. Jesse B.
    Replies:
    9
    Views:
    258
    Jesse B.
    Mar 27, 2010
  5. Replies:
    3
    Views:
    112
    Randy Webb
    Jul 17, 2006
Loading...

Share This Page