in reply to Re: Week Algorithm
in thread Week Algorithm

Hi,
Based on your "points", including the fact that Ive been trying to simplify the way im going to handle the problem, I want to make some modifications:

1. Last week is 6 days before the current day, including the current day (so you will have 7 days always).
2. If the day/month has only 1 digit (eg. 1-9), there is no leading zeros. The year has a leading zero.

Thanks for your feedback. I'll try Date::Manip. Though I had a look at Date::Calc that was suggested by 3Dan, it seems to be complicated too, and not a quick solution. Otherwise, it has a lot of features. The tricky part is to get the output from the module and put it in a regular exp.

Did you ever notice that when you blow in a dog's face, it gets mad at you but when you take him on a car ride,he sticks his head out the window and likes it?

Replies are listed 'Best First'.
Re: Re: Re: Week Algorithm
by edan (Curate) on Aug 25, 2003 at 08:03 UTC

    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

    --
    3dan