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

Noob alert ;) I need help in getting the nearest ___day from the current date. For example, I want to know what date is the next, nearest Monday from today. Any ready to use function in Perl? Thanks! H

Replies are listed 'Best First'.
Re: Calculate the nearest day
by GrandFather (Saint) on Apr 13, 2007 at 02:09 UTC

    Various of the date/time modules on CPAN do that trick including the "kitchen sink" module Date::Manip. For example:

    use Date::Manip; print UnixDate (ParseDate ('next monday'), '%c');

    Prints:

    Mon Apr 16 00:00:00 2007

    DWIM is Perl's answer to Gödel
Re: Calculate the nearest day
by Herkum (Parson) on Apr 13, 2007 at 02:02 UTC
    take a look at DateTime, it offers good functionality for getting particular days on the week and month.
Re: Calculate the nearest day
by Trizor (Pilgrim) on Apr 13, 2007 at 02:03 UTC
    Date::Calc and Date::Manip have the functions you need for this sort of thing. They account for just about everything under the sun in terms of things like leap seconds, leap years, other strange calendarial phenomena, the phase of the moon, and perhaps they might even determine what phase the spoon you're using to eat your cereal is in. Definatly what you want, but pick one and stick with it, I don't reccomend using them both in the same program as it becomes a headache later on. Or even both on the same box where applications might one day want to play nice.
Re: Calculate the nearest day
by badaiaqrandista (Pilgrim) on Apr 13, 2007 at 03:31 UTC

    Use Date::Simple. That's my favorite module for all date related operations. And my favorite for time related stuff is Time::Piece, if you ever need to go more than just date operations.

    use Date::Simple; my $d = Date::Simple->today; printf "today is: %s\n", $d->day_of_week; printf "tomorrow is: %s\n", ($d + 1)->day_of_week; printf "yesterday is: %s\n", ($d - 1)->day_of_week;
    -cheepy-
Re: Calculate the nearest day
by thezip (Vicar) on Apr 13, 2007 at 21:30 UTC

    In the spirit of TMTOWTDI, here's a version that only uses core Perl (ie. no Date::* modules):

    #!/perl/bin/perl -w use strict; use Time::Local; my $next_target_day = 'Mon'; my $one_day = 86400; my @DOWname = qw/ Sun Mon Tues Wed Thurs Fri Sat /; my %DOWhash = map { $DOWname[$_] => $_ } 0 .. $#DOWname; my @T = localtime(time); my $days_til_target = $DOWhash{$next_target_day} - $T[6]; $days_til_target += 7 if $days_til_target < 0; my $beginning_of_today = timelocal(0,0,0,$T[3],$T[4],$T[5]); print scalar localtime($beginning_of_today), "\n"; print scalar localtime($beginning_of_today + $days_til_target*$one_day + $one_day/2 ), "\n";

    Where do you want *them* to go today?
      By using timegm+gmtime, you can get rid of the + $one_day/2:
      my $beginning_of_today = timegm(0,0,0,$T[3],$T[4],$T[5]); print scalar gmtime($beginning_of_today), "\n"; print scalar gmtime($beginning_of_today + $days_til_target*$one_day ), + "\n";