From this answer, I’m using DateTime::Format::Strptime to grab the date from a string.
I’m using the following pattern: %m%d%Y%n%T. This should be matching:
%m: The month number (01-12). This will parse single digit numbers as well.%d: The day of month (01-31). This will parse single digit numbers as well.%Y: The year, including century (for example, 1991).%n: Arbitrary whitespace.%T: Equivalent to%H:%M:%S:%H: The hour (00-23). This will parse single digit numbers as well.%M: The minute (00-59). This will parse single digit numbers as well.%S: The second (0-60; 60 may occur for leap seconds. SeeDateTime::LeapSecond).
However, on input: [05/18/2011 14:14:05] it’s failing with the error:
Your datetime does not match your pattern
As far as I can see, my pattern matches the input pattern. Where am I going wrong?
Relevant code is below:
use DateTime::Format::Strptime qw( );
my $format = DateTime::Format::Strptime->new(
pattern => '%m%d%Y%n%T',
time_zone => 'local',
on_error => 'croak',
);
my $dt = $fields->[1] ;
print "Date:[$dt]\n";
my $dateopen = $format->parse_datetime($dt);
Output:
Date:[05/18/2011 14:14:05]
Your datetime does not match your pattern
The non-pattern characters are important as well. You have slashes between your date components, hence the pattern
'%m/%d/%Y%n%T'works while yours doesn’t.Note that the pattern
%Dwould not work either as it is a shortcut for%m%d%Yand doesn’t include the slashes either.