在AIX中将date转换为unix时间戳

时间:2015-12-14 12:26:22

标签: perl unix awk aix

我有几个日期被转换为像那个月和&日期:

20151213 20151214

在AIX中似乎date -d无法识别-d选项

date -d 20151213 +%s

所以我无法将我动态接收的特定日期格式转换为Unix时间戳 如何将AIX中的这些日期转换为时间戳?

2 个答案:

答案 0 :(得分:3)

您可以将Perl与Time::Piece模块一起使用来简洁地执行此操作

use strict;
use warnings 'all';
use feature 'say';

use Time::Piece;

for ( qw/ 20151213 20151214 / ) {
    my $time = Time::Piece->strptime($_, '%Y%m%d')->epoch;
    say $time;
}

输出

1449964800
1450051200

答案 1 :(得分:2)

Perl的Time::Local module就是您所需要的:

perl -e '
    use Time::Local;
    for $d ("20151213", "20151214") {
        ($year, $month, $day) = $d =~ /(\d{4})(\d{2})(\d{2})/;
        $epoch = timelocal(0,0,0, $day, $month-1, $year-1900);
        print "$d => $epoch\n"
    }
'
20151213 => 1449982800
20151214 => 1450069200

在shell脚本中,我写了

epoch() {
    echo "$1" | perl -MTime::Local -lne '
        ($year, $month, $day) = /(\d{4})(\d{2})(\d{2})/;
        print timelocal(0, 0, 0, $day, $month-1, $year-1900)
    '
}

timestamp=$(epoch 20151213)