How can I check the date / label "yyyy-MM-dd'THH: mm: ssZ" in UTC using Perl?

The code would be good, but a point in the right direction is also good.

CPAN? RegEx?

I saw both ways

"YYYY-MM-dd'T'HH: mm: SSZ";

"YYYY-MM-DDThh: mm: SSZ";

+3
source share
2 answers

Air is definitely on the right track with DateTime. Using DateTime, you can be sure that you have a time that actually exists when something on February 29, 2000 can pass if you wrote the checks yourself.

Your format looks like an ISO8601 string. So use DateTime :: Format :: ISO8601 to do the parsing.

use DateTime; use DateTime::Format::ISO8601; my $string = '2010-02-28T15:21:33Z'; my $dt = DateTime::Format::ISO8601->parse_datetime( $string ); die "Impossible time" unless $dt; 

You can use other format modules, such as D :: F :: Strptime , but you will finish re-creating what the formats ISO8601 already does.

+11
source

Depending on what you are doing, you can force your string to a DateTime object, for example:

 use DateTime::Format::MySQL; my $dt = DateTime::Format::MySQL->parse_datetime( '2003-01-16 23:12:01' ); 

Then you can easily display your temporary string in a different format, perform calculations with it, etc.

You did not specify what the string generates in this particular format, but for a large number of input sources, there are DateTime :: Format :: modules.

+4
source

All Articles