in reply to Re: Re: Week Algorithm
in thread Week Algorithm
Here is a lightly-tested solution using POSIX::mktime and localtime calculations based on your latest requirements. I make some assumptions about your data, so you may have to modify the splits to match your data correctly.
use POSIX qw/mktime/; my ($dy, $mo, $yr) = (localtime)[3 .. 5]; $mo += 1; $yr = sprintf("%02d", $yr % 100); my $today = join '-', $mo, $dy, $yr; my $today_in_epoch = get_epoch_time($today); my $week_ago = $today_in_epoch - (60 * 60 * 24 * 6); # 6 days print "today is: $today\n"; while (<DATA>) { my ($date, undef) = split /,/; my $date_in_epoch = get_epoch_time($date); if ($date_in_epoch >= $week_ago && $date_in_epoch <= $today_in_epo +ch) { print "$date is in the last 6 days!!\n"; } } sub get_epoch_time { my $date = shift; my ($mo, $day, $year) = split /-/, $date; # notice we assume the year is 20XX return POSIX::mktime( 0, 0, 0, $day, $mo - 1, 100 + $year ); } __DATA__ 8-12-03,somename,someaddress....<br> 8-13-03,someothername,someaddress.... 8-20-03,foo,bar 8-18-03,blah,blah 8-19-03,blah,blah 08-25-03,blah,blah 8-30-03,future,date
HTH
--
|
|---|