Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > find the number of hours in a day

Reply
Thread Tools

find the number of hours in a day

 
 
Nick Brown
Guest
Posts: n/a
 
      09-21-2009
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/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      09-21-2009
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)

 
Reply With Quote
 
 
 
 
Axel Etzold
Guest
Posts: n/a
 
      09-22-2009
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Now.day and day(now) returning the wrong day! Andy ASP .Net 1 11-06-2008 12:20 PM
Recorded 2 hours, deleted 2 hours, no space left? Tony Tee DVD Video 5 07-25-2007 10:20 AM
Date::Manip hours elapsed not business hours elapsed rutherf Perl Misc 2 10-28-2006 08:05 AM
1700 series: S0 is up 2 hours, down 2 hours, up, down, arghgh! Ivan Cisco 11 10-14-2004 08:41 AM
Hi im going to make a "time counter" from hours to hours and a loggfile export system Rahmi Acar C++ 0 07-17-2003 08:28 PM



Advertisments