http://www.perlmonks.org?node_id=203537


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.

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