summaryrefslogtreecommitdiff
path: root/timespec_str.c
diff options
context:
space:
mode:
authorGary E. Miller <gem@rellim.com>2016-08-10 12:40:43 -0700
committerGary E. Miller <gem@rellim.com>2016-08-10 12:40:43 -0700
commit290701e02dc3dbc2dc6e4ce5dce2a318d322459c (patch)
treed2608642c7320b391664f36d08166926278158b9 /timespec_str.c
parent86600a67cf59389ffefb60474f55c1ac0c0e9ae0 (diff)
downloadgpsd-290701e02dc3dbc2dc6e4ce5dce2a318d322459c.tar.gz
Fix timespec_str() to the year 2286 on 32 bit systems.
Diffstat (limited to 'timespec_str.c')
-rw-r--r--timespec_str.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/timespec_str.c b/timespec_str.c
index 755adb9a..e297d14b 100644
--- a/timespec_str.c
+++ b/timespec_str.c
@@ -32,10 +32,14 @@
*
* So 12 chars, like this: "-0.123456789"
*
- * Absolute worst case is 10 digits of seconds.
+ * Probable worst case is 10 digits of seconds,
+ * but standards do not provide hard limits to time_t
* So 21 characters like this: "-2147483647.123456789"
*
-*/
+ * date --date='@2147483647' is: Mon Jan 18 19:14:07 PST 2038
+ * date --date='@9999999999' is: Sat Nov 20 09:46:39 PST 2286
+ *
+ */
void timespec_str(const struct timespec *ts, char *buf, size_t buf_size)
{
char sign = ' ';
@@ -43,9 +47,9 @@ void timespec_str(const struct timespec *ts, char *buf, size_t buf_size)
if ( (0 > ts->tv_nsec ) || ( 0 > ts->tv_sec ) ) {
sign = '-';
}
- (void) snprintf( buf, buf_size, "%c%ld.%09ld",
+ (void) snprintf( buf, buf_size, "%c%lld.%09ld",
sign,
- (long)labs(ts->tv_sec),
+ (long long)llabs(ts->tv_sec),
(long)labs(ts->tv_nsec));
}