Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Time::Piece->strptime formatting throws: Error parsing time . . .

by hesco (Deacon)
on Sep 24, 2014 at 00:05 UTC ( [id://1101725]=perlquestion: print w/replies, xml ) Need Help??

hesco has asked for the wisdom of the Perl Monks concerning the following question:

I'm getting this error:

# perl test.pl Error parsing time at /usr/lib/perl/5.14/Time/Piece.pm line 469.

where test.pl reads:

#!/usr/bin/env perl use strict; use warnings; use Time::Piece; my $dt = 'Fri, 20 Sep 2024 01:14:03 UTC'; my $format = '%a, %d %b %Y %T %Z'; print Time::Piece->strptime( $dt, $format ); exit;

Fully reading the `man strptime` documentation for the formatting codes has given me no clues as to why this might be. Reading that line number in the underlying Time::Piece module, as well as previous experience tells me that I have an issue where my $format does not match my $dt somehow. But various experiments have failed to stumble across a solution. Can anyone here with experience with this method of Time::Piece please elucidate the path forward?

All clues appreciated, particularly ones which point me to documentation which might help me avoid this frustration in the future.

Thanks,

-- Hugh


UPDATE:

Thank you so much toolic and Wallah. That was the ticket. I tested that after stripping off the time zone from the dates and the format and moved past that error.

And to the Anonymous Monk who pointed out a portable version of these methods: thank you as well, though your solution was not appropriate for this implementation as I am writing a data validation script for a puppet module, for which one of my design criteria has been that I stick to perl core and not introduce any unnecessary additional dependencies which could not reasonably be expected to reside on the target server anyway. Gave me an opportunity to learn about corelist and Module::CoreList, which I had not previously known about.

if( $lal && $lol ) { $life++; }
if( $insurance->rationing() ) { $people->die(); }

Replies are listed 'Best First'.
Re: Time::Piece->strptime formatting throws: Error parsing time . . .
by NetWallah (Canon) on Sep 24, 2014 at 00:59 UTC
    Confirming toolic's observation that it is the attempt at providing the time zone (UTC) that throws off Time::Piece (tested on T::P ver 1.20_1).

    It works fine if you leave out the TZ.

    The error seems to be outside the perl module, when it calls an external strftime function.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: Time::Piece->strptime formatting throws: Error parsing time . . .
by toolic (Bishop) on Sep 24, 2014 at 00:40 UTC
    I get different output. I also show my version:
    garbage at end of string in strptime: UTC at ..../linux/x86_64/5u5/lib +/perl5/5.12.2/x86_64-linux/Time/Piece.pm line 470. Fri Sep 20 01:14:03 2024 perl -MTime::Piece -le 'print $Time::Piece::VERSION' 1.15_01
Re: Time::Piece->strptime formatting throws: Error parsing time . . . (split %Z)
by Anonymous Monk on Sep 24, 2014 at 00:44 UTC

    yeah, strftime/strptime isn't identically portable ... I've always had issue with %Z on win32 ... but DateTime's strftime/strptime is portable

    #!/usr/bin/perl -- use strict; use warnings; use Time::Piece; my $dt = 'Fri, 20 Sep 2024 01:14:03 UTC'; my $format = '%a, %d %b %Y %T %Z'; #~ print Time::Piece->strptime( $dt, $format ); my @date = split ' ', $dt; my @format = split ' ', $format; for my $ix ( 0 .. $#date ){ print "$ix ## ", "$date[$ix] == $format[$ix]\n"; print "$ix ## ", eval { Time::Piece->strptime( $date[$ix], $format[$ix] ); } || $@, "\n"; } exit; __END__ 0 ## Fri, == %a, 0 ## Thu Jan 1 00:00:00 1970 1 ## 20 == %d 1 ## Tue Jan 20 00:00:00 1970 2 ## Sep == %b 2 ## Tue Sep 1 00:00:00 1970 3 ## 2024 == %Y 3 ## Mon Jan 1 00:00:00 2024 4 ## 01:14:03 == %T 4 ## Thu Jan 1 01:14:03 1970 5 ## UTC == %Z 5 ## Error parsing time at C:/citrusperl/site/lib/Time/Piece.pm +line 469.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1101725]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found