Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Getting yesterday's date, and random dates in the past

by mhearse (Chaplain)
on Apr 01, 2005 at 23:46 UTC ( #444324=perlquestion: print w/replies, xml ) Need Help??
mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I've got several scripts which rely on previous calendar dates. Not just yesterday, but possibly 60, 30, etc days in the past. A typical example would be to fill a hash with the caledar dates for the past 45 days. I wrote a subrouting which works fine. If I want a date other that yesterday, I just loop through the routine for the desired number of days. Is there a module which handles this type of date calculation easily? Or a standard way of handling this in Perl? I'm just looking for input.

My subroutine accepts a date such as 2005-03-01, and returns the previous date, similarly formatted.

sub get_yesterday { my $date = shift; my ($year, $month, $day) = split/-/,$date; $month--; my @days_per_month = qw(31 28 31 30 31 30 31 31 30 31 30 31); if (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) { $days_per_month[1] = '29'; } if ($day == 1) { if ($month == 0) { $month = '11'; $year--; } else { $month--; } $day = $days_per_month[$month]; } else { $day--; } $month++; for ($year, $month, $day) { if (/\b\d{1}\b/) { $_ = "0$_"; } } return "$year-$month-$day"; }

Replies are listed 'Best First'.
Re: Getting yesterday's date, and random dates in the past
by bart (Canon) on Apr 02, 2005 at 00:00 UTC
    If you have the seconds-since-epoch integer value, like time produces, it's easy to get the date of any number of seconds ago. Problems can arise due to Daylight Saving Time, but starting out from (around) noon on any day, you can always reliably derive the date for $x days in the past. Depending on whether you started out using timegm() or timelocal(), you can use gmtime() or localtime() to regenerate a string — possibly complemented with POSIX' strftime(), for more formatting options.

    Starting from a string, there are various date parsing modules available. Personally, I like Time::Local, a standard module, to convert date parts back into seconds since epoch. A bit of regexp manipulation, and you can easily pull the date parts out of the string, and you can complement them with your own values, such as 12 for the hour, for solving that DST problem I mentioned above.

    for ($year, $month, $day) { if (/\b\d{1}\b/) { $_ = "0$_"; } }
    Don't do that, use sprintf instead. sprintf "%02d", $n formats any number with 2 digits, using a leading 0 if necessary. And you can use it to produce a whole date string consisting of multiple parts, at once. Or like I said, use strftime() from POSIX.

    As an example, here's my code that does the equivalent from yours, for dates between 1970 and 2035 (or so). Much shorter!

    #!/usr/local/bin/perl -wl use Time::Local; use POSIX 'strftime'; sub get_yesterday { my $date = shift; my ($year, $month, $day) = split/-/,$date; my $time = timegm(0, 0, 12, $day, $month-1, $year); return strftime "%y-%m-%d", gmtime($time - 24*60*60); } print get_yesterday("2005-04-01");
    I get:

      You have to use timegm_nocheck, not timegm. The latter can handle month -1, for example.

      This is what I wanted. Sometimes I just can't find the right module. I agree that working with epoch makes date calculations easier. From what I can tell, those two modeules are included with the basic Perl install. All the better. Thanks much.
Re: Getting yesterday's date, and random dates in the past
by brian_d_foy (Abbot) on Apr 02, 2005 at 02:42 UTC

    The DateTime module (as well as other Date modules) can add or subtract arbitrary time values.

    #!/usr/bin/perl use DateTime; my $today = DateTime->now->truncate( to => 'day' ); my $yesterday = $today->subtract( days => 1 ); print "Today is $today\nYesterday was $yesterday\n";
    brian d foy <>

      even easier:

      my $today = DateTime->today;
Re: Getting yesterday's date, and random dates in the past
by Cristoforo (Curate) on Apr 02, 2005 at 02:42 UTC
    A module that makes simple date calculations painless is Date::Simple.
    #!/usr/bin/perl use strict; use warnings; use Date::Simple; my $date = Date::Simple->new('2005-03-01'); for (reverse 1..5) { print $date - $_, "\n"; }
Re: Getting yesterday's date, and random dates in the past
by ikegami (Pope) on Apr 02, 2005 at 00:39 UTC

    Why not use the following? It only uses core functions and modules, and works irregardless of Daylight savings Time.

    use Time::Local qw( timegm_nocheck ); sub get_yesterday { my ($date) = @_; my ($year, $month, $day) = split(/-/, $date); return sprintf("%d-%02d-%02d", (gmtime(timegm_nocheck(0, 0, 0, $day-1, month, $year)))[5, 4, 3] ); }
Re: Getting yesterday's date, and random dates in the past
by kelan (Deacon) on Apr 02, 2005 at 13:51 UTC

    Date::Calc for date calculations!

    use Date::Calc 'Add_Delta_Days'; sub get_yesterday { my ( $year, $month, $day ) = split /-/, shift; return join '-', Add_Delta_Days( $year, $month, $day, -1 ); }

Re: Getting yesterday's date, and random dates in the past
by tlm (Prior) on Apr 02, 2005 at 00:01 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://444324]
Approved by Tanktalus
[Corion]: Meh. I have a very simple mechanism to scrape+track prices, and even to send me mail if something changes (via cron), but SQLite doesn't support window functions, so my simple SQL to determine a price change won't work :-( Maybe I should store the DB ...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-02-23 15:00 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (302 votes). Check out past polls.