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

how to find out the date 'Tuesday' for this week? how to find out the date 'Tuesday' for last week?
  • Comment on how to find out the date 'Tuesday' for this week

Replies are listed 'Best First'.
Re: how to find out the date 'Tuesday' for this week
by dreadpiratepeter (Priest) on Sep 04, 2002 at 01:29 UTC
    Date::Calc is your friend! All hail Date::Calc.

    Here is a quick example I just tried (Monday of this week):
    perl -e 'use Date::Calc qw(:all);print join(",",Monday_of_Week(Week_of +_Year(Today))) . "\n"'


    -pete
    "Pain heals. Chicks dig scars. Glory lasts forever."
Re: how to find out the date 'Tuesday' for this week
by sauoq (Abbot) on Sep 04, 2002 at 01:34 UTC
    Look into Date::Manip. The Date_GetNext() and Date_GetPrev() functions will do what you want, I think.
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: how to find out the date 'Tuesday' for this week
by davorg (Chancellor) on Sep 04, 2002 at 08:26 UTC
    use POSIX 'strftime'; # get today my $today = time; # get current day of week my $dow = (localtime($today))[6]; # get last Sunday (day 0) my $sunday = $today - (86_400 * $dow); # get Tuesday (day 2) my $tuesday = $sunday + (86_400 * 2); # print Tuesday in a nice format print strftime "Tuesday is %d %B %Y\n", localtime $tuesday;
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      That is a nice way to do it without needing to install modules. However, it's often better to use a module anyway, because there are many subtle traps with time processing. For example, not all days are 86,400 seconds long, such as those Sundays when we switch between standard and daylight savings time.

        Yeah, I was going to add a note to that effect, but I seem to have forgotten.

        Dealing with that isn't much more complex, you just need to use Time::Local and normalise the times to midday. That way you can be sure that subtracting 86,400 seconds will get you to the previous day. Like this:

        use Time::Local; use POSIX 'strftime'; # get today my @today = localtime; # normalise to midday @today[0 .. 2] = (0, 0, 12); # get normalised today my $today = timelocal @today; # get current day of week my $dow = (localtime($today))[6]; # get last Sunday (day 0) my $sunday = $today - (86_400 * $dow); # get Tuesday (day 2) my $tuesday = $sunday + (86_400 * 2); # print Tuesday in a nice format print strftime "Tuesday is %d %B %Y\n", localtime $tuesday;
        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: how to find out the date 'Tuesday' for this week
by fglock (Vicar) on Sep 04, 2002 at 13:05 UTC

    Ok, this is just another way to do it

    use strict; use Date::Tie; tie my %d, "Date::Tie"; $d{weekday}=2; print $d{day};

    also golfing-style:

    perl -MDate::Tie -e 'tie %d,"Date::Tie"; $d{weekday}=2;print $d{day};'
Re: how to find out the date 'Tuesday' for this week
by zigdon (Deacon) on Sep 04, 2002 at 01:29 UTC
    I guess I'd try to combine localtime with timelocal - though I have a feeling that's not the best way to go.

    -- Dan