summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Leinweber <bleinweber@spaceflight.com>2017-10-30 08:24:47 -0700
committerJoey Hess <joeyh@joeyh.name>2017-10-30 12:48:52 -0400
commitd4592ac408f2086a3ed305c3fccf9107763e2be4 (patch)
treeddc20487db1829d71a8a836c3be9cc653e7cc0e0
parent01d1b22f22fae647cbfd855dc82f8d9548607ab0 (diff)
downloadmoreutils-d4592ac408f2086a3ed305c3fccf9107763e2be4.tar.gz
ts: Introduce '-m' option to use CLOCK_MONOTONIC
The new '-m' option will retrieve the monotonic clock rather than the realtime clock. Sometimes it is required that the timer being used not change even when the user updates the system time. The monotonic clock guarantees a monotonically increasing time value which may not be adjusted. Signed-off-by: Ben Leinweber <bleinweb@gmail.com>
-rwxr-xr-xts22
1 files changed, 18 insertions, 4 deletions
diff --git a/ts b/ts
index 63baf18..7f6992a 100755
--- a/ts
+++ b/ts
@@ -56,8 +56,9 @@ $|=1;
my $rel=0;
my $inc=0;
my $sincestart=0;
+my $mono=0;
use Getopt::Long;
-GetOptions("r" => \$rel, "i" => \$inc, "s" => \$sincestart) || die "usage: ts [-r] [-i | -s] [format]\n";
+GetOptions("r" => \$rel, "i" => \$inc, "s" => \$sincestart, "m" => \$mono) || die "usage: ts [-r] [-i | -s] [format]\n";
if ($rel) {
eval q{
@@ -77,15 +78,19 @@ $format=shift if @ARGV;
# For subsecond resolution, Time::HiRes is needed.
my $hires=0;
-if ($format=~/\%\.[Ss]/) {
+if ($format=~/\%\.[Ss]/ || $mono) {
require Time::HiRes;
+ use Time::HiRes qw(CLOCK_MONOTONIC);
$hires=1;
}
my $lastseconds = 0;
my $lastmicroseconds = 0;
-if ($hires) {
+if ($mono) {
+ ($lastseconds, $lastmicroseconds) =
+ Time::HiRes::clock_gettime(CLOCK_MONOTONIC);
+} elsif ($hires) {
($lastseconds, $lastmicroseconds) = Time::HiRes::gettimeofday();
} else {
$lastseconds = time;
@@ -96,7 +101,16 @@ while (<>) {
if (! $rel) {
if ($hires) {
my $f=$format;
- my ($seconds, $microseconds) = Time::HiRes::gettimeofday();
+ my ($seconds, $microseconds);
+ if ($mono) {
+ my $raw_time =
+ Time::HiRes::clock_gettime(CLOCK_MONOTONIC);
+ $seconds = int($raw_time);
+ $microseconds = $raw_time - $seconds;
+ print "$raw_time $seconds $microseconds\n";
+ } else {
+ ($seconds, $microseconds) = Time::HiRes::gettimeofday();
+ }
if ($inc || $sincestart) {
my $deltaseconds = $seconds - $lastseconds;
my $deltamicroseconds = $microseconds - $lastmicroseconds;