1. perl 中date 的基本命令:
!/usr/bin/perl
$timestamp=time;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($timestamp);
printf("sec=%d min=%d hour=%d mday=%d mon=%d year=%d wday=%d yday=%d isdst=%d\n",$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
my $t = localtime($timestamp);
printf("%s\n",$t);
[hostname ~]$ ./test.pl
sec=5 min=23 hour=15 mday=12 mon=11 year=112 wday=3 yday=346 isdst=0
Wed Dec 12 15:23:05 2012
my $y = $year + 1900;
my $m = $mon + 1;
my $dt = $y.$m.$mday;
printf("%s\n",$dt);
dt = 20121212
(1). 系统正常显示:
上面代码中 localtime($timestamp)会显示正常的时间: Wed Dec 12 15:23:05 2012
如果需要在代码中获取 year、month、day、hour、minute、sec、weekday 需要用数组处理一下;
year =$year + 1900
month= $mon + 1
(2). perl 字符串连接用 .
my $dt = ($year + 1900).($mon + 1).$mday;
printf("dt = %s\n",$dt);
dt = 20121212