Can I get more accurate time than new Date()->getTime() in node.js

2,140 views
Skip to first unread message

jiabin xiong

unread,
Mar 5, 2012, 12:57:45 AM3/5/12
to nod...@googlegroups.com


--
Hello,World!
熊佳斌(totty)

jiabin xiong

unread,
Mar 5, 2012, 7:12:37 AM3/5/12
to nod...@googlegroups.com
Can get micro time via  this module . https://github.com/wadey/node-microtime 
works fine.


On Mon, Mar 5, 2012 at 1:57 PM, jiabin xiong <totty...@gmail.com> wrote:


--
Hello,World!
熊佳斌(totty)



--
Hello,World!
熊佳斌(totty)

Bert Belder

unread,
Mar 5, 2012, 7:37:47 AM3/5/12
to nodejs
Creating an addon that provides an uv_hrtime() binding would be a good
cross-platform solution for this.

Ben Noordhuis

unread,
Mar 5, 2012, 8:51:35 AM3/5/12
to nod...@googlegroups.com
On Mon, Mar 5, 2012 at 13:37, Bert Belder <bertb...@gmail.com> wrote:
> Creating an addon that provides an uv_hrtime() binding would be a good
> cross-platform solution for this.

I don't mind adding a `process.hrtime()` function, it would be useful
for benchmarks. The problem is that uv_hrtime() returns a 64 bits
unsigned integer, while JS numbers only have 53 bits of precision.

Dean Landolt

unread,
Mar 5, 2012, 9:32:38 AM3/5/12
to nod...@googlegroups.com
But a tuple of 2 JS numbers has 106. This wouldn't be the prettiest API but it would get the job done.

Micheil Smith

unread,
Mar 5, 2012, 9:43:33 AM3/5/12
to nod...@googlegroups.com
Yeah, you could do something like [seconds, microseconds], such that you
could have precision on your values.

– Micheil

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Matt

unread,
Mar 5, 2012, 9:46:40 AM3/5/12
to nod...@googlegroups.com
Alternatively, maybe just not worry about it, since 2**53 microseconds isn't hit until 2255. I hope to not be using Node then :-)

Dean Landolt

unread,
Mar 5, 2012, 9:51:47 AM3/5/12
to nod...@googlegroups.com
On Mon, Mar 5, 2012 at 9:46 AM, Matt <hel...@gmail.com> wrote:
Alternatively, maybe just not worry about it, since 2**53 microseconds isn't hit until 2255. I hope to not be using Node then :-)

Heh. I like Matt's idea better :)

Ben Noordhuis

unread,
Mar 5, 2012, 10:15:29 AM3/5/12
to nod...@googlegroups.com

uv_hrtime() has nanosecond resolution and it's not necessarily a
timestamp, just an arbitrary monotonically increasing counter. But
yes, a tuple would work. Anyone feel like doing a pull request?

Jorge

unread,
Mar 5, 2012, 10:26:46 AM3/5/12
to nod...@googlegroups.com

You could return it % 2^53: 2^53*1e-9/60/60/24 === 104 days

Or an object: {
mod2_53: (ts.tv_sec * NANOSEC + ts.tv_nsec) % 2^53
sec: ts.tv_sec
nsec: ts.tv_nsec }
--
Jorge.

Nathan Rajlich

unread,
Mar 5, 2012, 11:46:16 AM3/5/12
to nod...@googlegroups.com
I'm working on the patch. Up in a sec.

Nathan Rajlich

unread,
Mar 5, 2012, 12:01:55 PM3/5/12
to nod...@googlegroups.com

billywhizz

unread,
Mar 5, 2012, 12:12:39 PM3/5/12
to nod...@googlegroups.com
*applause*
Reply all
Reply to author
Forward
0 new messages