summaryrefslogtreecommitdiff
path: root/timeadd.c
diff options
context:
space:
mode:
Diffstat (limited to 'timeadd.c')
-rw-r--r--timeadd.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/timeadd.c b/timeadd.c
index e43c951..584180c 100644
--- a/timeadd.c
+++ b/timeadd.c
@@ -14,7 +14,8 @@
static void print_usage(const char *program_name) {
fprintf(stderr, "Usage: %s [-d]\n", program_name);
fputs("Options:\n"
- " -d Display time difference instead of a monotonic clock\n",
+ " -d Display time difference instead of a monotonic clock\n"
+ " -z Start the monotonic clock at zero.\n",
stderr);
}
@@ -40,12 +41,16 @@ int main(int argc, char **argv) {
int tp_i = 0;
int opt;
bool diff = false;
+ bool zero = false;
- while ((opt = getopt(argc, argv, "d")) != -1) {
+ while ((opt = getopt(argc, argv, "dz")) != -1) {
switch (opt) {
case 'd':
diff = true;
break;
+ case 'z':
+ zero = true;
+ break;
default:
print_usage(argv[0]);
return 1;
@@ -58,6 +63,11 @@ int main(int argc, char **argv) {
out_format = "[%6llu.%06lu] %s\n";
}
+ if (zero) {
+ /* Use zero as reference. */
+ clock_gettime(CLOCK_MONOTONIC, &tp[1]);
+ }
+
while (fgets(buf, sizeof(buf), stdin) != NULL) {
char *newline = strrchr(buf, '\n');
@@ -74,6 +84,8 @@ int main(int argc, char **argv) {
* inverse "tp_i" points to the new time (in the next
* iteration it becomes the old time) */
tp_i ^= 1;
+ } else if (zero) {
+ tp[tp_i] = timediff(tp[tp_i ^ 1], tp[tp_i]);
}
printf(out_format,