Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Epoch time in milliseconds: Is there a better way?

by logan (Curate)
on Nov 25, 2008 at 06:08 UTC ( #725771=note: print w/replies, xml ) Need Help??

in reply to Re: Epoch time in milliseconds: Is there a better way?
in thread Epoch time in milliseconds: Is there a better way?

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.

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

Replies are listed 'Best First'.
Re^3: Epoch time in milliseconds: Is there a better way?
by Xilman (Friar) on Nov 25, 2008 at 12:31 UTC
    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.
      It's true, I don't actually need real accuracy for the script to work, but my feeling is that having an accurate timestamp can't hurt. Also, I may want/need to use the data at some point in the future so I might as well do it right the first time.

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

Re^3: Epoch time in milliseconds: Is there a better way?
by GrandFather (Sage) on Nov 25, 2008 at 06:42 UTC

    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://725771]
[Corion]: usemodperl: Why don't you set up your own (http-only) CPAN mirror? Or just fatpack your scripts? I wonder what problem you're trying to solve here.
[usemodperl]: don't worry about that, it's really cool, i promise!
[Corion]: usemodperl: Well, if the world changes and makes your "wonderful" approach not work anymore, you can either change your approach, or change the world. You seem to want to change others instead.
[usemodperl]: no dude i'm just asking how to access meta via http and you are getting defensive for some reason (did you turn off http at meta? :-)
[choroba]: why do you need to access metacpan via http?
[usemodperl]: stop asking why, just say how
usemodperl pulls teeth...
[usemodperl]: change the world? you mean BREAK THE WEB? we will regret this once google starts pulling certs for cursing or something...
Veltro Sees open mouth and feeds troll
[Corion]: usemodperl: Naah, you're just being unreasonable. It's not hard to download https URLs, and we've shown you ways to avoid downloading at all, and ways to keep downloading via http possible. You reject both.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-06-24 16:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.