Re: about timestamp reformat

From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 03/22/04


Date: Mon, 22 Mar 2004 19:35:00 GMT

janet wrote:
> I want to change the format from following
>
> a|b|c|Jul 14 2000 4:56:00:000PM|d|e|f|Jul 14 2000
> 4:56:00:000PM|h|I|j
>
> to
>
> a|b|c|2000-07-14-16:56:00:000000|d|e|f|2000-07-14-16:56:00:000000|h|I|j
>
> "|" is the seperator in the text, and the timestamp could be
> different values and could happen at any place..
>
> Is there any way to process the file easily?

There are a few ways. Assuming the whole file is in $text, this is one
approach:

     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);

     $text =~ s{\|(\w{3})\s+(\d{1,2})\s+(\d{4})\s+
                (\d{1,2})(:\d{2}:\d{2}:)(\d{3})(\w{2})\|}
           { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
             $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '|' }egx;

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl