[ABE.pm] finding calendar units
Ricardo SIGNES
rjbs-perl-abe at lists.manxome.org
Sun Jan 9 14:14:26 PST 2005
* Ricardo SIGNES <rjbs-perl-abe at lists.manxome.org> [2005-01-09T15:09:17]
> Given a date in the format [year, [month, [ day ]]] I want to determine
> (begin, end) seconds. So:
> span(2004); # (first sec of 2004, last sec of 2004)
> span(2004,01); # (first sec of jan 2004, last sec of jan 2004)
> span(2004,01,02); # (first sec of jan 2nd 2004, last sec of jan 2nd 2004)
This is what I'm using in the interim:
use strict;
use warnings;
use Time::Local;
my @monthdays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
# ly if /4 and not /100 unless % 400
sub is_leap_year {
return 0 if $_[0] % 4;
return 1 if not $_[0] % 400;
return 0 if not $_[0] % 100;
return 1;
}
sub span {
my $date = shift;
my ($y,$m,$d) = $date =~ /\A(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?\Z/;
return unless $y;
my $begin_secs = timelocal(0,0,0,$d||1,$m||0,$y);
my $length;
if ($d) {
$length = 86400
} elsif ($m) {
$length = 86400 * $monthdays[$m+0];
$length++ if $m==1 and is_leap_year($y)
} else {
$length = 86400 * is_leap_year($y) ? 366 : 365;
}
return ($begin_secs, $begin_secs + $length);
}
--
rjbs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20050109/a5fefcc2/attachment.bin
More information about the ABE-pm
mailing list