To change the time stamp format...

C

clearguy02

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
 
F

Felix Geerinckx

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
 
G

Gunnar Hjalmarsson

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
 
G

Gunnar Hjalmarsson

[ Please give some context when you reply to a message. See the posting
guidelines for this group at
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html ]

Felix said:
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...
 
J

John W. Krahn

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
 
A

A. Sinan Unur

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
 
B

Brian McCauley

A. Sinan Unur said:
Yeah, it seems like a new version is released every century :)

Indeed, I always wondered why people insisted on calling the centuary
bug 'the millenium bug' or the 'Y2K bug'.

But it's really more often than that. There are all sort of ways that
various software encodes dates internally. Each of these can introduce
its own problems.

There was one on 9th September 2001. (10**9 seconds since 1970).

Of course there's the famous 'Unix' one on 19th January 2038 (2**31
seconds since 1970).

There's a lesser known one one a couple of years earlier (2**32 seconds
since 1900).

There's even one in 2010 that some users of systems written in the MUMPS
programing language may hit.
 
D

Doron Nissimi

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

Try Date::Manip
Simple, and powerful.
 
J

John W. Kennedy

Brian said:
Indeed, I always wondered why people insisted on calling the centuary
bug 'the millenium bug' or the 'Y2K bug'.

But it's really more often than that. There are all sort of ways that
various software encodes dates internally. Each of these can introduce
its own problems.

There was one on 9th September 2001. (10**9 seconds since 1970).

Of course there's the famous 'Unix' one on 19th January 2038 (2**31
seconds since 1970).

There's a lesser known one one a couple of years earlier (2**32 seconds
since 1900).

There's even one in 2010 that some users of systems written in the MUMPS
programing language may hit.

IBM mainframes started to have "keep forever" files deleted on August
16th, 1972, which was 9999 days before 2000-01-01.

(There was also a beta version of OS/VS1 that crashed every 24 hours at
exactly midnight, GMT, with a zero-divide error in the nucleus, because
one of the programmers who wrote the date/time code was aware that 1900
was not a leap year, and another one wasn't.)

--
John W. Kennedy
"The pathetic hope that the White House will turn a Caligula into a
Marcus Aurelius is as naïve as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top