此代码从当天返回前一天的日期。不确定您是否在寻找这个。我将它用于非关键模拟器,所以如果有的话,我并不担心极端情况。
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
printf("%s\n",TimeStamp2Date(Date2TimeStamp('2013-07-01') - (1*24*60*60)));
# Prints date of previous day.
sub Date2TimeStamp
{
if($_[0]=~/(\d\d\d\d)-(\d\d)-(\d\d)/)
{
my @Temp=split(/-/,$_[0]);
my $Stamp=mktime(0,0,01,$Temp[2],$Temp[1]-1,$Temp[0]-1900,0,0,-1) ;
#Seconds #Minutes #Hours #Day #Month #Year
return($Stamp);
}
else
{
printf("Error:Invalid date format\n");
getc();
}
}
sub TimeStamp2Date
{
my @temp = localtime($_[0]);
my $time = join('-',reverse(sprintf("%02d",$temp[3]), # Day
sprintf("%02d",$temp[4]+1), # Month(0-11)
sprintf("%04d",$temp[5]+1900)));# Year since 1900
return("$time");
}