Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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."


In reply to Epoch time in milliseconds: Is there a better way? by logan

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (7)
    As of 2014-08-27 10:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (236 votes), past polls