Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: garbage at end of string in strptime

by manorhce (Beadle)
on Feb 16, 2013 at 07:33 UTC ( #1019017=note: print w/ replies, xml ) Need Help??


in reply to garbage at end of string in strptime

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);


Comment on Re: garbage at end of string in strptime
Download Code
Replies are listed 'Best First'.
Re^2: garbage at end of string in strptime
by Khen1950fx (Canon) on Feb 16, 2013 at 12:56 UTC
    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: note [id://1019017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2015-07-31 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls