Lord Rau has asked for the wisdom of the Perl Monks concerning the following question:

OK, I have a script that looks at logs to build weekly usage stats. It's called from cron every Sunday, then on the last Sunday I clear out the results for the next month. Right now I have two scripts running from cron. The second is responsible for deleting the monthly data. The problem comes from the fact that if the accounting script doesn't finish before the cleaner, the data gets canned. I know there are a lot of ways to fix this, but I'd like to make a single script that can check to see if it's the last Sunday in a month and delete said data after the accounting subs. Since cron doesn't offer this granularity I'll be able to use this elsewhere hence me wanting to do it this way. Any tips?
  • Comment on Determining when the last day a script will run...

Replies are listed 'Best First'.
RE: Determining when the last day a script will run...
by nuance (Hermit) on May 16, 2000 at 04:46 UTC
    There is module on CPAN that will let you work with dates in the format that you want. Date::Manip. The following code works out the current month and then whether today is the last sunday. Hope you find it useful.
    #!/usr/bin/perl -w use strict; use Date::Manip; my $curmonth = &UnixDate("today","%b"); my $date1=&ParseDate("today"); my $date2=&ParseDate("last sunday in $curmonth"); my $flag=&Date_Cmp($date1,$date2); if ($flag<0) { # date1 is earlier } elsif ($flag==0) { # the two dates are identical } else { # date2 is earlier }

    Nuance

    Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

Re: Determining when the last day a script will run...
by lhoward (Vicar) on May 16, 2000 at 02:55 UTC
    You could use Date::Calc's Days_in_Month function (or any other function that will tell you how many days there are in the current month) and code as follows:
    if(($cur_day+7) > Days_in_Month($cur_year,$cur_month)){ # its the last n-day of the month 'cause this day next week is in +next month }
Re: Determining when the last day a script will run...
by turnstep (Parson) on May 16, 2000 at 19:48 UTC

    Quick and easy hack:

    for (`cal`) { /^(\d+)/ && $x=$1; } print "Latest Sunday is $x!\n";