find the number of hours in a day

N

Nick Brown

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?
 
D

David A. Black

Hi --

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top