Monotonic Time, or Perfect model vs Imperfect Reality

gmarik 2 min
Table Of Contents ↓

A Day: Perfect model vs Imperfect Reality

1 Day is not exactly 24 hours Time And Date

1 Day (measured with Solar Time) is the time it takes the Earth to rotate around its own axis so the Sun appears in the “same position” in the sky.

It takes mean 1s/0.002s ~ 500 days to accumulate 1 second excess. This second is what’s called Leap Second.

A Day: Imperfections of the Perfect model

In order to keep up with the Solar Time the 1 second excess “is introduced” every once in a while.

To preserve “time progresses forward” invariant WallClock needs to add the 1 second excess(Leap Second) through imperfection as 23:59:60.

Unfortunately:

Most computers, unable to represent 23:59:60, instead insert a clock reset and repeat 23:59:59.

T(n) : "23:59:59.995"

… spend/wait 10ms.

T(n+1) : "23:59:59.005"

computers are “spending” 1 second without progressing WallClock forward.

WallClock: Implications of not progressing forward

or T(n) - T(n+1) >= 0 or T(n+1) - T(n) <= 0

In other words a non-zero duration measured with WallClock may be negative or equal 0.

WallClock: implications in programming

It’s very common to have code that measures duration like this:

start := time.Now()
// 
// ... do some stuff
// 
dur := Time.Now().Sub(start)
if dur == 0 {
//  do this
} else {
 // do that
}

Without assuming imperfections, the above code may have false positives as described in:

The solution for Go was to maintain the invariant “time progresses forward” with monotonic time which was introduced in Go1.9

A Leap Day

Interestingly there’s also a Leap Day, an additional day to make up for ~365.25 days it takes the Earth to rotate around the Sun. Despite the similarities with the Leap Second the problems were avoided by introducing an extra day (aka Feb 29) and preserving the invariant “time progresses forward” in this case.

Related Posts
Read More
Hosting static site with App Engine, Cloud Build and Hugo
SwiftUI Hello World
Comments
read or add one↓