Output:#!/usr/bin/perl use strict; use warnings; use Time::Local; my $t = time; ## or, plug in your own timestamp my ($day_start, $day_end) = day_period ($t); my ($month_start, $month_end) = month_period ($t); my ($year_start, $year_end) = year_period ($t); print "The day starts at ", scalar localtime $day_start, "\n"; print "The day ends at ", scalar localtime $day_end, "\n"; print "The month starts at ", scalar localtime $month_start, "\n"; print "The month ends at ", scalar localtime $month_end, "\n"; print "The year starts at ", scalar localtime $year_start, "\n"; print "The year ends at ", scalar localtime $year_end, "\n"; sub day_period { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me shift; my $t1 = timelocal (0, 0, 0, $mday, $mon, $year); my $t2 = timelocal (0, 0, 0, $mday+1, $mon, $year); wantarray ? ($t1, $t2) : \($t1, $t2) } sub month_period { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me shift; my $t1 = timelocal (0, 0, 0, 1, $mon, $year); my $t2 = timelocal (0, 0, 0, 1, $mon+1, $year); wantarray ? ($t1, $t2) : \($t1, $t2) } sub year_period { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me shift; my $t1 = timelocal (0, 0, 0, 1, 0, $year ); my $t2 = timelocal (0, 0, 0, 1, 0, $year+1); wantarray ? ($t1, $t2) : \($t1, $t2) }
If you need week-long periods, you can either use a heavier, non-core module like Date::Manip, or write the logic yourself (hope you like special cases!).The day starts at Sat Nov 3 00:00:00 2007 The day ends at Sun Nov 4 00:00:00 2007 The month starts at Thu Nov 1 00:00:00 2007 The month ends at Sat Dec 1 00:00:00 2007 The year starts at Mon Jan 1 00:00:00 2007 The year ends at Tue Jan 1 00:00:00 2008
Update: And if you want the periods to end just at the end of the day/month/year rather than at the beginning of the next one... subtract 1 from $XXXX_end.
In reply to Re: Date calculation: start and end of period
by gamache
in thread Date calculation: start and end of period
by dgaramond2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |