diff options
author | Gary E. Miller <gem@rellim.com> | 2015-02-05 00:32:10 -0800 |
---|---|---|
committer | Gary E. Miller <gem@rellim.com> | 2015-02-05 00:32:10 -0800 |
commit | 718db4342bb85570afba727886fff0b3311b2ff2 (patch) | |
tree | 4671d4cdac5ac66779c4fc24013541d73cecb3b3 /gpsutils.c | |
parent | a3a9587f80da3daccf1e830fb24096c0badd30d5 (diff) | |
download | gpsd-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.c | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -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; +} |