Time as Fractional Days?

· Updated · text

Is it reasonable to encode time as the fractional number of days since a suitable epoch? Excel and SQLite do it, but I thought the precision would be too low.

If your epoch starts recently and you use a binary64 for storage, your precision is 0.6 µs during this century, 80 µs when the glaciers retreated, 10 ms when Homo habilis emerged, and 1.4 minutes at the start of the Universe. Considering that significant events in recent history are discussed with precision of one second, these precisions seem generous.

Programming languages often represent time as seconds since plus nanoseconds thereafter. For example, clock_gettime(2) or Goʼs time.Time. This representation uses 16 bytes of memory to provide nanosecond precision everywhen. The algorithms require caution at the boundary between seconds and nanoseconds. Floating point days would reduce storage by half and might simplify the algorithms a little.

If fractional days work, what about fractional weeks or fractions of a 400-year Gregorian cycle? (We canʼt do months or years since their lengths vary). It turns out that the units donʼt matter much. A binary64 only has so much precision, so choose a representation with a convenient implementation.

Notes:

This code calculates the precisions. The only non-obvious part is the way it converts a float64 to a uint64, increments it, then converts it back. Since an IEEE-754 float stores its significand in the least significant bits, this trick increments a float64 by the smallest amount possible.

This article is only relevant when working with absolute times. If youʼre measuring the duration between two events, use your systemʼs monotonic clock. It does the right thing when NTP moves time backwards.

is a convenient epoch since it begins a four century Gregorian cycle. Some date algorithms are cleaner if the leap years are at the end of a cycle instead of the beginning. Using a recent cycle, instead of 1601 or 1201, keeps the precision high for recent dates.

Update : rewording, mention fractional weeks and fractional Gregorian cycles. The previous text is available at archive.today or archive.org.