Operating System - Linux
1753924 Members
9213 Online
108810 Solutions
New Discussion юеВ

Re: perl localtime to get format

 
Ratzie
Super Advisor

perl localtime to get format

I would like to use the perl localtime to display the format
YYMMDDHHMMSS
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: perl localtime to get format

Hi:

Use the POSIX::strftime module. THe directives are the same as the libc ones.

# perl -MPOSIX=strftime -e 'print strftime("%Y%m%d%H%M%S\n",localtime)'

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: perl localtime to get format

#!/usr/bin/perl -w


sub do_1_timestamp
{
use integer;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($_[0]);
my $s_tmp = sprintf("%02d%02d%02d%02d%02d%02d",
($year + 1900) % 100,$mon + 1,$mday,$hour,$min,$sec);
return($s_tmp);
} # do_1_timestamp


print "Current Time is ",do_1_timestamp(time()),"\n";


If it ain't broke, I can fix that.
Ratzie
Super Advisor

Re: perl localtime to get format

First time I am not using perl, and has saved me code.

chomp( my $dispdate = `date +%Y-%m-%dT%X.000` );
James R. Ferguson
Acclaimed Contributor

Re: perl localtime to get format

Hi (again):

The downside of your own solution is that you are invoking the shell to run the 'date' command. This adds the creation of an unnecessary process to do what Perl can do directly.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: perl localtime to get format

... and to add injury to insult, your use of the external date command means that your can only format the current date and time rather than any date and time such as the modification time of a file, the date 3 hours from now, ... which would all simply be a different argument to the function I gave you.
If it ain't broke, I can fix that.
Ratzie
Super Advisor

Re: perl localtime to get format

OK OK, Perl still rules.
I know I was going to open up a can of worms with this one.