I have a function that gives me a formatted timestamp string when I need it. It will format a value I pass, or if I don't pass anything to it, it uses the current time.
sub _timestamp {
my $t = shift; # allow me to pass in a time
$t = time unless $t; # use the current time if none specified
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti
+me($t);
my $timestamp = sprintf("%04u-%02u-%02u %02u:%02u:%02u",
$year + 1900, $mon + 1, $mday, $hour, $min, $sec);
return $timestamp;
}
All you have to do is change the format to however you like. You can then use the returned string as part of a filename, or as I use it, to prefix entries in my log files.
Cheers,
Akoya |