NAME Time::Monotonic - A clock source that only increments and never jumps


  use Time::Monotonic qw(monotonic_time);
  $t1 = monotonic_time();
  $t2 = monotonic_time();

  die unless Time::Monotonic::is_monotonic();
  say "Backend API: ".Time::Monotonic::backend();

  $oo = Time::Monotonic->new;


Time::Monotonic gives access to monotonic clocks on various platforms (Mac OS X, Windows, and POSIX). A monotonic clock is a time source that won't ever jump forward or backward (due to NTP or Daylight Savings Time updates).

Time::Monotonic uses Thomas Habets's cross platform "monotonic_clock" library under the hood.



This function returns a monotonic time as a floating point number (fractional seconds). Note that this time will not be comparable to times from the built-in time function. The only real use of the value is comparing against other values returned from monotonic_time().


This function returns which backend clock functions are in use. It will be one of:

'clock_gettime' (POSIX)
'mach_absolute_time' (Mac OS X)
'QueryPerformanceCounter' (win32)
'generic' (none of the above)

This will return true if the backend supports monotonic clocks. This can return false if there is no support for monotonic clocks in the current Platform/OS/hardware combo.

OO Interface

Creating a new instance of Time::Monotonic stores the current monotonic counter value as a blessed scalar reference. Consecutive calls to $instance->now returns the time difference since the moment of instantiation. This behavior is equivalent to "tv_interval" in Time::HiRes.

  my $t0 = Time::Monotonic->new;
  my $t1 = $t0->now;

The constructor also accepts an offset value (in fractional seconds), which will be added to the counter value.

  my $t0 = Time::Monotonic->new($offset);
  my $t1 = $t0->now;

Dereferencing returns the initial value:

  print "timer started at: ", $$t0;


Source Code Repository

Why are monotonic clocks important?

monotonic_clock library


David Caldwell <>


Copyright (C) 2015 by David Caldwell

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12.4 or, at your option, any later version of Perl 5 you may have available.