Chon-Ji has asked for the wisdom of the Perl Monks concerning the following question:

Hi! Is it possible to get the previous date in perl? I know we can get the current date using localtime but how do we get the previous date? thanks

Replies are listed 'Best First'.
Re: Get previous date
by davorg (Chancellor) on Jun 30, 2006 at 07:52 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Get previous date
by davido (Cardinal) on Jun 30, 2006 at 08:15 UTC

    You could use Date::Calc like this:

    use strict; use warnings; use Date::Calc qw/Date_to_Text Add_Delta_Days Today/; print "Yesterday: ", Date_to_Text( Add_Delta_Days( Today(), -1 ) ), "\n";

    This approach has the advantage of being very easy to read, and less prone to goofing up the math behind subtracting a day from localtime, and it should avoid daylight savings time issues.


    Dave

Re: Get previous date
by Corion (Patriarch) on Jun 30, 2006 at 07:45 UTC

    The time function gives you the current time in seconds. The previous day is the day which is at least 22 hours before the current time but has a different day date than the current day. 22 hours translates to 22 * 3600 seconds.

    So one approach would be not to use localtime without an argument but localtime with a time() value adjusted for the difference you want and then check that the day date is different.

Re: Get previous date
by Herkum (Parson) on Jun 30, 2006 at 11:33 UTC
    If your looking for the previous day you can use DateTime easily enough.
    use DateTime; my $date = DateTime->today; $date->subtract( days => 1 ); warn "Yesterday's date is " . $date->dmy('-') . "\n";
Re: Get previous date
by Ieronim (Friar) on Jun 30, 2006 at 09:54 UTC
    Another way to get yesterday's date:
    #!/usr/bin/perl use warnings; use strict; use Time::Local 'timelocal_nocheck'; sub days_before_now { my @date = localtime; return timelocal_nocheck(@date[0..2],$date[3]-$_[0], @date[4,5]); } print scalar( localtime days_before_now(1)), "\n";
    days_before_now(-1) will return the tomorrow's date ;)
Re: Get previous date
by radiantmatrix (Parson) on Jun 30, 2006 at 15:13 UTC

    Subtract a day? Date::Calc exists for this reason.

    use Date::Calc qw[Add_Delta_Days Today]; my ($year, $month, $day) = Today(); print "Today is $year-$month-$day; "; ($year, $month, $day) = Add_Delta_Days( Today(), -1 ); print "Yesterday was $year-$month-$day!\n"; __END__ # the above code prints: Today is 2006-6-30; Yesterday was 2006-6-29!

    This way, you'll take into account all the weirdness of dates (like leap-years, to pick a simple example) without very much work. Good luck!

    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: Get previous date
by Moron (Curate) on Jun 30, 2006 at 10:53 UTC
    At the risk of being awkward: in most industries, it is the previous business day that is needed, often because large corporate end-to-end systems take longer than the few hours between COB and midnight to complete and have to pass files down a production line of systems. It is also a usual requirement that occasionally some transactions dated on a Friday may be processed on a Sunday or even later. Public holidays, not just weekends, often have to be taken into account. Some systems perform empty runs on the day after public holidays to overcome this problem, but that can't work for every situation and (for e.g. credit card, ATM and GSM billing systems) the algorithm often has to be able to handle DST changes without double-entering transactions.

    -M

    Free your mind

A reply falls below the community's threshold of quality. You may see it by logging in.