my ($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime(time);
$wday = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')[$wday]
$year += 1900;
printf FILES "$year/$mon/$mday %02d:%02d:%02d ", $hour, $min, $sec;
打印输出root用户的日期值,而不是运行脚本的日期值。对此有任何想法。
答案 0 :(得分:1)
您还需要在月份中添加1,因为localtime
从零开始返回,您应该使用printf
中的格式说明符,以便字段大小不变。
use strict;
use warnings;
my ($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime;
$wday = qw( Sun Mon Tue Wed Thu Fri Sat )[$wday];
$year += 1900;
++$mon;
printf "%s %4d/%02d/%02d %02d:%02d:%02d\n", $wday, $year, $mon, $mday, $hour, $min, $sec;
<强>输出强>
Tue 2015/03/17 14:06:36
或者你可以在这里非常有效地使用Time::Piece
。
use strict;
use warnings;
use Time::Piece;
print localtime->strftime('%a %Y/%m/%d %H:%M:%S'), "\n";
<强>输出强>
Tue 2015/03/17 14:06:36
答案 1 :(得分:0)
time()
会给你UTC。
localtime
将根据$ENV{'TZ'}
进行调整 - 我唯一能想到的可能导致您的问题的原因是,如果设置不正确。尝试将其打印为脚本的一部分。
Time::Piece
是一个很好的模块:
$t->tzoffset # timezone offset in a Time::Seconds object
$t->strftime(FORMAT) # same as POSIX::strftime (without the overhead
# of the full POSIX extension)
$t->strftime() # "Tue, 29 Feb 2000 12:34:56 GMT"
您可以按格式打印时间,包括时区(应告诉您最新动态)。
这样的事情对你有用:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
$ENV{'TZ'} = 'UTC';
Time::Piece::_tzset();
my ($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime(time);
$wday = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')[$wday];
$year += 1900;
$mon++;
printf "$year/$mon/$mday %02d:%02d:%02d ", $hour, $min, $sec;
print "\n";
$ENV{'TZ'} = 'UTC-8';
Time::Piece::_tzset();
($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime(time);
$wday = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')[$wday];
$year += 1900;
$mon++;
printf "$year/$mon/$mday %02d:%02d:%02d ", $hour, $min, $sec;
$ENV{'TZ'} = 'PST8PDT';
Time::Piece::_tzset();
($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime(time);
$wday = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')[$wday];
$year += 1900;
$mon++;
printf "$year/$mon/$mday %02d:%02d:%02d ", $hour, $min, $sec;
(是的,我知道导入Time::Piece
但没有使用它 - 我想尽可能多地重现原始代码)。