ԱŮװ
Ů
׷
Դװ
˼Դ
Ҿ̳
Ա
װ
װ
83ʱŮ
Ҿװ
Ա̳
Ա̳ Ҿװ Ҿ̳ 83ʱŮ ԱŮװ Ů ׷ Դװ ˼Դ װ Ա װ

存档

文章标签 ‘time’

perl下的时间处理

2009年6月20日 4 条评论

获得unix时间:

1
2
3
4
5
#!/usr/bin/perl 
use strict;
use warnings;
 
print time;

获得当前系统时间:

1
print scalar localtime();

这里localtime()可带一个时间参数,默认是以当前的unix时间为参数,若要得到unix时间100秒的具体时间,可以用localtime(100)

unix时间转换成普通的日期时间:

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

$sec –> 秒
$min –> 分
$hour –> 时
$mday –> 当月的第几天(1..31)
$mon –> 月(0.11)
$year –> 年(1900..,从1900年开始,所以得加上1900才能得到当前年份)
$wday –> 一周中的第几天(0是星期天,然后1..6代表周一到周六)
$yday –> 一年中的第几天(0..365)
$isdst –> 夏令时标志,如果当前时间正在使用夏令时,则$isdst为1,否则为0。
执行localtime函数得到的就是由上面这些值所组成的一个字符串。

普通的日期时间转换成unix时间
有时候为了得到两次时间间隔,把普通日期时间转换成unix时间处理起来就方便多了。

1
2
3
use Time::Local;
my $time = timelocal($sec,$min,$hour,$mday,$mon,$year); 
# replace 'timelocal' with 'timegm' if your input date is GMT/UTC
分类: tips 标签: , ,