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

Hi there Monks!
I updated Time::Piece to version 1.29 and now it doesnt work anymore. It is breaking with an error message as:
Error parsing time at ../Time/Piece.pm line 469.

Has anyone encountered the same issue and a fix for such a bug?
Test code
perl -MPOSIX=strftime -MTime::Piece -E ' say strftime q{%FT%T}, localt +ime; Time::Piece->strptime( strftime(q{%FT%T}, localtime), q{%FT%T}); +' 2015-04-28T14:10:47 Error parsing time at /usr/local/lib64/perl5/Time/Piece.pm line 469.

Thanks for looking!

Replies are listed 'Best First'.
Re: Error parsing using Time::Piece
by kcott (Archbishop) on Apr 28, 2015 at 18:40 UTC

    Updating Time::Piece to 1.29 is not the problem. I get the same error under 1.27.

    $ perl -MTime::Piece -E 'say $Time::Piece::VERSION' 1.27 $ perl -MPOSIX=strftime -MTime::Piece -E ' say strftime q{%FT%T}, loca +ltime; Time::Piece->strptime( strftime(q{%FT%T}, localtime), q{%FT%T} +);' 2015-04-29T04:18:23 Error parsing time at /Users/ken/perl5/perlbrew/perls/perl-5.20.2t/lib +/5.20.2/darwin-thread-multi-2level/Time/Piece.pm line 469.

    The error message includes a pathname to the file and a line number. Had you viewed that file and gone to line 469, you would have seen that line is in sub strptime and, just a few lines above, you see sub strftime (sound familiar?).

    Looking at the Time::Piece documentation, you'll see that Time::Piece::strftime is the "same as POSIX::strftime (without the overhead of the full POSIX extension)".

    To fix, the easiest would be to remove the POSIX references. If, for some reason, that's not possible (I've no idea what your real code looks like), you'll need to be specific about which strftime you mean, i.e. POSIX::strftime or Time::Piece::strftime.

    -- Ken

      This is the line the gives me the error:
      ... my $date = Time::Piece->strptime("32015", "%m%Y");

        You will need to add a leading zero to that:

        my $date = Time::Piece->strptime("032015", "%m%Y");

        Maybe you could use sprintf?

        perl -MTime::Piece -le'print Time::Piece->strptime( sprintf( "%06d", " +32015"), "%m%Y" )'

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Error parsing using Time::Piece
by jeffa (Bishop) on Apr 28, 2015 at 18:43 UTC

    Dealing with these parsing errors can be very painful. It seems to me that %F is not being respected. Consider the following:

    $ perl -MTime::Piece -le 'print Time::Piece->strptime( "2015-04-28T11: +33:12", "%Y-%m-%dT%H:%M:%S" )' Tue Apr 28 11:33:12 2015 $ perl -MTime::Piece -le 'print Time::Piece->strptime( "2015-04-28T11: +33:12", "%Y-%m-%dT%T" )' Tue Apr 28 11:33:12 2015 $ perl -MTime::Piece -le 'print Time::Piece->strptime( "2015-04-28T11: +33:12", "%FT%H:%M:%S" )' Error parsing time ...

    Why don't you change your format string to "%Y-%m-%dT%T" and see if that works? (It did for me using Time::Piece v1.29.)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Error parsing using Time::Piece
by hippo (Archbishop) on Apr 28, 2015 at 18:45 UTC

    I see the same with version 1.20_01, so it's nothing new.

    $ perl -MTime::Piece -E 'say Time::Piece->strptime("2015-04-28T19:40:0 +0", q{%Y-%m-%dT%T})->strftime;' Tue, 28 Apr 2015 19:40:00 UTC $ perl -MTime::Piece -E 'say Time::Piece->strptime("2015-04-28T19:40:0 +0", q{%FT%T})->strftime;' Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469.

    If you avoid %F for now that should serve as a workaround.