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

Dear Monks,

I come here for your wisdom, as I stumble upon this problem I have that I've been trying to accomplish eversince this mourning.

Can any of you please guide me on where to learn to find out the day range of the current week.

So for the month of June 2005, today is 6/23/2005. I want my Perl script to display this current week events. The start of the week was the Monday that just past which is 6/20 and the end of the week will be Sunday, which is 6/26.

This is easy of course to figure in your head, but I have no clue on where to start to tell Perl this. So basically what I'm searching for is a calculation or perhaps some module that does the calculating to find out the start of the week and the end of the week. I have this "annoucement script" and want to display the current weeks announcements.

Thanks!
  • Comment on Findout the day range of the current week

Replies are listed 'Best First'.
Re: Findout the day range of the current week
by GrandFather (Saint) on Jun 23, 2005 at 06:06 UTC

    Date::Calc does the trick:

    use strict; use warnings; use Date::Calc qw(:all); my ($year,$month,$day) = Today([gmtime()]); my $week; ($week,$year) = Week_of_Year($year,$month,$day); ($year, $month, $day) = Monday_of_Week ($week,$year); print "Monday this week is $year/$month/$day\n"; Monday this week is 2005/6/20

    Perl is Huffman encoded by design.
Re: Findout the day range of the current week
by tlm (Prior) on Jun 23, 2005 at 05:53 UTC

    Date::Calc comes to mind:

    use strict; use warnings; use Date::Calc qw( Today Day_of_Week Add_Delta_Days Date_to_Text ); my $monday_day_of_wk = 1; my @today = Date::Calc::Today(); my $current_day_of_wk = Day_of_Week( @today ); $current_day_of_wk += 7 if $current_day_of_wk < $monday_day_of_wk; my @day = Add_Delta_Days( @today, $monday_day_of_wk - $current_day_of_ +wk ); for ( 1..7 ) { print Date_to_Text( @day ), "\n"; @day = Add_Delta_Days( @day, 1 ); } __END__ Mon 20-Jun-2005 Tue 21-Jun-2005 Wed 22-Jun-2005 Thu 23-Jun-2005 Fri 24-Jun-2005 Sat 25-Jun-2005 Sun 26-Jun-2005

    the lowliest monk

Re: Findout the day range of the current week
by PodMaster (Abbot) on Jun 23, 2005 at 07:07 UTC
    You can also use DateTime :) Here's an example of something close enough Re: Re: Get date interval from week of year -> Date::Manip

    update: here's a direct example

    use strict; use warnings; use DateTime; my $now = DateTime->now; #$now = DateTime->new( year => 1960, month => 3, day => 19 ); my $mon = $now->clone->subtract( days => $now->day_of_week_0, ); my $sun = $mon->clone->add( days => 6, ); printf " Today is %s (%d) %s $/", $now->ymd, $now->day_of_week, $now->day_name, ; printf "Week begins %s (%d) %s $/", $mon->ymd, $mon->day_of_week, $mon->day_name, ; printf " Week ends %s (%d) %s $/", $sun->ymd, $sun->day_of_week, $sun->day_name, ; __DATA__ Today is 2005-06-23 (4) Thursday Week begins 2005-06-20 (1) Monday Week ends 2005-06-26 (7) Sunday

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Findout the day range of the current week
by TedPride (Priest) on Jun 23, 2005 at 05:50 UTC
    You can get the current weekday from localtime(), and it isn't hard to go backwards and forwards from there to the start / end of the week.

    Code to follow...

    use strict; use warnings; my $t = time(); $t -= $t % 86400; my $wday = (localtime($t))[6]; my $s = $t - 86400 * $wday; my $e = $t + 86400 * (7 - $wday) - 1; @_ = localtime($s); $_[4]++; $_[5] += 1900; print "$_[4]-$_[3]-$_[5] to "; @_ = localtime($e); $_[4]++; $_[5] += 1900; print "$_[4]-$_[3]-$_[5]";
    EDIT: Changed code slightly so $s and $e have the min timestamp on the first day and the max timestamp on the last day, respectively. You can now use them to check if a timestamp is inside the range.