Search code examples
perldatetimeparsingstrptime

Why does strptime from Time::Piece not parse my format?


My collegue (who has left the company) has written a bunch of scripts, including batch and Perl scripts, and I'm getting rid of the regional settings dependencies.

In the last Perl script, he's written the following piece of code:

my $format = "%d.%m.%Y %H:%M";
my $today_converted = Time::Piece->strptime($today, $format) - ONE_HOUR - ONE_HOUR - ONE_HOUR - ONE_HOUR - ONE_HOUR; 

(the idea is to get five hours before midnight of that particular date)

The value of $today seems to be "03/04/2017" (which stands for the third of April (European dateformat)), which seems not to be understood by Time::Piece implementation:

Error parsing time at C:/Perl64/lib/Time/Piece.pm line 481.

Which format can I use which is understood by Time::Piece Perl implementation?


Solution

  • In the format you have dots . as the date delimiter, but in the data you have slashes /. That's why it doesn't parse. It needs an exact match.