Created
July 19, 2010 22:56
-
-
Save preaction/482174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# As rotations get later, distance gets longer: | |
# 7 rotations are latest, 6 daily | |
# 10 rotations are latest, 6 daily and 3 weekly (a full month of coverage) | |
# 15 rotations are latest, 6 daily, 3 weekly, and 5 monthly (6 full months) | |
use DateTime; | |
my $MAX_DAYS = 7; | |
my $MAX_WEEKS = 4; | |
sub get_rotation_dates { | |
my ($dt_today, $num) = @_; | |
my $dt = $dt_today->clone->truncate( to => "day" ); | |
my $dt_sun; | |
my $dt_first_sun; | |
my @dates = (); | |
# 1 is "latest" | |
$num--; | |
# 2-7 are "daily" | |
my $days = 0; | |
while ( $num-- >= 0 && ++$days <= $MAX_DAYS - 1 ) { | |
$dt->subtract( days => 1 ); | |
if ( $dt->day_name eq "Sunday" ) { | |
$dt_sun = $dt->clone; | |
} | |
push @dates, $dt->ymd; | |
} | |
# 8-11 are "weekly" | |
$dt = $dt_sun->clone; | |
my $weeks = 0; | |
while ( $num-- >= 0 && ++$weeks <= $MAX_WEEKS ) { | |
$dt->subtract( weeks => 1 ); | |
if ( $dt->day <= 6 ) { | |
$dt_first_sun = $dt->clone; | |
} | |
push @dates, $dt->ymd; | |
} | |
# 12+ are "monthly" | |
$dt = $dt_first_sun->clone; | |
while ( $num-- >= 0 ) { | |
$dt->subtract( weeks => 4 ); | |
push @dates, $dt->ymd; | |
} | |
return @dates; | |
} ## end sub get_rotation_dates | |
$, = "\n"; | |
my $dt = DateTime->now->truncate( to => "day" )->subtract( days => 3 ); | |
print get_rotation_dates( $dt, 15 ); | |
print "\n\n"; | |
$dt->add( weeks => 3, days => 5 ); | |
print get_rotation_dates( $dt, 15 ); | |
print "\n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment