Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

garbage at end of string in strptime

by manorhce (Beadle)
on Feb 15, 2013 at 13:34 UTC ( #1018892=perlquestion: print w/ replies, xml ) Need Help??
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 }

Comment on garbage at end of string in strptime
Download 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 15, 2013 at 18:21 UTC

    As I have mentioned this error is coming for Time::Piece module line no 470 this is cpan module

      As I have mentioned this error is coming for Time::Piece module line no 470 this is cpan module

      And then what?

      Read How do I post a question effectively?

      And post your code , code you wrote that uses "this cpan module"

      Copy/pasting from "this cpan module" is the last thing you should do

Re: garbage at end of string in strptime
by aitap (Deacon) 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 (Pilgrim) 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 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 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);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018892]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-07-29 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (215 votes), past polls