swedstar
swedstar

Reputation: 101

Is it safe to rely on milliseconds when calculating dates?

In java you can call the getTime() method on the Date object to represent a date in milliseconds since January 1, 1970 00:00:00 GMT. Is this representation universal across other programming language APIs?

For example, if I ask somebody who is using an API from a different programming language to give me current date and time represented in milliseconds, can I safely make the assumption that I would end up with the same date and time if I calculate this value on the server-side using java.

The reason I'm asking is because I'm building a public API over http where I want the client to provide me with a timestamp which I need to process server-side. My question really is whether it's safe to ask for a date representation in the form of milliseconds since January 1, 1970 00:00:00 GMT, rather than a full string representation such as yyyy-MM-dd'T'HH:mm:ss.SSS'Z

Upvotes: 2

Views: 405

Answers (4)

Stephen C
Stephen C

Reputation: 719238

You cannot in general assume that every numeric timestamps uses January 1, 1970 as the baseline. But it doesn't really matter, because you could specify your API as requiring timestamps in that form. It is a trivial matter to adjust the baseline and / or scale a numeric timestamp to the form you require.

So looking at the alternatives:

  • Numeric timestamps take marginally less space in messages, and are marginally easier and cheaper to convert. However, there is a risk of someone mis-implementing your specification by using the wrong scaling and/or baseline.

  • A standard (e.g. ISO) textual format uses (marginally) more space and is marginally harder to convert, but standard parser / unparser implementations exist. (There is still the risk that someone will use a variant that your specification doesn't allow.)

  • A non-standard textual format is not a good idea because of potential for mis-implementation and ambiguity in the format. (For example 3-letter timezones are ambiguous)

  • Textual formats are easier for a human to read; e.g. for debugging purposes.

But all in all, I don't think it matters if you use a numeric or (standards-based) textual format, provided that you clearly specify the required format, and what it means.

Upvotes: 2

npe
npe

Reputation: 15709

Instead of a straight answer, I'll give a reference to OAuth. According to the OAuth 1.0 specification:

Unless otherwise specified by the Service Provider, the timestamp is expressed in the number of seconds since January 1, 1970 00:00:00 GMT.

So, if you really don't really need a millisecond granurality, do what others do.

Upvotes: -1

Bananeweizen
Bananeweizen

Reputation: 22070

You can rely on that date format as Java also only gets that date from the operating system. It does not matter what programming language you use. See unix time in Wikipedia for a more detailed explanation.

Upvotes: 1

Sparky
Sparky

Reputation: 8477

No, the millisecond time stamp is far from universal. I suggest you use a standard string format such as RFC 3339 for exchanging time between computers or language runtimes on the same computer. See also Wikipedia: System Time

Upvotes: 6

Related Questions