#!/usr/bin/perl -w use strict; use Time::Local; # Get midnight of previous day. We can't simply subtract 24 hours since # number of hours in each day might be different due to daylight savings. sub PreviousDay($) { my ($t) = @_; # Assuming that input timestamp is already aligned to midnight, # going back 12 hours is guaranteed to put us in the previous day. my ($sec, $min, $hour, $mday, $mon, $year) = localtime($t - 43200); return timelocal(0, 0, 0, $mday, $mon, $year); } # Get first Sunday on or before first day of month. sub GetMonthStart($) { my ($t) = @_; # Go back to first day of the month. my ($sec, $min, $hour, $mday, $mon, $year) = localtime($t); $t = timelocal(0, 0, 0, 1, $mon, $year); # Go back to first Sunday. for(;;) { ($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime($t); last if $wday == 0; $t = PreviousDay($t); } return $t; }