Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   To change the time stamp format... (http://www.velocityreviews.com/forums/t891630-to-change-the-time-stamp-format.html)

clearguy02@yahoo.com 04-02-2005 05:01 PM

To change the time stamp format...
 
Hi experts,

I need a function in my perl program that converts the given time stamp
to a specific time stamp format.

example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00

Can some one tell me how to do this?

Thanks,
John


Felix Geerinckx 04-02-2005 05:19 PM

Re: To change the time stamp format...
 
On 02/04/2005, clearguy02@yahoo.com wrote:

> example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
>
> Can some one tell me how to do this?


use split with /[-:.]/ to get the individual date parts
use a hash to translate month abbreviations to month numbers
use sprintf to format your target date

--
felix

Gunnar Hjalmarsson 04-02-2005 05:27 PM

Re: To change the time stamp format...
 
clearguy02@yahoo.com wrote:
> I need a function in my perl program that converts the given time stamp
> to a specific time stamp format.
>
> example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
>
> Can some one tell me how to do this?


There are two approaches:

1) You can search for an applicable module at http://search.cpan.org/

2) You can write the function yourself. Useful built-in Perl functions
may be:

perldoc -f split
perldoc -f sprintf

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

clearguy02@yahoo.com 04-02-2005 06:04 PM

Re: To change the time stamp format...
 
Thanks Felix,

Do you have any example code to do this?

Thanks,
John


Gunnar Hjalmarsson 04-02-2005 06:59 PM

Re: To change the time stamp format...
 
[ Please give some context when you reply to a message. See the posting
guidelines for this group at
http://mail.augustmail.com/~tadmc/cl...uidelines.html ]

clearguy02@yahoo.com wrote:
> Felix Geerinckx wrote:
>>>
>>> example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
>>>
>>> Can some one tell me how to do this?

>>
>> use split with /[-:.]/ to get the individual date parts
>> use a hash to translate month abbreviations to month numbers
>> use sprintf to format your target date

>
> Thanks Felix,
>
> Do you have any example code to do this?


Felix probably has code in his collection of Perl code snippets that
does exactly the convertion you need, and it was most likely through an
oversight he didn't post it along with the above hints.

If I'm wrong in my assumption, you may need to look up those parts of
the Perl documentation and give it a try yourself. Scaring thought, I
know, but what can you do...

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

John W. Krahn 04-02-2005 10:30 PM

Re: To change the time stamp format...
 
clearguy02@yahoo.com wrote:
>
> I need a function in my perl program that converts the given time stamp
> to a specific time stamp format.
>
> example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
>
> Can some one tell me how to do this?



my $date = '15-Aug-04.19:03';

my %months = qw(
Jan 1
Feb 2
Mar 3
Apr 4
May 5
Jun 6
Jul 7
Aug 8
Sep 9
Oct 10
Nov 11
Dec 12
);

my $mon_lookup = join '|', keys %months;

if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {

printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1, $4, $5;

}




John
--
use Perl;
program
fulfillment

A. Sinan Unur 04-02-2005 10:37 PM

Re: To change the time stamp format...
 
"John W. Krahn" <someone@example.com> wrote in
news:C2F3e.127630$ZO2.8180@edtnps84:

> clearguy02@yahoo.com wrote:
>>
>> I need a function in my perl program that converts the given time
>> stamp to a specific time stamp format.
>>
>> example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
>>
>> Can some one tell me how to do this?

>
>


> my $mon_lookup = join '|', keys %months;
>
> if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {
>
> printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1,
> $4, $5;


Ooops! How about that Y2.1K bug?!

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Tad McClellan 04-02-2005 10:46 PM

Re: To change the time stamp format...
 
clearguy02@yahoo.com <clearguy02@yahoo.com> wrote:

> Thanks Felix,
>
> Do you have any example code to do this?



Post the code that you have tried so far, and we will help you fix it.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

John W. Krahn 04-03-2005 12:19 AM

Re: To change the time stamp format...
 
A. Sinan Unur wrote:
> "John W. Krahn" <someone@example.com> wrote in
> news:C2F3e.127630$ZO2.8180@edtnps84:
>>
>>if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {
>>
>> printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1,
>> $4, $5;

>
> Ooops! How about that Y2.1K bug?!


Is that thing still around? :-)


John
--
use Perl;
program
fulfillment

A. Sinan Unur 04-03-2005 12:34 AM

Re: To change the time stamp format...
 
"John W. Krahn" <someone@example.com> wrote in news:EEG3e.127836$ZO2.94802
@edtnps84:

> A. Sinan Unur wrote:
>> "John W. Krahn" <someone@example.com> wrote in
>> news:C2F3e.127630$ZO2.8180@edtnps84:
>>>
>>>if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {
>>>
>>> printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1,
>>> $4, $5;

>>
>> Ooops! How about that Y2.1K bug?!

>
> Is that thing still around? :-)


Yeah, it seems like a new version is released every century :)

Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html


All times are GMT. The time now is 06:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.