在Perl中将日期转换为纪元的最佳方法

时间:2020-03-28 06:06:16

标签: perl date time epoch

将-> 3月11日10:29:47 2021 GMT <-转换为多个平台上的时代的最好方法是什么。 默认情况下,Date :: Parse模块并非在所有平台上都可用。

1 个答案:

答案 0 :(得分:3)

使用Time::Piece中的strptime()(“字符串解析时间”)方法,该方法自2007年的5.10.0开始成为标准Perl发行版的一部分。

#!/usr/bin/perl

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

use Time::Piece;

my $str = 'Mar 11 10:29:47 2021 GMT';
my $fmt = '%b %d %H:%M:%S %Y %Z';

my $date = Time::Piece->strptime($str, $fmt);

say $date->epoch; # prints 1615458587

strptime是一种标准的Unix工具,可以以多种形式使用。您可以从manual page中获取有关其使用的格式字符串的更多信息。

相关问题