53 points · by iudqnolq · 3y ago · 101 comments
If you think microsecond precision is good enough for anybody, then you could consider using an integer where the LSB represents 1 microsecond. But don't use floating point time, unless your specific application cares more about time scale invariance than time translation invariance.
18446744073709551615
and epoch time now in nanoseconds is less than a tenth of that i.e. 1686340765688397824
You can go for another 500+ years safely on nanoseconds-as-uint64.I fully disagree with using float64. It wastes bits on an exponent and you cannot safely use == to compare timestamps that have math done on them or have been converted to/from a string (including e.g. sent as part of a JSON).
You don't want to get into floating point, it gets all fuzzy and then when you need to change it, there's a bunch of code to change.
And integer representations have traps too. To pick on your particular example: "timestamps" are not the same thing as "times" and if your code predicated on that, it likely has bugs already with monotonicity and uniqueness assumptions.
And the benefit is that, as the article points out, you can have easy subsecond precision, something that is much harder (and pervasively unsupported by existing libraries) with fixed point math.
Should one go to the mattresses to defend either of these designs? No. But on the whole my heart agrees with the linked article. If you have to make a choice today, for new code, stick with a double.
Only because its timestamp (as in `Date.getTime`) is actually the number of milliseconds since the UNIX epoch assuming no leap seconds. You can't keep the current scheme in a nanosecond precision (2^31 * 10^9 ~= 2^61, so float64 will truncate lower bits). Python had the exact same issue and it now has a separate `time.time_ns()` function as opposed to the traditional `time.time()` function.
Just use int64.
I'll go one step further. Time should be represented as unsigned integers, while time-differences should be represented as signed integers.
"10 minutes ago" is int64. June 9th 2023 should be unsigned integer64.
Why artificially limit yourself to only representing times after the epoch? It's not like an extra bit of range is going to make or break anything.
mmm I don’t think that’s true.
- your computer
Alternatively, you’d need an int65.
I was under impression unixtime is best for timestamps. But no reason to store something like a birthday using them; just do a string YYYY-MM-DD?
> "10 minutes ago" is int64. June 9th 2023 should be unsigned integer64.
Won't you run into problems with trying to turn "ten minutes before 1970" into a time?
IOW, if you subtract two `time` types, the result must be a valid `time` type, so you can add/subtract to it. The result cannot be a `timediff` type.
Huh? Of course `time` - `time` gives a `timediff` type. You can add/subtract to it: you add/subtract timediffs, or you can add/subtract a timediff to/from a time. Why would you ever be directly adding times? What is 100 AD + 100 AD? 200 AD? Not if you pick a different 0-point for your calendar ...
But yeah started using int64 30 years ago and all the fuckedness went away.
Step forward the MC146818, used in the PC/AT and emulated by chipsets since, whose built-in daylight savings time change rule implemented in hardware was rendered obsolete in 1987.
> Why am I not using float64 time already??? [fp used ot be slow and was not standardized, but it got faster and after a while you could count on having IEEE-754]
This is not the reason.
Until IEEE-754 fp was terrible. As in buggy terrible. It took years of pain to learn how to get it right. And you can see this in the standard: it's a very short standard, super easy to understand, trivial, really...if you ignore the enormous amount explanations, corner cases, special cases, surprising symmetries, and other copious testament to the well-meaning Mistakes that Were Made in the years before. Intel provided a ton of trade secret understanding from the mistakes in the 8087, and UCB maths prof was intimately involved. It's an incredible achievement.
The history of the standard is quite exciting but has been covered on HN several times before.
time precision was decreasing with time since system was booted and after about 100h of operation this accumulated enough to system believe that incoming missile is already unreachable using existing calculations and caused it to not react at all
I think that is great example why changing precision depending on time might be very bad idea.
imagine that afer Y2038 that slight drop in precision would cause some critical system to fail in unexpected way
A 24 bit mantissa isn't enough for anybody. A 53-bit mantissa is enough unless you need +/- one nanosecond precision at 100 days. If you need that -- and some people do -- your application is too exotic to influence the standards that apply to everyone else.
Jokes aside, Unix Epoch shouls be replaced by Tai time on every computer.
I.e., use a 536-bit unsigned integer to store Planck time with one Planck time following the big bang as the epoch, and use the top three bits as a tetration height instead of an exponent, and you can get real far.
All the best,
For the past, it’s possible to workaround by saying “no leap seconds” but this means that 12PM 1000 years ago might be any time of the day.
But that workaround not gonna work for the future. For many use cases it’s important to be able to represent date-time for many years in the future. The currently used formats allow that easily, at the price of these unpredictable leap second adjustments.
It's the same reason why you should never use floating point numbers for account balances.
Not sure about classic BASIC; I don't really recall there being a specific time type, though I know later things like Visual Basic and etc would just use the VARIANT date type above with a 0 day component.
I like Windows' FILETIME which stores time as 100-nanosecond intervals since the beginning of the year 1601. C#'s DateTime is similar but uses an epoch of 0001 January 1.
I know JavaScript and others simply use the usual unix Epoch but with milliseconds rather than seconds, so time() * 1000.
int64 "nanoseconds since the epoch" seems to be emerging as the de facto standard.
used in Golang [0] and adopted by Python starting in 3.7 [1]
that PEP also spells out some of the problems with using a float64:
> The Python time.time() function returns the current time as a floating-point number which is usually a 64-bit binary floating-point number (in the IEEE 754 format).
> The problem is that the float type starts to lose nanoseconds after 104 days. Converting from nanoseconds (int) to seconds (float) and then back to nanoseconds (int) to check if conversions lose precision:
I added a comment apologizing to software archaeologists on 12 October 287,396 C.E., when the precision of double timestamps (with 1970-01-01T00:00:00 UTC epoch) drops to 2 milliseconds, and the reviewer was fine after that.
* https://github.com/kdeldycke/awesome-falsehood#dates-and-tim...
Microsoft, for one example, has been modelling times as a 64-bit unsigned 100-nanosecond count since 1601 (proleptic-Gregorian proleptic-UTC) for about 30 years, now.
* https://learn.microsoft.com/en-us/windows/win32/api/minwinba...
Daniel J. Bernstein in the late 1990s chose a 0 point for an unsigned count so far back that it pre-dates most estimates of the point of the Big Bang.
* http://cr.yp.to/libtai/tai64.html
1970 is not the mandatory origin for every timescale. (Indeed, in the early years of Unix itself there wasn't even a stable origin for time.) It is not a valid reason for dismissing the idea of storing time as an unsigned integer.
It's also sad to note that the headlined page's first sentence has one of the very falsehoods that programmers believe about time in it.
IIRC the main reason floating point numbers weren’t used in system calls back in the 90-ties was that kernel code couldn’t use the CPUs floating point registers, or they would have to be saved at the entry point of each system call and restored before returning to user space. This was deemed too expensive.
It doesn't overflow until the year 4147 -- but since it's unsigned it can't represent times before 1970.
It's not designed to store times in the past, so that's not an issue for its intended use, but using it as a general time representation format could be problematic (say, if you want to represent a person's birth date).
(An aside: At a previous job, I worked on some code that would display a person's age given their stored birth date. The code didn't handle dates before 1900. I did a little research and found that that wouldn't be a problem, since everyone born before 1900 is dead. Ten years earlier, it would have been an issue.)
(in other words a terrible choice, though not for the reason above).
C already has (since the 2011 standard) `struct timespec`, a structure consisting of a `time_t` representing seconds since the epoch (typically 64 bits signed) and a `long` representing nanoseconds. It's admittedly awkward for arithmetic, but it doesn't overflow for another 292 billion years. It gives about 200 times better precision than 64-bit floating-point (for the interval from 2004 to 2038).
32-bit signed time_t is still a problem, particularly for embedded systems that are difficult to update. (A lot of such systems probably don't need to know the current time.)
A 64-bit signed integer representing nanoseconds since the epoch doesn't overflow until 2554.
Floating-point time is likely to be good enough in a lot of contexts -- but the fact that it's non-standard, unlike `time_t` and `struct timespec`, is a big disadvantage.
Y2038 is a non-problem, the representable range is about ±29000 years around the epoch. The extinction of the dinosaurs is not representable, but I think it’s OK in practice, only a small minority of software needs dates that far in the past or future.
Accuracy is 2.5 times better than FP64, and is equal over the complete range.
Disappointing to me how visually appealing this site is for how useless the words/idea themselves are.