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

SYNOPSIS

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

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

$oo = Time::Monotonic->new;
$oo->now;

DESCRIPTION

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.

API

monotonic_time()

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().

backend()

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)
is_monotonic()

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;

SEE ALSO

Source Code Repository

Why are monotonic clocks important?

monotonic_clock library

AUTHOR

David Caldwell <david@porkrind.org>

COPYRIGHT AND LICENSE

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.