Re: How to Compute Hours In a Day?

[email protected] (Zefram)
Newsgroups perl.datetime
Message-ID <[email protected]>
[email protected] wrote:
>How do I compute the number of hours in a day in a specific time zone?

Try this:

use DateTime;
for(my $today = DateTime->new(year=>2007, time_zone=>$ARGV[0]); $today->year == 2007; ) {
	(my $tomorrow = $today->clone)->add(days=>1);
	print $today, "  ", ($tomorrow->epoch - $today->epoch)/3600, "\n";
	$today = $tomorrow;
}

It ought to be possible to do the interval calculation using
DateTime::Duration instead of ->epoch, but the behaviour of DT::D is
unreasonably confusing and I couldn't get it to work.  (Strangely, I got
a version that worked fine for America/New_York, where all the days are
integer numbers of hours, but lost the fractional hour of 2007-12-09 in
America/Caracas, which was 24.5 hours long.)

Beware that the above code assumes that
midnight exists every day.  We recently had a thread
<http://www.nntp.perl.org/group/perl.datetime/2008/10/msg7086.html> about
midnight not existing in some timezones, specifically America/Sao_Paulo.

-zefram
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.