summaryrefslogtreecommitdiff
path: root/timespec_str.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2015-03-08 10:42:04 -0400
committerEric S. Raymond <esr@thyrsus.com>2015-03-08 10:42:04 -0400
commit0deeeb2b1fb55d78c4abedbf88c8f46d9b300ed6 (patch)
tree153b73860dff8d769cbbb3006cdb64d6014e23f0 /timespec_str.c
parent93f417401f02b85e936d93cf5f7ba54d75de49de (diff)
downloadgpsd-0deeeb2b1fb55d78c4abedbf88c8f46d9b300ed6.tar.gz
Break timespec_str out of gpsutils.c.
It's not used on the client side, and it's needed separately for ntplib. All regression tesrs pass. PPS is live.
Diffstat (limited to 'timespec_str.c')
-rw-r--r--timespec_str.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/timespec_str.c b/timespec_str.c
new file mode 100644
index 00000000..69e045ee
--- /dev/null
+++ b/timespec_str.c
@@ -0,0 +1,54 @@
+/*
+ * This file is Copyright (c) 2010 by the GPSD project
+ * BSD terms apply: see the file COPYING in the distribution root for details.
+ */
+
+/*
+ * We also need to set the value high enough to signal inclusion of
+ * newer features (like clock_gettime). See the POSIX spec for more info:
+ * http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02_01_02
+*/
+#define _XOPEN_SOURCE 600
+
+#include <stdio.h>
+#include <time.h>
+#include <sys/time.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <errno.h>
+#include <ctype.h>
+
+#include "timespec_str.h"
+
+/* Convert a normalized timespec to a nice string
+ * put in it *buf, buf should be at least 22 bytes
+ *
+ * the returned buffer will look like, shortest case:
+ * sign character ' ' or '-'
+ * one digit of seconds
+ * decmal point '.'
+ * 9 digits of nanoSec
+ *
+ * So 12 chars, like this: "-0.123456789"
+ *
+ * Absolute worst case is 10 digits of seconds.
+ * So 21 digits like this: "-2147483647.123456789"
+ *
+*/
+void timespec_str(const struct timespec *ts, /*@out@*/char *buf, int buf_size)
+{
+ char sign = ' ';
+
+ /*@-type@*//* splint is confused about timespec*/
+ if ( (0 > ts->tv_nsec ) || ( 0 > ts->tv_sec ) ) {
+ sign = '-';
+ }
+ (void) snprintf( buf, buf_size, "%c%ld.%09ld",
+ sign,
+ (long)labs(ts->tv_sec),
+ (long)labs(ts->tv_nsec));
+ /*@+type@*/
+}
+
+/* end */