0

Is there an accepted way to represent high resolution timestamps in JSON and/or JavaScript?

Ideally, I would like it to support at least 100 ns resolution, since that would make the server code a bit simpler (since the .NET DateTime resolution is 100 ns as well).

I found a lot of questions dealing with manipulating high-resolution timers and such (which is not possible, apparently), but I simply need to represent it somehow, in an application API.

This is for an API built in REST style using JSON, so actually measuring time in this resolution is not required. However, I would like to transfer and use (potentially in JavaScript) the timestamp in its full resolution (100 ns, since this is .NET).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon Lindgren
  • 500
  • 1
  • 6
  • 14

2 Answers2

3

In light of your clarification, isn't your timestamp just a really big integer? The current timestamp is 1329826212. If you want nanosecond precision, we are just talking about like 9 more digits: 1329826212000000000. That is a number that JavaScript can easily handle. Just send it over as:

myobject = {
    "time": 1329826212000000000
}

It should be perfectly fine. I just tried doing some arithmetic operations on it, division by a large number and multiplication by the same. There is no loss of value.

JavaScript supports a huge range of floating point numbers, but guarantees integral accuracy from -2^53 to 2^53. So I think you are good to go.

UPDATE I'm sorry, I just re-read your question. You wish to represent it? Well, one thing I can think of is to extract the last 9 digits (additional precision beyond second granularity) and show them to be the decimal part of the number. You may even wish to extract the last 6 digis (additional precision beyond the millisecond granularity).

Rohan Prabhu
  • 7,180
  • 5
  • 37
  • 71
  • Thanks, I was thinking of using a plain old integer, but wasn't sure if there was some commonly agreed solution already. Integers will do fine :) – Simon Lindgren Feb 21 '12 at 12:04
2

The JavaScript Date object only has millisecond precision.

However, if you are just looking for a standard format to encode nanosecond precision times, an ISO 8601 format string will allow you to define nanoseconds as fractions of seconds:

Decimal fractions may also be added to any of the three time elements. A decimal point, either a comma or a dot (without any preference as stated most recently in resolution 10 of the 22nd General Conference CGPM in 2003), is used as a separator between the time element and its fraction. A fraction may only be added to the lowest order time element in the representation. To denote "14 hours, 30 and one half minutes", do not include a seconds figure. Represent it as "14:30,5", "1430,5", "14:30.5", or "1430.5". There is no limit on the number of decimal places for the decimal fraction. However, the number of decimal places needs to be agreed to by the communicating parties.


There is a trick used by jsperf.com and benchmarkjs.com that uses a small Java applet that exposes Java's nanosecond timer.

See Stack Overflow question Is there any way to get current time in nanoseconds using JavaScript?.

Community
  • 1
  • 1
Sam Greenhalgh
  • 5,952
  • 21
  • 37
  • 1
    This does not answer the question, since I only need to transfer the timestamp, not actually measure anything in JavaScript. Clarified the question as well. – Simon Lindgren Feb 21 '12 at 09:46
  • 1
    ISO8601 format is a good solution. I'll probably go with integers though, since it seems to be a bit simpler to use and in this case human-readability is not important. – Simon Lindgren Feb 21 '12 at 12:11