# Find the number of hours in a day

**URL:** <https://www.ruby-forum.com/t/find-the-number-of-hours-in-a-day/175868>\
**Category:** Ruby\
**Created:** [September 21, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/find-the-number-of-hours-in-a-day/175868 "2009-09-21T22:31:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 21, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/find-the-number-of-hours-in-a-day/175868/1 "2009-09-21T22:31:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 21, 2009, 11:12pm UTC](https://www.ruby-forum.com/t/find-the-number-of-hours-in-a-day/175868/2 "2009-09-21T23:12:00Z")

</div>

Hi –

On Tue, 22 Sep 2009, Nick B. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 22, 2009, 9:46am UTC](https://www.ruby-forum.com/t/find-the-number-of-hours-in-a-day/175868/3 "2009-09-22T09:46:29Z")

</div>

> 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/](http://tzinfo.rubyforge.org/)

Best regards,

Axel
