Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate a "yyyy-MM-dd'T'HH:mm:ssZ" date/timestamp in UTC with Perl?

Code would be nice but a point in the right direction is good as well.

CPAN? RegEx?

I've seen both ways

"yyyy-MM-dd'T'HH:mm:ssZ";

"yyyy-MM-ddTHH:mm:ssZ";

like image 810
Phill Pafford Avatar asked Dec 08 '22 04:12

Phill Pafford


1 Answers

Ether is definitely on the right track with DateTime. Using DateTime, you can be sure that you have a time that actually exists, where something on Feb 29, 2000 might get by if you wrote the checks yourself.

Your format looks like an ISO8601 string. So, use DateTime::Format::ISO8601 to do your 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 could use other format modules, such as D::F::Strptime, but you will wind up recreating what ISO8601 formatter already does.

like image 127
daotoad Avatar answered Dec 09 '22 17:12

daotoad