http://qs321.pair.com?node_id=292519

chuleto1 has asked for the wisdom of the Perl Monks concerning the following question:

Sup, I am creating a file within my perl script (for metrics purposes). I would like to print the date to that file everytime a command within the scipt is executed. The date would preferably will include milliseconds
(i.e)
# BLAh BLAh print OUT date #one system call print OUT date #another system call
Regards

Replies are listed 'Best First'.
Re: printing date on a file
by davido (Cardinal) on Sep 18, 2003 at 21:19 UTC

    If you don't require millisecond resolution, you can just do this:

    print OUT scalar localtime,"\n";

    But that only provides 'to the second' resolution. The nice thing is that it is preformatted in a well-recognized fashion.

    If you require millisecond resolution you may use the Time::HiRes module. Specifically, with the time() function of that module you can obtain the time of day in floating point seconds since epoch. You'll then have to run it through the localtime function, and insert back into the 'localtime' time stamp the decimal portion to convert the output of time() to a human understandable format.

    Time::HiRes relies on certain functions being available in your systems OS. If they're not there, it will fail.

    UPDATE: Here is an example using Time::HiRes

    #!/usr/bin/perl use strict; use warnings; use Time::HiRes qw( time ); open OUT, ">>timelog" or die "Can't open time log.\n$!"; my $hightime = time; my $timestamp = localtime $hightime; my $msec = substr $hightime - int $hightime, 1; $timestamp =~ s/(:\d+)\s/$1$msec /; print OUT $timestamp, "\n"; close OUT;

    This snippet doesn't implement file locking. I presume that you've already got that part figured out. The snippet works as follows:

    • First, startup the Time::HiRes module and import the module's version of the time() function, as a drop-in replacement for Perl's standard time() function.
    • Open the file.
    • Grab the current time with tons of accuracy.
    • Turn the current time into a familiar timestamp, but with only whole seconds, not decimal seconds.
    • Extract the decimal portion of the string, and trim off the leading zero.
    • Insert the decimal portion (including decimal point) into the appropriate part of $timestamp.
    • Print the timestamp to OUT.
    • Close OUT.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: printing date on a file
by Paladin (Vicar) on Sep 18, 2003 at 21:08 UTC
    Time::HiRes provides a gettimeofday() function that returns the epoch time with milliseconds as well.
    use Time::HiRes qw( gettimeofday ); ($seconds, $microseconds) = gettimeofday;
Re: printing date on a file
by graff (Chancellor) on Feb 24, 2004 at 04:59 UTC
    You might want to scan down the man page for the perl debugger ("perldoc perldebug") to the section about "The Perl Profiler". If you put each of your system calls into its own little perl subroutine, then run the script as follows:
    perl -d:DProf your_script.pl
    the script will do what it normally does, and while it's doing that, the perl interpreter will be saving tons of trace and timing information to a file in your current directory. After the run is over, you can get a summary that profiles the time spent in each subroutine (i.e. running each system call) by running this utility that comes as part of the Perl distribution:
    dprofpp tmon.out
    (where "tmon.out" is the name that the debugger's profiling module assigns to the trace file that it creates, as explained in the perldebug man page).