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:
05-03-31
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
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.
| [reply] |
#!/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 <brian@stonehenge.com>
| [reply] [d/l] |
even easier:
my $today = DateTime->today;
| [reply] [d/l] |
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";
}
prints:
2005-02-24
2005-02-25
2005-02-26
2005-02-27
2005-02-28
Cris | [reply] [d/l] |
use Date::Calc 'Add_Delta_Days';
sub get_yesterday {
my ( $year, $month, $day ) = split /-/, shift;
return join '-', Add_Delta_Days( $year, $month, $day, -1 );
}
| [reply] [d/l] |
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]
);
}
| [reply] [d/l] |
| [reply] |