如何在solaris中将时间格式转换为秒

时间:2013-09-13 09:22:28

标签: perl shell scripting opensolaris

我有格式2013-04-29 08:17:58的时间,我需要将其转换为秒。

在UNIX中,我们可以使用date +%s。无论如何在Solaris中都可以这样做吗?

2 个答案:

答案 0 :(得分:3)

使用Time::Piece。它自9.5版以来一直是标准Perl 5发行版的一部分,不需要安装。

use strict;
use warnings;
use 5.010;

use Time::Piece;

my $t = '2013-04-29 08:17:58';

$t = Time::Piece->strptime($t, '%Y-%m-%d %H:%M:%S');

say $t->epoch;

<强>输出

1367223478

答案 1 :(得分:2)

付出更多的努力,你可以用你可怕的Perl版本做到这一点。

#!/usr/bin/perl

use strict;
use warnings;
use Time::Local;

my $str = '2013-04-29 08:17:58';
my @dt  = split /[- :]/, $str;

$dt[0] -= 1900;
$dt[1] -= 1;

print timelocal reverse @dt;
自Perl 5首次发布(1994年)以来,

Time::Local已包含在Perl中。

尽你所能让你的古老版本的Perl更新。

更新:对此进行一些投票。但没有人愿意解释原因。