(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


In reply to Re: How to format timestamps M/D/Y h:mm:ss AMPM (e.g., 4/8/2013 6:09:03 PM)? by Jim
in thread How to format timestamps M/D/Y h:mm:ss AMPM (e.g., 4/8/2013 6:09:03 PM)? by Jim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.