Bijan
Bijan

Reputation: 8594

Why do I get "Error Parsing Time" when I use the format string "%FT%T" with Time::Piece->strptime?

I am trying to convert 2015-09-11T04:00:00 to a Time::Piece object. I tried:

my $date = "2015-09-11T04:00:00";
my $t = Time::Piece->strptime($date, '%FT%T');
print $t->strftime('%F %T');

But I get Error Parsing Time. I think it is because I am looking for %FT%T and this is causing issues because of the spacing. How would I fix this?

Upvotes: 5

Views: 5136

Answers (2)

Matt Jacob
Matt Jacob

Reputation: 6553

Have you considered ditching Time::Piece for something else? DateTime, for example, does what it says on the tin:

use strict;
use warnings;

use DateTime::Format::Strptime;

my $date = '2015-09-11T04:00:00';
my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T');
my $dt = $strp->parse_datetime($date);

print $dt->datetime;  # prints 2015-09-11T04:00:00

Upvotes: 2

melpomene
melpomene

Reputation: 85767

Time::Piece seems to have some bugs in this area. In particular, it thinks that strptime %T is equivalent to %B %e, unlike what its documentation claims.

strftime %F and %T don't work for me either, but that may be because I'm on Windows.

Sticking to standard format specifications works fine, though:

my $date = "2015-09-11T04:00:00";
my $t = Time::Piece->strptime($date, '%Y-%m-%dT%H:%M:%S');
print $t->strftime('%Y-%m-%d %H:%M:%S'), "\n";

Upvotes: 6

Related Questions