find the number of hours in a day

Discussion in 'Ruby' started by Nick Brown, Sep 21, 2009.

  1. Nick Brown

    Nick Brown Guest

    I'm trying to determine the best way to find the number of hours (or
    seconds) in a given day in a given timezone. Is there a "right" way to
    do this with Ruby?

    There are so many ways to deal with dates and times in Ruby (Time, date,
    datetime, ActiveSupport:...:Time, Chronic, ActionView) that I almost
    don't know where to start.

    Basically, if there are 25 or 23 hours in a day due to daylight savings,
    I need to know so that I can handle that special case. How would you
    handle this?
    --
    Posted via http://www.ruby-forum.com/.
     
    Nick Brown, Sep 21, 2009
    #1
    1. Advertising

  2. Hi --

    On Tue, 22 Sep 2009, Nick Brown wrote:

    > I'm trying to determine the best way to find the number of hours (or
    > seconds) in a given day in a given timezone. Is there a "right" way to
    > do this with Ruby?
    >
    > There are so many ways to deal with dates and times in Ruby (Time, date,
    > datetime, ActiveSupport:...:Time, Chronic, ActionView) that I almost
    > don't know where to start.
    >
    > Basically, if there are 25 or 23 hours in a day due to daylight savings,
    > I need to know so that I can handle that special case. How would you
    > handle this?


    I suspect there's a better way, probably already in a library
    somewhere, but what occurs to me in the absence of knowing where is:

    def seconds(day)
    Integer(day.tomorrow.midnight - day.midnight)
    end

    def hours(day)
    seconds(day) / 3600
    end

    (using ActiveSupport). I guess the Integer part might clash with leap
    seconds, but you get the idea.


    David

    --
    David A. Black, Director
    Ruby Power and Light, LLC (http://www.rubypal.com)
    Ruby/Rails training, consulting, mentoring, code review
    Book: The Well-Grounded Rubyist (http://www.manning.com/black2)
     
    David A. Black, Sep 21, 2009
    #2
    1. Advertising

  3. Nick Brown

    Axel Etzold Guest

    > Hi --
    >
    > On Tue, 22 Sep 2009, Nick Brown wrote:
    >
    > > I'm trying to determine the best way to find the number of hours (or
    > > seconds) in a given day in a given timezone. Is there a "right" way to
    > > do this with Ruby?
    > >
    > > There are so many ways to deal with dates and times in Ruby (Time, date,

    >
    > I suspect there's a better way, probably already in a library
    > somewhere, but what occurs to me in the absence of knowing where is:
    >


    Dear Nick,

    maybe this can help determine what places have daylight saving time at
    a particular date:

    http://tzinfo.rubyforge.org/

    Best regards,

    Axel
    --
    Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3 -
    sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
     
    Axel Etzold, Sep 22, 2009
    #3
    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. Rahmi Acar
    Replies:
    0
    Views:
    563
    Rahmi Acar
    Jul 17, 2003
  2. Andy
    Replies:
    1
    Views:
    719
  3. Guest
    Replies:
    0
    Views:
    108
    Guest
    Nov 3, 2004
  4. Zayd Connor

    How Many hours in A day

    Zayd Connor, Mar 11, 2009, in forum: Ruby
    Replies:
    15
    Views:
    230
    Bassel Samman
    Mar 13, 2009
  5. rutherf
    Replies:
    2
    Views:
    431
    rutherf
    Oct 28, 2006
Loading...

Share This Page