三个perl 的时间函数
time()
表示从1970年开始到现在时间的总秒数。
localtime()
表示系统时间。
($sec,$min,$hour,$mday,$mon,$year_off,$wday,$yday,$isdat) = localtime;
字段 值
$sec 秒,0 ~ 59
$min 分,0 ~ 59
$hour 时,0 ~ 23
$mday 月份中的日期, 1 ~ 2 8、2 9、3 0或3 1
$mon 年份中的月份, 0 ~ 11(这里请特别要小心)
$year_off 1900年以来的年份。将1900加上这个数字,得出正确的4位数年份
$wday 星期几,0 ~ 6
$yday 一年中的第几天,0 ~ 364或365
$isdst 如果夏令时有效,则为真
my ($sec,$min,$hour,$mday,$mon,$year) = (localtime)[0..5];
($sec,$min,$hour,$mday,$mon,$year) = (
sprintf("%02d", $sec),
sprintf("%02d", $min),
sprintf("%02d", $hour),
sprintf("%02d", $mday),
sprintf("%02d", $mon + 1),
$year + 1900
);
print "$year-$mon-$mday $hour:$min:$sec\n";
gmtime()
表示标准格林威治时间。
-
#!/usr/bin/perl
-
use strict;
-
-
my $time = time();
-
my $localtime = localtime();
-
my $gmtime = gmtime();
-
my ($sec,$min,$hour,$mday,$mon,$year_off,$wday,$yday,$isdat) = localtime;
-
print "time() = ".$time."\n";
-
print "localtime() = ".$localtime."\n";
-
print "gmtime() = ".$gmtime."\n";
以上代码运行后显示不同的形式的时间。 time()取到的时间也是gm time,只是表现形式不同而已。
Perl关于时间的常用模块
HTTP::Date
参考cpan地址: ~gaas/HTTP-Date-6.02/lib/HTTP/Date.pm
-
#!/grid/common/bin/perl
-
-
use strict;
-
use HTTP::Date;
-
use HTTP::Date qw(time2iso time2isoz parse_date);
-
-
my $time = time();
-
my $time_string = time2str($time);
-
print $time_string."\n";
-
my $local_time_string = localtime();
-
my $local_time = str2time($local_time_string);
-
print $local_time."\n";
-
my $time_format1 = time2isoz($time);
-
print $time_format1."\n";
-
my $string = parse_date("Feb 3 17:03");
-
print $string."\n";
time2str
将1970年至时间的秒数转换成字符串形式显示时间
str2time
将字符串形式的时间转换成距1970年的秒数
parse_date
转换时间显示格式
可转换的时间格式:
"Wed, 09 Feb 1994 22:23:32 GMT" -- HTTP format
"Thu Feb 3 17:03:55 GMT 1994" -- ctime(3) format
"Thu Feb 3 00:00:00 1994", -- ANSI C asctime() format
"Tuesday, 08-Feb-94 14:15:29 GMT" -- old rfc850 HTTP format
"Tuesday, 08-Feb-1994 14:15:29 GMT" -- broken rfc850 HTTP format
"03/Feb/1994:17:03:55 -0700" -- common logfile format
"09 Feb 1994 22:23:32 GMT" -- HTTP format (no weekday)
"08-Feb-94 14:15:29 GMT" -- rfc850 format (no weekday)
"08-Feb-1994 14:15:29 GMT" -- broken rfc850 format (no weekday)
"1994-02-03 14:15:29 -0100" -- ISO 8601 format
"1994-02-03 14:15:29" -- zone is optional
"1994-02-03" -- only date
"1994-02-03T14:15:29" -- Use T as separator
"19940203T141529Z" -- ISO 8601 compact format
"19940203" -- only date
"08-Feb-94" -- old rfc850 HTTP format (no weekday, no time)
"08-Feb-1994" -- broken rfc850 HTTP format (no weekday, no time)
"09 Feb 1994" -- proposed new HTTP format (no weekday, no time)
"03/Feb/1994" -- common logfile format (no time, no offset)
"Feb 3 1994" -- Unix 'ls -l' format
"Feb 3 17:03" -- Unix 'ls -l' format
"11-15-96 03:52PM" -- Windows 'dir' format
time2isoz
将1970年至时间的秒数转换成 “YYYY-MM-DD hh:mm:ssZ”格式
time2iso
将1970年至时间的秒数转换成 “YYYY-MM-DD hh:mm:ss”格式
阅读(603) | 评论(0) | 转发(0) |