Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Obtaining The Exact Time

by sauoq (Abbot)
on Oct 08, 2002 at 00:19 UTC ( #203537=note: print w/replies, xml ) Need Help??

in reply to Obtaining The Exact Time

Need to capture the exact date/time the connection is made because a file will be created with the date/combo as part of the filename.

Using Time::HiRes to ensure unique filenames is probably overkill. It would be easier and cleaner to simply increment a counter for each connection created within the same second. I would use something like:

{ my ($t,$c); sub unique_timestamp { my $time = time; $c++; $c = 0 unless $time == $t; $t = $time; my ($s,$m,$h,$D,$M,$Y) = (localtime($time))[0..5]; sprintf "%04d%02d%02d%02d%02d%02d%03d", $Y+1900, $M+1, $D, $h, $m, $s, $c; } } # To see that work... for (1..100) { print unique_timestamp, "\n"; sleep rand 2; }

If you could have more than 1000 connections in a second, you may wish to increase that %03d in the sprintf to %04d or something. This code avoids the use of a module and parsing out fractional seconds. It also results in sequential unique stamps that are the same length.

"My two cents aren't worth a dime.";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://203537]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2020-11-29 10:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found