Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to format timestamps M/D/Y h:mm:ss AMPM (e.g., 4/8/2013 6:09:03 PM)?

by Jim (Curate)
on May 13, 2013 at 19:51 UTC ( #1033345=perlquestion: print w/ replies, xml ) Need Help??
Jim has asked for the wisdom of the Perl Monks concerning the following question:

I'm doing this…

# Remove leading zeros from months, days and hours, # but not from minutes or seconds... $timestamp =~ s{(?<!:)0(?=[1-9][/:])}{}g;

…to convert timestamps from this format…

    04/08/2013 06:09:03 PM

…into this format…

    4/8/2013 6:09:03 PM

…after having already converted them from this format…

    20130408 18:09:03

…using DateTime::Format::Strptime. I'd obviously prefer to specify a DateTime::Format format that does the whole job in one fell swoop. Is there a DateTime::Format module that accomplishes this directly? Am I making this more difficult than I should be? Am I missing something?

Here's a complete script that demonstrates what I'm currently doing:

#!perl use v5.14; use strict; use warnings; use DateTime; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d %H:%M:%S' ); my $format = '%m/%d/%Y %r'; while (my $timestamp = <DATA>) { my $dt = $parser->parse_datetime($timestamp); $timestamp = $dt->strftime($format); # Remove leading zeros from months, days and hours, # but not from minutes or seconds... $timestamp =~ s{(?<!:)0(?=[1-9][/:])}{}g; say $timestamp; } exit 0; __DATA__ 20040805 18:31:00 20050106 10:54:27 20050302 01:23:35 20100808 20:00:16 20110501 18:09:44 20110909 20:02:42 20130408 18:09:03

Thanks!

Jim

UPDATE: I discovered $DateTime::format_cldr() soon after posting this inquiry. It seems to work! I'd appreciate suggestions for improvements in any case.

Here's the revised demonstration script:

#!perl use v5.14; use strict; use warnings; use DateTime; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d %H:%M:%S' ); my $pattern = 'M/d/y h:mm:ss a'; while (my $timestamp = <DATA>) { my $dt = $parser->parse_datetime($timestamp); $timestamp = $dt->DateTime::format_cldr($pattern); say $timestamp; } exit 0; __DATA__ 20040805 18:31:00 20050106 10:54:27 20050302 01:23:35 20100808 20:00:16 20110501 18:09:44 20110909 20:02:42 20130408 18:09:03

Comment on How to format timestamps M/D/Y h:mm:ss AMPM (e.g., 4/8/2013 6:09:03 PM)?
Select or Download Code
Re: How to format timestamps M/D/Y h:mm:ss AMPM (e.g., 4/8/2013 6:09:03 PM)?
by Jim (Curate) on May 14, 2013 at 04:07 UTC

    (I'm replying to my own post because I believe I've found the best general solution to my problem.)

    Once I'd figured out that I could use DateTime::format_cldr() to render the timestamps in the required format, it seemed incongruous to be parsing them using DateTime::Format::Strptime. The new carpet made me realize I needed to replace the drapes. I was doing this…

            pattern => '%Y%m%d %H:%M:%S',
    

    …and this…

        my $pattern = 'M/d/y h:mm:ss a';
    

    …in two successive statements, which is inelegant. So I decided to use CLDR to parse the timestamps as well as to format them. Doing this neatens and tightens the code.

    #!perl use v5.14; use strict; use warnings; use DateTime; use DateTime::Format::CLDR; my $parser = DateTime::Format::CLDR->new( pattern => 'yyyyMMdd HH:mm:ss', on_error => 'croak', ); my $format = 'M/d/y h:mm:ss a'; while (my $timestamp = <DATA>) { chomp $timestamp; $timestamp = $parser->parse_datetime($timestamp) ->format_cldr($format); say $timestamp; } exit 0; __DATA__ 20040805 18:31:00 20050106 10:54:27 20050302 01:23:35 20100808 20:00:16 20110501 18:09:44 20110909 20:02:42 20130408 18:09:03

    This prints…

    8/5/2004 6:31:00 PM
    1/6/2005 10:54:27 AM
    3/2/2005 1:23:35 AM
    8/8/2010 8:00:16 PM
    5/1/2011 6:09:44 PM
    9/9/2011 8:02:42 PM
    4/8/2013 6:09:03 PM
    

    Jim

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-08-01 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls