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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |