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 ar开发者_运维百科e a huge amount of date/time manipulation modules for Perl and I'm a little overwhelmed. Any suggestions on how to do this?
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"
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.
精彩评论