0

I am running into issues converting the date Apr 9 2017 3:45:00:000AM to 2017-04-09 03:45:00

Here's what i have tried.

use DateTime::Format::Strptime;
my $strp = DateTime::Format::Strptime->new(pattern   => '%h %d %Y %H:%M',);
$start_date = $strp->parse_datetime('Apr  9 2017  3:45:00:000AM');

prints 2017-04-09T03:45:00 and not 2017-04-09 03:45:00. Trying to get 24 hour clock so when i switch AM to PM the same time prints.

7
  • The code you provided doesn't print anything. You mention AM/PM, but your example is with 3:45 AM, meaning that it is identical with 12-hour and 24-hour format, which makes your question a bit unclear. Could you add the code that does the print (it sounds like you just added an extra T in there...), and use 3:45 PM rather than 3:45 AM as your example please?
    – Dada
    Nov 13, 2020 at 16:15
  • Also, I assume that the final :000 before AM as milliseconds; is that right? If so, will they always be 000, or can they be an arbitrary 3-digit number? (the answer will not be the same in both cases)
    – Dada
    Nov 13, 2020 at 16:17
  • @Dada that is my code. It is a copy paste and it prints I forgot to add my $start_date; But the code is what I am using and it prints 2017-04-09T03:45:00. So I am not sure why it's not printing for you. yes the :000 is miliseconds. Yes I used AM but if you swap AM to PM i get the same result. Which I could expect to get 2017-04-09T15:45:00 for PM. What I am trying to produce is 2017-04-09 03:45:00 for am and 2017-04-09 15:45:00 for PM. Nov 13, 2020 at 19:56
  • 1/ Your code does not contain a print, nor any function that prints anything. Please provide your actual code. 2/ You did not answer my question regarding milliseconds: are they always 000 or not?
    – Dada
    Nov 13, 2020 at 22:58
  • print "$start_date"; and no they are not always 000. Nov 14, 2020 at 0:32

3 Answers 3

4

Update   See end for an strptime pattern to parse the shown input string format correctly


What is shown works, and so the DateTime::Format::Strptime constructor returns a DateTime object. But when an object is simply printed then one gets the stringification that is shown.

So you need to format it for printing as desired. A general way is with strftime

say $start_date->strftime("%F %T");

where both %F and %T are shorthands, see strftime patterns

Or combine the basic ymd and hms methods

say $start_date->ymd('-') . ' ' . $start_date->hms(':');

See the docs and adjust if/as needed. I didn't understand some details.


It works only by accident in this exact example, since the pattern given to use for parsing in new is wrong for the shown input format, and is also inconsistent with stated requirements

  • The shown pattern doesn't have a format specifier for the seconds, nor for the milliseconds that follow it, nor for the following AM/PM -- all expected in the input string. So in general an input in the shown form cannot be parsed correctly with the shown pattern

  • The %H matches 00-23 hour, so not 12-hour clock which is stated as expected and is implicit by the presence of AM. (It still matches a 12-hour-time number but it won't once the missing AM/PM format specifier is added.)

The pattern in the OP works and parses the given input correctly because 03:45... happens to be in AM, and the module uses regex to match the given pattern anywhere in the given string (by default), so %H:%M matches 03:45 and the rest of the input string doesn't matter. If we turn on strict in the constructor this'll fail. See documentation.

Assuming that the shown input is the correct part we'd need

my $strp = DateTime::Format::Strptime->new(
    pattern => '%h %d %Y %I:%M:%S%3N%p'
);

Here %I is for 12-hour clock (1-12), added %S is for seconds and %3N for milliseconds (see the page in docs for patterns, linked above), and %p for AM/PM.

The rest then works as it stands, along with printing in a desired format given above.

1

Following code demonstrates how desired output can be achieved without any modules

use strict;
use warnings;
use feature 'say';

my $date   = 'Apr  9 2017  3:45:00:000AM';
my @fields = qw/month mday year hour min sec usec/;
my %months = ( Jan => 1,  Feb => 2,  Mar => 3,
               Apr => 4,  May => 5,  Jun => 6, 
               Jul => 7,  Aug => 8,  Sep => 9, 
               Oct => 10, Nov => 11, Dec => 12
            );
my %parts;

@parts{@fields} = split "[ :]+", $date;

$parts{hour} += 12 if $parts{usec} =~ /PM/;
$parts{month} = $months{ $parts{month} };

printf "%04d-%02d-%02d %02d:%02d:%02d\n", 
       @parts{qw/year month mday hour min sec/};

Perl code with assistance of module

use strict;
use warnings;
use feature 'say';

use DateTime::Format::DateParse;
 
my($date, $dt);

$date = 'Apr  9 2017  3:45:00:000AM';
$date =~ s/:(\d{3}[AP]M)/.$1/;

$dt = DateTime::Format::DateParse->parse_datetime( $date );

$date = $dt;
$date =~ s/T/ /;

say $date;
1

There are two issues with your code. First, the pattern you use to parse your date is not correct: %H is used for a 24-hour format hour. Instead, you should use a combination of %i (12-hour) and %p (AM/PM). Second, to print a DateTime object, you should first format it first (using ->strftime or ->ymd() for instance).

The milliseconds in the date are, however, a bit of an issue because strptime does not have a option to match milliseconds. I suggest to first remove them from your date, and only then parse the date with strptime:

use DateTime::Format::Strptime;

my $date = 'Apr  9 2017  3:45:00:505PM';

# Removing milliseconds from date
$date =~ s/:(\d{3})(?=AM|PM)//; 
my $milliseconds = $1;

my $strp = DateTime::Format::Strptime->new(pattern => '%h %d %Y %I:%M:%S%p',);
my $start_date = $strp->parse_datetime($date);

# Taking into account the milliseconds that were removed earlier
$start_date->add(seconds => 1) if $milliseconds > 500;


say $start_date->strftime("%F %T");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.