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

I was wondering if there was a function available in the Date::Calc module which can help me get the dates for all 7 days of the week (mon-sun) if i pass this function just one date from that week?

Thanks ;-}

Replies are listed 'Best First'.
Re: Date::Calc question.
by kesterkester (Hermit) on Oct 09, 2003 at 19:53 UTC
    A combination of Week_Number, Monday_of_Week, and Add_Delta_Days should do it:

    use warnings; use strict; use Date::Calc qw( Week_Number Monday_of_Week Add_Delta_Days ); my ( $year, $month, $day ) = ( 2003, 8, 9 ); my $week = Week_Number ( $year, $month, $day ); ( $year, $month, $day ) = Monday_of_Week ( $week, $year ); print "", join(" ", Add_Delta_Days ( $year, $month, $day, $_ )), "\n" foreach 0..6;
Re: Date::Calc question.
by Zed_Lopez (Chaplain) on Oct 09, 2003 at 20:07 UTC
    Help you? Yes. Do exactly that? Don't think so.
    use Date::Calc qw(:all); my ($year, $month, $day) = @ARGV; my $day_of_week = Day_of_Week($year, $month, $day) - 1; # after subtra +ction, Mon => 0 .. Sun => 6 print Date_to_Text(Add_Delta_Days($year, $month, $day, $_)) . "\n" for + (-$day_of_week .. (6-$day_of_week));
    Code actually tested this time. I feel like Mr. Date::Calc today.
Re: Date::Calc question.
by kelan (Deacon) on Oct 09, 2003 at 21:45 UTC

    Here's another way:

    use Date::Calc qw(Today Standard_to_Business Business_to_Standard Date +_to_Text); my ($year, $week, $dow) = Standard_to_Business(Today()); print Date_to_Text(Business_to_Standard($year, $week, $_)),"\n" for (1 + .. 7);
    One thing to remember is that in the Date::Calc module, weeks start with Monday, unlike most US calendars.

    kelan


    Perl6 Grammar Student

Re: Date::Calc question.
by jeffa (Bishop) on Oct 10, 2003 at 03:16 UTC
    Now that you have some good answers, might i interest you in anther CPAN module? ;) Time::Piece
    #!/usr/bin/perl -l use strict; use warnings; use Time::Piece; my $t = localtime; $t -= Time::Piece::ONE_DAY * $t->day_of_week; print $t += Time::Piece::ONE_DAY for 1..7;
    You mention that you want Monday through Sunday ... this code does so, but i usually use Sunday through Saturday. Just add one to day_of_week() to achieve this.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)