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

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

I'm working on a project that demands I pass the time since the epoch in milliseconds. Getting the value was simply a matter of using Time::HiRes::gettimeofday. Now I've got the seconds since the epoch plus the extra microseconds. My understanding was that the function would return the milliseconds, but the value returned has six digits, not three. That quirk notwithstanding, my main issue is that my method of joining the two values together into a single number seems inelegant and I wonder if I'm missing something. Here's my code:
use Time::HiRes qw(gettimeofday); $one = gettimeofday; print STDOUT "one = $one\n"; ($sec,$milli) = gettimeofday; print STDOUT "sec = $sec, milli = $milli\n"; $milli2 = sprintf("%.*s", 3, $milli); print STDOUT "sec = $sec, milli = $milli, milli2 = $milli2\n"; $time = join ('',$sec,$milli2); print STDOUT "time = $time\n"; Output: one = 1227582343.55258 sec = 1227582343, milli = 552702 sec = 1227582343, milli = 552702, milli2 = 552 time = 1227582343552
It seems to me that I'm using three lines of code to do something that should be fairly basic and that using sprintf to trim microseconds into milliseconds is a complete hack. Moreover, the docs for gettimeofday() continually refer to the value returned as "milliseconds", which implies a three-digit number (thousandths of a second). I suspect that there's a better way to do this but I'm kind of stumped.

-Logan
"What do I want? I'm an American. I want more."

Replies are listed 'Best First'.
Re: Epoch time in milliseconds: Is there a better way?
by GrandFather (Saint) on Nov 25, 2008 at 03:37 UTC

    gettimeofday () returns seconds and microseconds (not milliseconds) in array context. The documentation is not at all ambiguous about that.

    What use are you putting the result to? Why not use the scalar context floating point result instead of a nasty string concatenation that will fail in nasty ways about 1/10th of the time?


    Perl reduces RSI - it saves typing
      Yup, microseconds. You are correct, sir. I will endeavor to more closely RTFM.

      What use are you putting the result to?

      What I'm doing is simulating a browser transaction and one of the required parameters is epoch time in milliseconds. I don't actually need the result to be millisecond accurate. I just need to pass a 13-digit number with no decimals as one of the parameters in the request. While millisecond accuracy would be nice, all I really need is for the number to be the right number of digits and for the value to be accurate to within a minute or two.

      Why not use the scalar context floating point result instead of a nasty string concatenation that will fail in nasty ways about 1/10th of the time?

      Ah! I think I get it:

      use Time::HiRes qw(gettimeofday); my $timestamp = int (gettimeofday * 1000); print STDOUT "timestamp = $timestamp\n"; exit; Output: timestamp = 1227593060768
      One line, no sprintf, and it's definitely a number so a potential future s/printf won't choke.

      -Logan
      "What do I want? I'm an American. I want more."

        If all you want is "minute or two" accuracy, you don't need a hires timer. Just set the milliseconds portion of your string to '000'. You are still two orders of magnitude better accuracy than the spec calls for.

        Or if you don't need the the time stamp later you could:

        print STDOUT "timestamp = ", int (gettimeofday * 1000), "\n";

        Perl reduces RSI - it saves typing