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:

Dave

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


In reply to Re: printing date on a file by davido
in thread printing date on a file by chuleto1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.