When to use Monotonic Time

Oct 3, 2016

When writing about time and software development, most people think of time zones. This Youtube video does a pretty good job of explaining why time zones can become such a nightmare for developers. Recently, I stumbled upon a whole different issue with time and software.

Sourcetoad develops a lot of software for the cruise industry. Time on cruise ships is an interesting dilemma. As cruise ships pass time zones, the time must change. However, it would be very confusing for passengers if the time suddenly changed in the middle of the day as the ship moved.

Imagine walking to a dinner reservation at 6:30 PM. You leave at 6:15 PM. When you get to the restaurant, the maître d’ says you have arrived an hour early. The ship crossed a timezone at 6:20 PM, and changed time to 5:20 PM. The average passenger isn’t going to be able to keep track of this. Instead, the ship’s time is controlled by a person. This person chooses convenient hours to adjust the time, mostly at night. In other words, the ship sort of has its own time zone.

This “custom” time zone causes a problem for systems that must display the time. How do the systems know what time to display? Luckily, there is a time server on the ship that sends out both UTC and the “Ship’s Time.” This information can be used in various ways for the application to know the correct display time. The easiest way is to set the application’s server to UTC, but point it at the ship’s time. If forced updates are on, as soon as the ship’s time changes, the server’s UTC time will change. The server will now know the correct time to give to the application.

The problem with this setup is that servers don’t expect their UTC time to change. UTC is supposed to be constant everywhere in the world. Time zone changes are fine, but the UTC timestamp should never change. We recently had an application that, when the UTC time changed, seemed to freeze for an hour. Once the time moved forward to where it was before the change, the application resumed functioning. It turned out, this application was using callback times based upon the system clock. When the system clock moved back, it was as though we set the callback to fire in an hour vs. 10 seconds.

I began looking for a solution to this problem, fearing there might not be one. The proper answer might be, “UTC time shouldn’t change.” Luckily, it turned out there was an answer in the form of Monotonic time. Unlike a UTC timestamp (which starts January 1st, 1970), Monotonic time starts at an arbitrary point. It cannot move backwards. Finally, and most importantly, it is completely independent of the system clock: changes to the system clock will not affect a monotonic time.

Using a monotonic time for our callback resolved the issue, but this is a very special use-case. In almost all applications, UTC should not ever move. So why do monotonic timers exist, and when are they commonly used? As it turns out, in high-precision environments, the system clock is not a reliable way to time events. System clocks can drift over time, and when a service such as NTP updates the clock to the reference, it can cause errors in precision measurements. Also, monotonic time functions generally return higher precision times. This means they can be used for measuring very small intervals, where the system clock is not granular enough. For example, in JavaScript, both the system time and the monotonic time is returned in milliseconds, but you can see the difference in precision:

console.log((new Date().getTime())); 1474991522515 console.log(window.performance.now()); 74891.05500000001

Are there places your software is using the system clock that it should be using a monotonic timer? Maybe it’s not a big deal, but it might be worthwhile to use one the next time.


Meet Sourcetoad, a 2023 Best Places to Work honoree

Meet Sourcetoad, a 2023 Best Places to Work honoree

Tampa Bay Business Journal — April 26, 2023 Sourcetoad has been selected as a 2023 Best Places to Work! We are thrilled to be among Tampa's top workplaces for the fourth year in a row, and we are grateful to each of our Toads for making Sourcetoad such a fantastic...

This Year in FinTech: Trends to Watch in 2023

This Year in FinTech: Trends to Watch in 2023

The financial services industry experienced rapid digitization throughout the COVID-19 pandemic, and growth of the financial technology market is expected to continue in 2023. The global fintech market grew to $158.9 billion in 2022, and projections estimate it will...

2023 Top HealthTech Trends

2023 Top HealthTech Trends

Technological innovation has played a vital role in the transformation and growth of the healthcare industry. Not only is the medical space an early adopter of emerging technologies, HealthTech is currently one of the fastest-growing industries worldwide. The global...