Record current time
0
robert
This perl function will output to a file of your choosing the current localtime as defined by perl's localtime function. It will output the date in ISO 8601 date format plus the current localtime such as:
2006-08-21 09:26:35 am
The function also returns the localtime without the date to the calling environment.
2006-08-21 09:26:35 am
The function also returns the localtime without the date to the calling environment.
sub recordTime {
open(TIME, ">>path/to/a/text/file.txt")
or die "Could not open file.txt to append record time: " . $!;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
my $am_pm = "am";
if ($hour > 12) {
$am_pm = "pm";
$hour = $hour - 12;
}
printf TIME "%4d-%02d-%02d %02d:%02d:%02d %s\n",
$year+1900,$mon+1,$mday,$hour,$min,$sec,$am_pm;
close(TIME);
# append a '0' to minutes without double digits
return $min<10 ? $hour . ":0$min $am_pm" :
$hour . ":$min $am_pm";
}





sub recordTime {
use POSIX qw(strftime);
my $time = strftime("%Y-%m-%d %T %p", localtime());
open(TIME, ">>path/to/a/text/file.txt")
or die "Could not open file.txt to append record time: " . $!;
print TIME $time;
close(TIME);
return $time;
}
my $time = strftime("%Y-%m-%d %I:%M:%S %p", localtime());
worked the way I wanted. %T didn't seem to output anything, but I guess it is not support under Windows. Either way, I wanted to display in 12-hour mode.
I noticed that the snippet above is not really correct. In the test for $hour > 12, there should also be a test for $sec > 0 to be as complete as possible. So the test should be if($hour > 12 && $sec > 0).