aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c9
-rw-r--r--lib/lib.h1
2 files changed, 10 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 41ecb1de..88dd13a0 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1394,3 +1394,12 @@ long long millitime(void)
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec*1000+ts.tv_nsec/1000000;
}
+
+// Formats `ts` in ISO format ("2018-06-28 15:08:58.846386216 -0700").
+char *format_iso_time(char *buf, size_t len, struct timespec *ts)
+{
+ strftime(buf, len, "%F %T", localtime(&(ts->tv_sec)));
+ sprintf(buf+strlen(buf), ".%09ld ", ts->tv_nsec);
+ strftime(buf+strlen(buf), len-strlen(buf), "%z", localtime(&(ts->tv_sec)));
+ return buf;
+}
diff --git a/lib/lib.h b/lib/lib.h
index 353e262d..61a33fa7 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -250,6 +250,7 @@ char *getgroupname(gid_t gid);
void do_lines(int fd, void (*call)(char **pline, long len));
long environ_bytes();
long long millitime(void);
+char *format_iso_time(char *buf, size_t len, struct timespec *ts);
#define HR_SPACE 1 // Space between number and units
#define HR_B 2 // Use "B" for single byte units