summaryrefslogtreecommitdiff
path: root/gpsutils.c
diff options
context:
space:
mode:
authorGary E. Miller <gem@rellim.com>2015-02-05 00:32:10 -0800
committerGary E. Miller <gem@rellim.com>2015-02-05 00:32:10 -0800
commit718db4342bb85570afba727886fff0b3311b2ff2 (patch)
tree4671d4cdac5ac66779c4fc24013541d73cecb3b3 /gpsutils.c
parenta3a9587f80da3daccf1e830fb24096c0badd30d5 (diff)
downloadgpsd-718db4342bb85570afba727886fff0b3311b2ff2.tar.gz
Add function timespec_str(), and use it.
Converting a timespec to a printable string is odd, so placed in a new function for ease of use.
Diffstat (limited to 'gpsutils.c')
-rw-r--r--gpsutils.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/gpsutils.c b/gpsutils.c
index 58d12119..388a7bae 100644
--- a/gpsutils.c
+++ b/gpsutils.c
@@ -485,3 +485,22 @@ double earth_distance(double lat1, double lon1, double lat2, double lon2)
return earth_distance_and_bearings(lat1, lon1, lat2, lon2, NULL, NULL);
}
+/* Convert a normailized timespec to a nice string
+ * put in it *buf, buf should be at least 22 bytes
+ * return negative for error
+ * otherwise the number of chars in buf, excluding trailing \0
+*/
+int timespec_str( struct timespec *ts, char *buf, int buf_size )
+{
+ int ret;
+ char sign = ' ';
+
+ if ( (0 > ts->tv_nsec ) || ( 0 > ts->tv_sec ) ) {
+ sign = '-';
+ }
+ ret = snprintf( buf, buf_size, "%c%ld.%09ld",
+ sign,
+ (long)labs(ts->tv_sec),
+ (long)labs(ts->tv_nsec));
+ return ret;
+}