Perl 时间函数

Perl中两种时间格式:

print time(),"\n";    #取得系统时间

print join(",",gmtime()),"\n";    #格林尼治时间

print join(",",localtime()),"\n";    #本地时间

1311076590

30,56,11,19,6,111,2,199,0

30,56,19,19,6,111,2,199,0

30秒,56分,19时,19日,7月,2011年,星期二,一年的第199天,夏令时无效

令:

($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst)=localtime(time());

$sec     秒,0-59

$min     分,0-59

$hour   时,0-23

$day  月份中的日期,1-28,29,30,31

$mon  年份中的月份,0-11

$year  1900年以来的年份,年份加上1900,得出正确的4位数年的年份,

$wday  星期几,0-6,0表示周日

$yday  一年中的第几天,0-364,365

$isdst  如果夏令时有效,则为真

($week,$mon,$day,$ht,$year)=split(" ",localtime(time()));

 Tue Jul 19 19:56:30 2011

$week 星期缩写

$mon 月份缩写

$day  日期

$ht    时间(00:00:00)

$year 年

time()取得系统时间,例如:1311076590

gmtime()与localtime()默认调用参数time().

gmtime()=gmtime(time());

localtime()=localtime(time());

时间的计算

一天以前

$then=time()-1*86400;

三小时间前

$then=time()-3*3600;

40分钟后

$then=time()+40*60;

示例:

($week,$mon,$day,$ht,$year)=split(" ",time());

@months{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=split(",","01,02,03,04,05,06,07,08,09,10,11,12");

@week{qw(Sun Mon Tues Wed Thur Fri Sat)}=split(" ","日 一 二 三 四 五 六");

$mon=$months{$mon};

$week=$week{$week};

print "现在是$year年$mon月$day日$ht,星期$week\n";

$then=time()-3*3600;

$then=localtime($then);

{

my ($week,$mon,$day,$ht,$year)=split(" ",$then);

$then=$year."-".$months{$mon}."-".$day." ".$ht;

   #$then=sprintf("%04d-%02d-%02d %s",%year,$months{$mon},%day,$ht);

   #my $format_time=sprintf("%d-%d-%d %d:%d:%d",$year+1990,$mon+1,$mday,$hour,$min,$sec);

}

print "三小时以前是$then\n";

示例:

#!/usr/bin/perl

print time(),"\n";
print gmtime(),"\n";
print localtime(time()),"\n";

print join(",",gmtime()),"\n";
print join(",",localtime(time())),"\n";

($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst)=localtime(time());
$year+=1900;
$mon+=1;
print "time is $year,month is $mon,day is $day\n";
print "hour is $hour,minutes is $min,second is $sec\n";


$now=time();
$then=$now-3600-$now%3600;
$start_time=$now-3600-$now%3600;
$now=localtime($now);
$then=localtime($then);
print "now is $now\n";
print "then is $then\n";
($week,$mon,$day,$ht,$year)=split(" ",$now);
@months{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=split(",","01,02,03,04,05,06,07,08,09,10,11,12");
@week{qw(Sun Mon Tues Wed Thur Fri Sat)}=split(" ","日 一 二 三 四 五 六");
$mon=$months{$mon};
$week=$week{$week};
print "now is $year年$mon月$day日$ht,星期$week\n";


print "------------------------------------\n";
$now=time();
print "now is $now\n";
$now=$now-3600*10;
$start_time=$now-3600-$now%3600;
$end_time=$now-$now%3600;
$start_time=localtime($start_time);
{
	my ($week,$mon,$day,$ht,$year)=split(" ",$start_time);
	$start_time=$year."-".$months{$mon}."-".$day." ".$ht;
}
$end_time=localtime($end_time);
{
	my ($week,$mon,$day,$ht,$year)=split(" ",$end_time);
	$end_time=$year."-".$months{$mon}."-".$day." ".$ht;
}
print "start is $start_time\n";
print "end id $end_time\n";
posted @ 2011-07-19 21:05  zjking99  阅读(15823)  评论(0编辑  收藏  举报