Perl and Monotonic Time Functions

Perl on Linux supports the POSIX C clock_gettime() function to get the monotonic time (always increasing system time, except for variable overflow) values:

Comparing monotonic time values:

  • avoid problems with leap seconds going backwards in time by NTP, but can “warp”
  • avoid problems with VM time going backwards
  • can only be used locally, not compared across machines
  • can rollover on variable overflow

Disadvantages of clock_gettime() over time/gmtime:

  • rollover requires awareness and calculation
  • not supported on Mac OS X and buggy before RHEL 5.3
  • relative time, not actual time, so cannot be displayed for humans
  • for most programs, requires code change and re-QA
  • dichotomy still exists between system and database time
use strict;
use diagnostics;

use Time::HiRes qw(clock_gettime CLOCK_REALTIME CLOCK_MONOTONIC);

   my $realtime = clock_gettime(CLOCK_REALTIME);

   my $mono = clock_gettime(CLOCK_MONOTONIC);

   print "realtime = $realtime, monotonic = $mono\n";
$ perl /tmp/
realtime = 1483451061.64625, monotonic = 4536159.37919642

Perl – Time::HiRes
clock_gettime(3) – Linux man page
Erlang – Postscript: Time Goes On The leap second of doom
SO: How do I get monotonic time durations in python?
SO: Linux clock_gettime(CLOCK_MONOTONIC) strange non-monotonic behavior
SO: Is CLOCK_MONOTONIC process (or thread) specific?
How the NYE leap second clocked Cloudflare – and how a single character fixed it
W: Swatch Internet Time (Beats)

This entry was posted in API Programming, Linux, Open Source, Perl, Tech. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.