# Help with a script..

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hi all,

I have the below script ready to change a time stamp from
15-Sep-04.01:03 to 2004-09-15 01:03:00.

===================================
$date = '15-Sep-04.01:03'; %months = qw(January 1 February 2 March 3 April 4 May 5 June 6 July 7 August 8 September 9 October 10 November 11 December 12);$month_lookup = join '|', keys %months;
if ( $date =~ /(\d+)-($month_lookup)-(\d+)\.(\d+):(\d+)/ )
{
$finalDate = sprintf ("20%02d-%02d-%02d %02d:%02d:00\n",$3,
$months,$1, $4,$5);
}
print "$finalDate"; ==================================== Now my problem is that the$date variable might have 15-Sep-04.01:03 or
15-Sept-04.01:03 or 15-September-04.01:03 (month will have a range from
minumum first three chanracters to the whole month name).

then how can I make sure that I can translate either Sep or Sept or
September in to 09 in the final output?

John.

## Re: Help with a script..

clearguy02@yahoo.com wrote:
> I have the below script ready to change a time stamp from
> 15-Sep-04.01:03 to 2004-09-15 01:03:00.

> ===================================
> $date = '15-Sep-04.01:03'; > %months = qw(January 1 February 2 March 3 April 4 May 5 June 6 July 7 > August 8 September 9 October 10 November 11 December 12); > >$month_lookup = join '|', keys %months;
> if ( $date =~ /(\d+)-($month_lookup)-(\d+)\.(\d+):(\d+)/ )
> {
>  $finalDate = sprintf ("20%02d-%02d-%02d %02d:%02d:00\n",$3,
> $months,$1, $4,$5);
> }
> print "$finalDate"; > ==================================== > Now my problem is that the$date variable might have 15-Sep-04.01:03 or
> 15-Sept-04.01:03 or 15-September-04.01:03 (month will have a range from
> minumum first three chanracters to the whole month name).

> then how can I make sure that I can translate either Sep or Sept or
> September in to 09 in the final output?

Well, you could just expand the hash %months...

$date = '15-Sep-04.01:03'; %months = qw(Jan 1 January 1 enero 1 Feb 02 Mar 03 Apr 04 May 05 Jun 06 Jul 07 Aug 8 Sep 9 Sept 9 September 9 Oct 10 Nov 11 Dec 12); if ($date =~ /(\d+)-(\w+)-(\d+)\.(\d+):(\d+)/
&& (my $mon =$months)) {
$finalDate = sprintf ("20%02d-%02d-%02d %02d:%02d:00\n",$3, $mon,$1, $4,$5);
}
print "$finalDate"; Or without changing the hash, use the first three characters of the month supplied and reply on your data to be correct and not have, e.g. 'Sepulchre' present where the name of a month should occur, use something like... if ($date =~ /(\d+)-(\w+)-(\d+)\.(\d+):(\d+)/
&& (my $mon =$months{substr($2, 0, 3)})) { Axel ## Re: Help with a script.. clearguy02@yahoo.com wrote in news:1112576321.475302.71500 @z14g2000cwz.googlegroups.com: >$finalDate = sprintf ("20%02d-%02d-%02d %02d:%02d:00\n", $3, >$months, $1,$4, $5); As I noted before, there is a Y2.1K bug in there. When doing the right thing is just as easy as doing the wrong thing, why not do the right thing? > then how can I make sure that I can translate either Sep or Sept or > September in to 09 in the final output? What have you tried? Sinan -- (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html ## Re: Help with a script.. On 04/04/2005, A. Sinan Unur wrote: > clearguy02@yahoo.com wrote in news:1112576321.475302.71500 > @z14g2000cwz.googlegroups.com: > > >$finalDate = sprintf ("20%02d-%02d-%02d %02d:%02d:00\n", $3, > >$months, $1,$4, $5); > > As I noted before, there is a Y2.1K bug in there. When doing the > right thing is just as easy as doing the wrong thing, why not do the > right thing? > It's more than a Y2.1K bug:$shortyear = '04';

could mean any of

$longyear = '1904';$longyear = '2004';
$longyear = '2104'; etc. So doing the right thing is not as easy as some years ago, when we said e.g.$longyear = ($shortyear gt '50' ? '19' : '20') .$shortyear;

--
felix

## Re: Help with a script..

A. Sinan Unur wrote:
> clearguy02@yahoo.com wrote in news:1112576321.475302.71500
>>
>>  $finalDate = sprintf ("20%02d-%02d-%02d %02d:%02d:00\n",$3,
>> $months,$1, $4,$5);
>
> As I noted before, there is a Y2.1K bug in there. When doing the right
> thing is just as easy as doing the wrong thing, why not do the right
> thing?

What's "the right thing" when converting a 'DD-Mon-YY' date string?

If it's a bug at all, isn't it rather a Y(-)x.yK bug?

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

## Re: Help with a script..

> A. Sinan Unur wrote:
>> clearguy02@yahoo.com wrote in news:1112576321.475302.71500
>>>
>>>  $finalDate = sprintf ("20%02d-%02d-%02d %02d:%02d:00\n",$3,
>>> $months,$1, $4,$5);
>>
>> As I noted before, there is a Y2.1K bug in there. When doing the
>> right thing is just as easy as doing the wrong thing, why not do the
>> right thing?
>
> What's "the right thing" when converting a 'DD-Mon-YY' date string?

"The right thing", it seems to me, would be to add the number of years
to 2000 as a rule. In this particular case, with two-digit years the OP
knows to be in this century, it probably does not matter, but IMHO, it
is a good habit to get into.

Sinan

--
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:

## Re: Help with a script..

A. Sinan Unur wrote:
> Gunnar Hjalmarsson wrote:
>> What's "the right thing" when converting a 'DD-Mon-YY' date string?
>
> "The right thing", it seems to me, would be to add the number of years
> to 2000 as a rule. In this particular case, with two-digit years the OP
> knows to be in this century, it probably does not matter, but IMHO, it
> is a good habit to get into.

I'm still not with you.

Aren't you mixing it up with how you should treat the sixth element
returned by localtime() or gmtime() in list context? One typical Y2K bug
in Perl programs was:

my $year = (localtime)[5];$year = "19$year"; while it should have been:$year = $year+1900; But the sixth element never was a two-digit year, even if it was treated as such in many programs. When converting two-digit years to four-digit ditto you *always* need to know in which century you are, don't you? -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl ## Re: Help with a script.. > A. Sinan Unur wrote: >> Gunnar Hjalmarsson wrote: >>> What's "the right thing" when converting a 'DD-Mon-YY' date string? >> >> "The right thing", it seems to me, would be to add the number of >> years to 2000 as a rule. In this particular case, with two-digit >> years the OP knows to be in this century, it probably does not >> matter, but IMHO, it is a good habit to get into. > > I'm still not with you. > > Aren't you mixing it up with how you should treat the sixth element > returned by localtime() or gmtime() in list context? I am not mixing it up, but that is what I am thinking of. > When converting two-digit years to four-digit ditto you *always* > need to know in which century you are, don't you? Yes, and in this case, the OP knows which century the data are from. However, the string concatenation approach, which he copied and pasted, again, IMHO, obscures that you are adding years to a base century (if that makes sense). Mine is just a warning against getting into the habit of string concatenation. Since the OP was using printf anyway, why not use # log files were generated after 2001 printf "%4.4d", 2000 +$two_digit_year;

Anyway, this is probably not wasting any more bits over. I see your point,
and my attitude is appropriately softened on the issue.

Sinan

--
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:

## Re: Help with a script..

> When converting two-digit years to four-digit
> ditto you *always* need to know in which century you are, don't you?

In one interpretation, you need to know the current century and
the other century that is within plus-or-minus 50 years from now.

31-Dec-99 = 1999 for 1949 through 2048.
31-Dec-99 = 2099 for 2049 through 2148.
1-Jan-70 = 1970 for 1920 through 2019.
1-Jan-70 = 2070 for 2020 through 2119.

Rules like the above are definitely application dependent.
-Joe

## Re: Help with a script..

comp.lang.perl.misc:
> Hi all,
>
> I have the below script ready to change a time stamp from
> 15-Sep-04.01:03 to 2004-09-15 01:03:00.

<snip>

> Now my problem is that the $date variable might have > 15-Sep-04.01:03 or 15-Sept-04.01:03 or 15-September-04.01:03 > (month will have a range from minumum first three chanracters > to the whole month name). > > then how can I make sure that I can translate either Sep or > Sept or September in to 09 in the final output? The Date::Parse module on CPAN do what you want. Your original format [close enough to] one of the accepted formats for the str2time() function:$fmt_date = strftime( "...", localtime( str2time \$date_str )

Also look at the docs for the POSIX module, mainly strftime().

Brandan L.
--
bclennox \at eos \dot ncsu \dot edu