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

In reply to 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.