格式化日期时间戳

时间:2015-07-23 07:32:14

标签: perl perl-module perl-data-structures

我得到了perl gmtime(epoch)的输出,如下所示。

Thu Jul 23 07:19:52 2015

我想使用perl将其修改为如下所示的目录名称。什么是最好的方式?

2015072307

2 个答案:

答案 0 :(得分:3)

一种方法是使用Time::Piece,它位于Perl核心*。

use strict;
use warnings;
use feature 'say';
use Time::Piece;

my $t = gmtime(time);
say $t->strftime('%Y%m%d%H');

输出

2015072307

作为旁注,我喜欢http://www.strftime.net/因为我永远不会记住所有的格式。

*您可以使用the utility corelist这样查看某些内容是否属于核心:corelist Time::Piece - 将为我提供Time::Piece was first released with perl v5.9.5

答案 1 :(得分:0)

我让它以这种方式工作。

use POSIX qw(strftime);
my $dir_name = strftime "%Y%m%d%H", gmtime ($epoch);