http://www.perlmonks.org?node_id=1018892

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

Hi, I am getting issue with Time::Piece module the error is like garbage at end of string in strptime: 2 at /usr/local/lib/perl/5.8.8/Time/Piece.pm line 470. garbage at end of string in strptime: 2 at /usr/local/lib/perl/5.8.8/Time/Piece.pm line 470. garbage at end of string in strptime: 2 at /usr/local/lib/perl/5.8.8/Time/Piece.pm line 470.

Inside the module the 470 line is like below

466 sub strptime { 467 my $time = shift; 468 my $string = shift; 469 my $format = @_ ? shift(@_) : "%a, %d %b %Y %H:%M:%S %Z"; 470 my @vals = _strptime($string, $format); 471 # warn(sprintf("got vals: %d-%d-%d %d:%d:%d\n", reverse(@vals)) +); 472 return scalar $time->_mktime(\@vals, (ref($time) ? $time->[c_i +slocal] : 0)); 473 }

Replies are listed 'Best First'.
Re: garbage at end of string in strptime
by Khen1950fx (Canon) on Feb 16, 2013 at 03:39 UTC
    The error message is telling you exactly what the error is: you have an extra 2 that it can't format. For example, an example with garbage 2:
    #!/usr/bin/perl -l use strict; use warnings; use Time::Piece; my $time = Time::Piece->strptime( "2013-02-15 19:07:292", "%Y-%m-%d %H:%M:%S"); print $time;
    Now without the extra 2:
    #!/usr/bin/perl -l use strict; use warnings; use Time::Piece; my $time = Time::Piece->strptime( "2013-02-15 19:07:29", "%Y-%m-%d %H:%M:%S"); print $time;
    Does that help? If not, please post your code, and we'll go from there.
Re: garbage at end of string in strptime
by aitap (Curate) on Feb 15, 2013 at 19:41 UTC
    Well, perhaps the problem is with the $string you supplied to the strptime sub as the 2nd argument? It's hard to tell while you didn't provide any of your code.
    Sorry if my advice was wrong.
Re: garbage at end of string in strptime
by fishmonger (Chaplain) on Feb 15, 2013 at 19:43 UTC

    I suspect that the problem is with the data you're passing to strptime() not the sub itself.

    Can you post your code?

Re: garbage at end of string in strptime
by Anonymous Monk on Feb 15, 2013 at 17:56 UTC

    Inside the module the 470 line is like below

    Sorry, but that doesn't help. We need code to reproduce the error message, not code where the error occurs

Re: garbage at end of string in strptime
by manorhce (Beadle) on Feb 16, 2013 at 07:33 UTC

    Apologies for the delay of code. My code is like

    my $last_executed = $time_file . ".txt"; return 1 unless (-e $last_executed); open(IN, '<' . $last_executed); my $time = <IN>; close(IN); my $valid_time = $time; return 1 if ( $valid_time !~ m/(\d{4})-(\d{2})-(\d{2})(?:T)?(\d{2}):(\ +d{2})/gi ); my ($year, $month, $day, $hour, $min) = $time =~ m#(\d{4})-(\d{2})-(\d +{2})(?:T)?(\d{2}):(\d{2})#gi; my $dt1 = DateTime->new ( year => $year, month => $month, day => $day, +hour => $hour, minute => $min); my $time2 = localtime->datetime; my ($year2, $month2, $day2, $hour2, $min2) = $time2 =~ m#(\d{4})-(\d{2 +})-(\d{2})(?:T)?(\d{2}):(\d{2})#gi; my $dt2 = DateTime->new ( year => $year2, month => $month2, day => $da +y2,hour => $hour2, minute => $min2);
      What happened to Time::Piece:-). In general, it would be better to use a DateTime::Format::Strptime class like this:
      #!/usr/bin/perl -l use strict; use warnings; use DateTime::Format::Strptime qw(); my $format = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %H:%M:%S', time_zone => 'local', on_error => 'croak', ); my $ts1 = '2012-02-15 00:00:00'; my $dt1 = $format->parse_datetime($ts1); $dt1->add( hours => 3, minutes => 56, seconds => 53, ); print $format->format_datetime($dt1); my $ts2 = '2012-02-15 00:00:00';; my $dt2 = $format->parse_datetime($ts2); $dt2->add( years => 1, hours => 4, minutes => 27, seconds => 25, ); print $format->format_datetime($dt2);
A reply falls below the community's threshold of quality. You may see it by logging in.