in reply to Re: how to find out the date 'Tuesday' for this week
in thread how to find out the date 'Tuesday' for this week

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.
  • Comment on Re: Re: how to find out the date 'Tuesday' for this week

Replies are listed 'Best First'.
Re: Re: Re: how to find out the date 'Tuesday' for this week
by davorg (Chancellor) on Sep 04, 2002 at 16:55 UTC

    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