Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-25 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found