[ABE.pm] finding calendar units

Jim Eshleman jce0 at Lehigh.EDU
Sun Jan 9 16:10:25 PST 2005


> Ok, I want to do something, and I know how to do it, but I'm hoping
> there exists a nice pre-built (non-DateTime!) solution.
> 
> 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)
> 
> Suggestions?

Something like this?:

#!/usr/bin/perl -w
 

use Date::Calc qw(:all);
use strict;
 

print "span(2004)=", span(2004), "\n";
print "span(2004,01)=", span(2004,01), "\n";
print "span(2004,01,02)=", span(2004,01,02), "\n";
 

sub span {
 

     my($year, $month, $day) = @_;
     my($startm, $startd, $endm, $endd);
 

     if ($month) {
         $startm = $endm = $month;
     } else {
         $startm = 1;
         $endm = 12;
     }
 

     if ($day) {
         $startd = $endd = $day;
     } else {
         $startd = 1;
         $endd = Days_in_Month($year, $endm);
     }
 

     my $startsecs = Mktime($year, $startm, $startd, 0, 0, 0);
     my $endsecs = Mktime($year, $endm, $endd, 23, 59, 59);
 

     return $endsecs - $startsecs + 1;
 

}

should produce:

$ ./span.pl
span(2004)=31622400
span(2004,01)=2678400
span(2004,01,02)=86400
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20050109/9c50a21b/signature.bin


More information about the ABE-pm mailing list