在 Perl 中格式化 DateTime 字段

Formatting a DateTime field in Perl

我正在尝试使用字符串日期,并将其格式化为正确输出为“%Y%m%d_%H%M”。除了那一天,一切都输出正确。它实际上在第二天返回,我不知道为什么会这样。下面是代码和输出。

my $currentTime = strftime("%Y%m%d_%H%M\n", gmtime(time));
my $hashTime    = strftime("%Y%m%d_%H%M\n", gmtime(UnixDate($user->{'add_date'}, "%s")));

$self->Print($user->{'add_date'} ."\n". $currentTime . "\n" . $hashTime);

输出:

2016-12-02 20:35:43 # Date from the Database 
20161202_2046  # Current GMTime
20161203_0235  # This should be 20161202_2035?

怎么输出的是03?

好的,所以我明白了。对于好奇的人,新代码如下。

my $currentTime = strftime("%Y%m%d_%H%M\n", gmtime(time));
my $hashTime    = UnixDate($user->{'add_date'}, "%Y%m%d_%H%M");

$self->Print($user->{'add_date'} ."\n". $currentTime . "\n" . $hashTime);

如您所知,在 $hashTime 下,当使用 UnixDate() 时,您不需要使用 strftime,因为它已经按照您需要的方式进行格式化。