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.
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.for ($year, $month, $day) { if (/\b\d{1}\b/) { $_ = "0$_"; } }
As an example, here's my code that does the equivalent from yours, for dates between 1970 and 2035 (or so). Much shorter!
I get:#!/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");
05-03-31
In reply to Re: Getting yesterday's date, and random dates in the past
by bart
in thread Getting yesterday's date, and random dates in the past
by mhearse
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |