Skip to content

Instantly share code, notes, and snippets.

@preaction
Created July 19, 2010 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preaction/482174 to your computer and use it in GitHub Desktop.
Save preaction/482174 to your computer and use it in GitHub Desktop.
# 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