Designing a REST API: Unix Time vs. ISO-8601 (2013)
nickb.dev
nickb.dev
[1] https://geometrian.com/programming/reference/timestds/index....
There are not alway 86400s in 1 day. Midnight is not a whole number of multiples of 86400 after Jan 1st 1970.
Isn't that an issue with the format you want to convert UNIX time to ?
I mean, UNIX time is quite clearly and unambiguously defined as the number of seconds that passed since January 1st 1970. How are UTC's leap seconds any relevant?
It's not though. Unix time is not monotonic. It goes backwards sometimes.
Another way of viewing it is that unix timestamp is seconds since 1970-01-01 minus number of leap seconds that have passed.
So certain unix timestamp can represent two different points in time, the last normal second of the day or the leap second that happened after it. This makes (certain) unix timestamps ambiguous.
Leap seconds exist in UTC, that's the entire thing.
A leap second getting inserted (which has always been the case so far, though it may eventually change) means a UTC day ends on 23:59:60, which as far as UTC is concerned is perfectly fine, nothing wrong with a minute having 61 seconds.
UNIX time though? UNIX time is (86400 * days since epoch) + seconds since midnight.
23:59:60 is 86400, so on the last second of day X you're at 86400 * X + 86400, and on the next second (the first of day X+1) you're at 86400 * (X+1) + 0.
Which is the same value. And thus one second repeats.
099.75
100.00
100.25
100.50
100.75
100.00
100.25
100.50
100.75
101.00
Maybe some day we’ll get rid of this collective brain damage: https://en.wikipedia.org/wiki/Leap_second#International_prop...Doesn't that mean that UNIX time is in fact a stopwatch, but to express it in UTC you need to factor in UTC's quirks such as its leap seconds?
The hardware RTC on your motherboard is a stopwatch. When a leap second occurs, your system time will be set 1 second backwards which is also 1 second less than what the RTC reports. Then, the new fuckered time will be written to the RTC so it stops acting as a stopwatch and instead tracks unix time.
An application that reads the system unix time 4 times a second (using a monotonic clock for the 250ms delay) and prints it will observe a negative duration in unix time as seen in my previous comment.
> to express it in UTC you need to factor in UTC's quirks such as its leap seconds
Oh that would make unix time so much more useful. But no, the UTC quirk is incorporated into unix time the instant a leap second happen as if some nutjob logged into all your servers and changed each clock manually.
Thus UNIX time is at least 27 seconds late (because there have been 27 seconds since 1972).
The only place where I can envision this being a potential problem might be with stock trading, but HFT or other low-latency trading wouldn't be done through a REST API or web browser anyway.
Meanwhile, with UNIX time, you gain a mostly unambiguous, cross-platform way of dealing with dates/times with just an integer, easy math and comparisons (such as: this integer timestamp is > another, so it occurred after), and zero parsing or storage of text strings. That is a huge win in practical API design, especially when dealing with thousands or millions of events, and using an integer datatype in a database instead of a text field is a significant win, too, in terms of storage and indexing.
With that said, standards are nice, especially when dealing with an API that might talk to many different types of clients, but a text-based format like ISO-8601 probably isn't an optimal solution for most use cases.
ntpd also supports this [2], though I'm not sure how popular it is in practice. Anyone making use of this?
http://www.madore.org/~david/computers/unix-leap-seconds.htm...
ISO is fun up until you have to parse "2022-089". Yes, that's a valid ISO date. And it's in March. (And yes, I've seen that used in the wild, though I don't know why anyone would ever choose that format.)
JSON self-describes booleans, numbers, strings, arrays, objects, and null. This is especially convenient when working with dynamic or ambiguous structures, and working in dynamically typed languages. Timestamp is sufficiently common and useful that it should be added to that list.
If you mean ".NET parser pattern matches strings and converts them dynamically" - that's the same horrible behavior yaml parsers have and it causes endless trouble. Imagine parsing titles of posts and some clever author makes a title that matches the timestamp format.
10-15 years later I'm still baffled by that early decision (and glad it's been fixed since then)
and that is important in realworld applications.
Tell me more about that. I'm not sure I grasp what it is telling about JSON?
"My programming language doesn't have timestamps" is not a real problem.
"Ten thousand stupid date formats which are all ambiguous with either numbers or strings" is an actual problem that people bang their heads into every day.
What are you talking about? JSON is a format for exchanging data of any kind. It doesn't have types beyond a set of primitives to that it can remain as a _notation_ that allows you to pass any type you define and want to send.
Nothing is stopping any library or language from implementing an object or String for dates or times (or both) and tons of them have done so.
It wouldn't be JSON's place at all to start defining anything like that.
You seem to believe that the primitives in JSON are some sort of platonically pure form. They were merely a choice, and a that choice could be improved.
Is it still a 50/50 split today? I suspect ISO 8601 has grown to become the standard.
The biggest advantage of ISO 8601 is probably that you can also use it to represent dates and/or times rather than only datetimes but this can result in unnecessary ambiguity (e.g. if you decide to split the datetime into a date and a time field and make assumptions about the timezone).
Looks like someone hasn't had to deal with data from multiple different time zones processed by code written by different developers over several years!
Unix time could be unambiguous, but it is not in practice. If it has been unambiguous for you, congratulations!
Use ISO style dates if you don't want to spend the rest of your life explaining what a date actually means.
This feels strongly an argument FOR unix timestamps. Simply a number, the biggest mess up is seconds/millis. This order of magnitude difference is not that hard to disambiguate.
---
What I'm saying is - in your json document, you see a number and you rely on a convention to decode that number. The number has no way to tell you anything about what the number actually means. The ISO standard does. People could still mess it up, but I feel like the people who use the ISO standard care a lot more than the people who use the Unix standard.
"The right thing" is, for example, simply `System.currentTimeMillis()` in Java. I don't have to think about what 0 means. All I need to know is there's a mapping from an instant to a number.
This representation is arguably closer to the essense of time. Barring relativistic effects, it's just an affine line. Dates and timezones are artificial structure that we put on this line.