Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I'm trying to code a script that will trigger different actions upon anniversaries.

What I need to do is read a past date (in the mm/dd/yyyy format) and compare it to today's date. Then, obtain the difference in days and trigger some actions according to this result.

Does anyone know how to do this, using standard Perl modules?

Please help.

Thanks,
Ralph.

Replies are listed 'Best First'.
Re: Trigger Anniversary
by emilford (Friar) on Jun 17, 2002 at 03:06 UTC
    A good module for finding the difference between two dates in days is Date::Calc. I used it to do something similar to what I think you are attempting to achieve. I wrote a script that checks a list of important days (i.e. - birthdays, anniversaries, etc) and emails me a reminder two weeks in advance. What can I say, I'm a forgetful person at times.

    Anyway, the Date::Calc module can be used to find the number of days, as well as a huge assortment of other cool bits of information. I use the line $difference = Delta_Days ($current_year, $current_month, $current_day, $stored_year, $stored_month, $stored_day); and the Date::Calc module returns the days between. The module takes standard input: yyyy for the year, mm (0-11) for the month, and dd (1-X) for the day. I also use the standard localtime[3,4,5] to get today's date before sending it to the Date::Calc module.

    Read up on the module and you'll discover its many uses. -Eric
Re: Trigger Anniversary
by graff (Chancellor) on Jun 17, 2002 at 02:42 UTC
    I doubt that anything beyond the core Perl functions will be needed:
    my $birthday = "2/29/1980"; # (not really -- just an example) my ($month,$date) = split /\//, $birthday; my ($today,$thismonth) = (localtime)[3,4]; $thismonth++; # localtime gives you "0" for January &letsParty if ( $today == $date and $thismonth == $month );
    Do read the output of "perldoc -f localtime" -- a great read.
Re: Trigger Anniversary
by Starky (Chaplain) on Jun 18, 2002 at 08:02 UTC
    You may find Schedule::Cron will be helpful depending on the exact nature of the trigger you are looking for.

    Hope this helps :-)