in reply to Increment through date range by day with only standard modules.
This solution doesn't use modules at all.
use strict; use warnings; my $startDate = q{2007-11-23}; my $endDate = q{2008-04-12}; my $leap = isLeap( ( split m{-}, $startDate )[ 0 ] ); my @dates = ( $startDate ); while( $startDate ne $endDate ) { $startDate = incByOneDay( $startDate ); push @dates, $startDate; } print do{ local $" = qq{\n}; qq{@dates\n}; }; sub incByOneDay { my $raDaysInMonth = [ [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ], [ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ], ]; my( $y, $m, $d ) = split m{-}, $_[ 0 ]; $d ++; $d = 1, $m ++ if $d > $raDaysInMonth->[ $leap ]->[ $m ]; $m = 1, $y ++, $leap = isLeap( $y ) if $m > 12; return sprintf q{%04d-%02d-%02d}, $y, $m, $d; } sub isLeap { my $year = shift; return 0 if $year % 4; return 1 if $year % 100; return 1 unless $year % 400; return 0; }
I would probably use modules if starting from scratch but I had this code lying around anyway so I thought I'd adapt it.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Increment through date range by day with only standard modules.
by Narveson (Chaplain) on Dec 12, 2008 at 23:11 UTC | |
by johngg (Canon) on Dec 12, 2008 at 23:19 UTC |