" />
Home > Perl, Programming > sub-routine: gettime();

sub-routine: gettime();

Over the years working as a systems administrator I have a large sub-routine collection on my belt. One that I use quite frequently is gettime();. This is a simple routine someone in my group wrote to return a nicely formatted timestamp. It is use mostly for logging and such.

sub gettime () {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $year += 1900;
    $mon += 1;
    my $retval = sprintf("$year\-%02d\-%02d %02d:%02d:%02d",$mon,$mday,$hour,$min,$sec);
    return $retval;
}
 

Example:

#!/usr/bin/perl
print gettime(),"\n";
 

returns:

2009-05-27 07:18:43
 
Categories: Perl, Programming Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.