Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix to Perl - system date display 1

Status
Not open for further replies.

chibaru

Programmer
Aug 24, 2005
13
US
Hi !!
I'm converting a Unix script to Perl on Sun Solaris. The script does message logging with date and time as each step is completed. The time is displayed and the seconds change.

The Unix print looks like this:
print "$(date +'%D %T') Step $STEP completed."

The log looks like this:
07/30/06 17:05:55 Step STEP00 completed.
07/30/06 17:05:56 Step STEP01 completed.
07/30/06 17:05:56 Step STEP02 completed.
07/30/06 17:05:56 Step STEP03 completed.
07/30/06 17:05:57 Step STEP04 completed.
07/30/06 17:05:57 Step STEP05 completed.
07/30/06 17:05:57 Step STEP06 completed.
07/30/06 17:05:58 Step STEP07 completed.

To do the same display, I have to do this before every print to show the changing seconds:

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon = $mon + 1;
$year = $year - 100;
my $date = sprintf "%02i/%02i/%02i", $mon, $mday, $year;
my $time = sprintf "%02i:%02i:%02i", $hour, $min, $sec;
print "$date $time Step $STEP completed. \n";

Is there an easier way? It seems so much to do just to display date and time.

Thank you..
 
Code:
use POSIX qw(strftime);

print strftime "%m/%d/%y %H:%M:%S Step $STEP completed. \n", localtime(time);
 
Thank you so much!! It works great, even after a 10 second pause!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top