Record current time





0
Date Submitted Mon. Oct. 9th, 2006 1:14 PM
Revision 1 of 1
Beginner robert
Tags "record time" | day | hour | localtime | minute | Perl
Comments 3 comments
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.

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";
}
 

Robert Pena

www.penacentral.com

Comments

Comments An easier way
Wed. Oct. 11th, 2006 5:57 PM    Helper jeremec
  Comments Easier
Thu. Oct. 12th, 2006 4:06 PM    Beginner robert
Comments Update to this snippet
Tue. Oct. 10th, 2006 11:51 AM    Beginner robert

Voting