use warnings; use strict; use Date::Manip; my $t = time(); my $date = ParseDateString( "epoch $t" ); my $day_start = Date_SetTime( $date, 0, 0, 0); my $day_end = Date_SetTime( $date, 23, 59, 59); print "\nDay: ", start_end( $day_start, $day_end ); my $err; my ($year, $month, $day, $day_of_week) = UnixDate( $date, '%Y','%m', '%d', '%w'); my $week_start = $day_of_week == 1 ? $date : DateCalc( $date, 'last Monday', \ +$err) ; $week_start = Date_SetTime( $week_start, 0, 0, 0); my $week_end = $day_of_week == 7 ? $date : DateCalc( $date, 'next Sunday', \$e +rr) ; $week_end = Date_SetTime( $week_end, 23, 59, 59); print "\nWeek: ", start_end( $week_start, $week_end ); my $month_start = Date_SetDateField( $date, 'D', 1 ); $month_start = Date_SetTime( $month_start, 0, 0, 0); my $next_month = DateCalc( $date, 'next month' ); my $month_end_epoch = UnixDate( $next_month, '%s' ) - 1; my $month_end = ParseDateString( "epoch $month_end_epoch" ); $month_end = Date_SetTime( $month_end, 23, 59, 59); print "\nMonth: ", start_end( $month_start, $month_end ); my $year_start = Date_SetDateField( $date, 'M', 1); # January $year_start = Date_SetDateField( $year_start, 'D', 1 ); # 1 $year_start = Date_SetTime( $year_start, 0, 0, 0); my $year_end = Date_SetDateField( $date, 'M', 12); # December $year_end = Date_SetDateField( $year_end, 'D', 31 ); # 31 $year_end = Date_SetTime( $year_end, 23, 59, 59 ); print "\nYear: ", start_end( $year_start, $year_end ); exit; sub start_end { my ($date_start, $date_end) = @_; return join ( '', 'starts at ', UnixDate( $date_start, '%O'), ', ends at ', UnixDate( $date_end, '%O'), "\n\t(", UnixDate( $date_start, '%s'), ' - ', UnixDate( $date_end, '%s'), ")\n", ); }
A slight simplification is done anyway: timezone of the starting epoch is considered to be identical to the one in which the results are given.Day: starts at 2007-11-04T00:00:00, ends at 2007-11-04T23:59:59 (1194127200 - 1194213599) Week: starts at 2007-10-29T00:00:00, ends at 2007-11-04T23:59:59 (1193608800 - 1194213599) Month: starts at 2007-11-01T00:00:00, ends at 2007-11-30T23:59:59 (1193868000 - 1193954399) Year: starts at 2007-01-01T00:00:00, ends at 2007-12-31T23:59:59 (1167602400 - 1199138399)
In reply to Re: Date calculation: start and end of period
by Krambambuli
in thread Date calculation: start and end of period
by dgaramond2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |