3 回答

TA貢獻(xiàn)1854條經(jīng)驗 獲得超8個贊
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local; #自帶日期模塊
my $input = shift;
my ($year, $mon, $day) = $input=~/(\d{2})(\d{2})(\d{2})/;
my $today = time();
my $other_day = timelocal(0,0,0,$day,$mon-1, $year);
my $diff = $other_day - $today;
printf "It is %.2f days ", abs($diff/(60*60*24));
print $diff>0? "in the future\n" : "ago\n";

TA貢獻(xiàn)1799條經(jīng)驗 獲得超6個贊
模塊不用另外裝的。本來就有,連載入都不用啊,除非你的版本特別老?
以下是一些時間的常見用法。你自己直接用這些函數(shù)就好了,我做了簡單的注解。
my $t=time; # 得到時間,不過是一串?dāng)?shù)字
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($t); #也可以用=gmtime
$year+=1900; # $year變量的意思是since 1900
my @months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); # count since 0
message("$year-$months[$mon]-$mday $hour:$min:$sec, isdst: $isdst, day: $mday"); # number of seconds since Jan 1st, 1970
- 3 回答
- 0 關(guān)注
- 946 瀏覽
添加回答
舉報