如何在 Perl 中将 ISO8601 时间戳转换为 unix 时间?

发布于 2024-09-07 02:48:05 字数 194 浏览 1 评论 0原文

给定一个表示 ISO8601 格式的日期/时间的字符串(例如 20100723T073000),我最终需要使用通用 strftime 格式字符串将其解析为用户提供的格式。为此,我需要将 ISO8601 时间戳转换为 Unix 时间戳。 Perl 有大量的日期/时间操作模块,我有点不知所措。关于如何执行此操作有什么建议吗?

Given a string that represents a date/time in ISO8601 format (e.g. 20100723T073000), I need to ultimately parse this into a user-supplied format using a general strftime format string. In order to do that, I need to convert the ISO8601 timestamp to a Unix timestamp. There are a huge amount of date/time manipulation modules for Perl and I'm a little overwhelmed. Any suggestions on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水微香 2024-09-14 02:48:20

如果您没有或不想安装 DateTime::Format::ISO8601 模块,您可以使用 HTTP::Date 解析至少 ISO8601 的一个子集>:

$ perl -MHTTP::Date -wle 'print str2time("2013-11-30T23:05:03")'
1385845503

与任何日期处理一样,请注意时区问题。

If you don't have or don't want to install the DateTime::Format::ISO8601 module, you can parse at least a subset of ISO8601 with HTTP::Date:

$ perl -MHTTP::Date -wle 'print str2time("2013-11-30T23:05:03")'
1385845503

As with any date handling, be cautious about time zone issues.

小帐篷 2024-09-14 02:48:18

用于处理日期、时间、持续时间、时区和格式的冠军模块是 DateTime。获得 DateTime 对象后,您可以对其执行大量操作,例如添加/减去其他 DateTime 对象或 DateTime::Duration 对象,或以其他格式打印出来。

use strict; use warnings;
use DateTime;
use DateTime::Format::ISO8601;

my $dt = DateTime::Format::ISO8601->parse_datetime('20100723T073000');

print $dt->strftime('%F %T');

打印:“2010-07-23 07:30:00”

The champion module for handling dates, times, durations, timezones and formats is DateTime. Once you've got your DateTime object, you can perform a huge number of operations on it, like adding/subtracting other DateTime objects or DateTime::Duration objects, or printing it out in another format.

use strict; use warnings;
use DateTime;
use DateTime::Format::ISO8601;

my $dt = DateTime::Format::ISO8601->parse_datetime('20100723T073000');

print $dt->strftime('%F %T');

prints: "2010-07-23 07:30:00"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文