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

Hello Monks!

One of my daily running applications (6am) needs the yesterdays date, or better the date of $param-1day.

Yesterday, sunday, was here in germany the change from 2am to 3am. Running on sunday with date=20100328 it was correct yday=20100327. But I got today with date=20100329 yday=20100327 again.

Code for this:

#!/bin/perl use strict; use Time::Local; die "Wrong parameter count." unless scalar(@ARGV) == 1; die "Wrong parameter format." unless $ARGV[0] =~ /(\d{4})(\d{2})(\d{2} +)/; my @d = localtime(timelocal(0, 0, 0, $3, $2-1, $1) - 86400); printf("%04d%02d%02d", ($d[5]+1900), $d[4]+1, $d[3]);

My question is now how I can fix this issue?

I searched for another module wich help me on this, but Date::Manip have the same problems (reported in "Known Bugs") and Date::Calc have to much dependencies that I have to install, just for task.

Greetings, Schaelle

Replies are listed 'Best First'.
Re: Yesterdays date while daylight saving time change
by moritz (Cardinal) on Mar 29, 2010 at 08:44 UTC
    Date::Simple to the rescue:
    $ perl -MDate::Simple=today -wle 'print today() - 1' 2010-03-28

    (Update:) Of course this can be generalized to get the previous date from any other date:

    $ perl -MDate::Simple=date -wle 'print date("2010-04-01") - 1' 2010-03-31

    Or in fact going any number of days forwards or backwards.

    Perl 6 - links to (nearly) everything that is Perl 6.

      This would be another solution than, because it's also just one module (like in my solution) with no dependencies.

Re: Yesterdays date while daylight saving time change
by JavaFan (Canon) on Mar 29, 2010 at 08:43 UTC
    Since you only want the date, don't take the date of 86400 (24 hours) seconds before midnight. Take 43200 (12 hours) before midnight - that should work unless you live in a piece of the world that shifts 12 hours during DST change over. Or use 86400 seconds before midday.
      Used this solution and just subtract only 12 hours instead of 24 hours. works with only 1 hour subtraction too. Thanks all!
        Used this solution and just subtract only 12 hours instead of 24 hours. works with only 1 hour subtraction too.
        If you use a 1 hour subtraction, you'll be back here in half a year when it fails because of a DST change in the other direction.
Re: Yesterdays date while daylight saving time change
by Corion (Patriarch) on Mar 29, 2010 at 08:43 UTC

    I prefer to do my date math by repeatedly adding/subtracting 22 hours until the day part of the date changes. Another approach is to Just Not Schedule Anything In The 02:00 To 03:00 Time Slot :).

      Another approach is to Just Not Schedule Anything In The 02:00 To 03:00 Time Slot
      Actually, that wasn't the OPs problem. (And it's not everywhere in Europe that clocks move at 02:00 - Europe moves at 01:00 UTC, which is 02:00 local time in large parts of Europe, but 01:00 local time in the UK, Ireland, and Portugal, and 03:00 local time further East). In fact, if he had done the calculations based from a 02:00 timestamp, he wouldn't not have run into problems. Assuming a DST shift of one hour, the OP could do his calculation of taking the date of the time 24 hours earlier if the time component is between 01:00:00 and 22:59:59.