Time - days of clock vs Monotonic clocks

less than 1 minute read

Getting days of clocks
- Linux: clock_gettime(CLOCK_REALTIME)
- Java: System.currentTimeMillis()

Getting monotonic clocks
- Linux: clock_gettime(CLOCK_MONOTONIC)
- Java: System.nanoTime()

It's important to know difference between days of clocks and monotonic clocks because days of clocks is measured from a certain epock(Java is using 1970 Jan 1 in UTC) it can be backward when time is adjusted by NTP.
But monotonic clocks will never go back, it will guarantee to be always forward so if application want to measure the duration of time, monotonic clocks should be used.
However it's meaningless on comparing monotonic clocks between two machines.