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

Hi All,

I am trying to implement the logic that will return the date of nearest saturday (similar to oracle next_day function)... here is what I have so far:

perl -MDate::Calc=:all -e 'printf "%04s%02s%02s", Add_Delta_Days(Monda +y_of_Week(Week_of_Year(Today())), 5);'
The code above is giving correct results for most of the week hoever it looks wrong when run on saturday and sunday. Then instead of nearest(read next) saturday it returns current day...

Could you advice please what would be the best way to do this?

btw, I am looking for one liner but any solution is welcome.

Thanks in advance!
PP

Replies are listed 'Best First'.
Re: How for the current date return nearest saturday
by toolic (Bishop) on May 25, 2011 at 17:06 UTC
    From the RECIPES section of Date::Calc:

    9) How do I calculate the last and the next Saturday for any given date?

    I can't prove it works for Saturday (yet), but it seems to work for Wednesday (today)

    use Date::Calc qw( Today Day_of_Week Add_Delta_Days Day_of_Week_to_Text Date_to_Text ); #$searching_dow = 6; # 6 = Saturday $searching_dow = 3; # wed @today = Today(); $current_dow = Day_of_Week(@today); if ($searching_dow == $current_dow) { @prev = Add_Delta_Days(@today,-7); @next = Add_Delta_Days(@today,+7); } else { if ($searching_dow > $current_dow) { @next = Add_Delta_Days(@today, $searching_dow - $current_dow); @prev = Add_Delta_Days(@next,-7); } else { @prev = Add_Delta_Days(@today, $searching_dow - $current_dow); @next = Add_Delta_Days(@prev,+7); } } $dow = Day_of_Week_to_Text($searching_dow); print "Today is: ", ' ' x length($dow), Date_to_Text(@today), "\n"; print "Last $dow was: ", Date_to_Text(@prev), "\n"; print "Next $dow will be: ", Date_to_Text(@next), "\n"; __END__ Today is: Wed 25-May-2011 Last Wednesday was: Wed 18-May-2011 Next Wednesday will be: Wed 1-Jun-2011
Re: How for the current date return nearest saturday
by wind (Priest) on May 25, 2011 at 19:35 UTC
    Using DateTime:
    use DateTime; use strict; use warnings; my $dt = DateTime->now(); # Dow starts with Monday my %delta = (1 => -2, 2 => -3, 3 => 3, 4 => 2, 5 => 1, 6 => 0, 7 => -1 +); my $saturday = $dt->add(days => $delta{$dt->day_of_week}); print $saturday, "\n";
Re: How for the current date return nearest saturday
by zek152 (Pilgrim) on May 25, 2011 at 17:19 UTC

    Pretty easy fix. Add an offset if the day of the week is greater than 5 (will add an extra week if the day is saturday or sunday)

    printf "%04s/%02s/%02s", Add_Delta_Days(Monday_of_Week(Week_of_Year(Today())),5+7*(Day_of_Week(Today())>5));
      That's perfect and very smart solution, thanks a lot!
Re: How for the current date return nearest saturday
by Anonymous Monk on May 25, 2011 at 17:14 UTC
Re: How for the current date return nearest saturday
by ww (Archbishop) on May 26, 2011 at 01:48 UTC

    Your writing confused "nearest" and "next" ...until your parenthetical explantion in the paragraph after the code. A little clarity in your exposition would be a big help ...probably even more so for you than to those trying to help.

    What's the nearest Saturday, if today is Saturday. Why, it's today, not next Saturday, which is a full 7 days away. And if it's Sunday, the nearest Sat is the one just past.