/src/gpsd/gpsd-3.25.1~dev/libgps/timespec_str.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * We also need to set the value high enough to signal inclusion of  | 
3  |  |  * newer features (like clock_gettime).  See the POSIX spec for more info:  | 
4  |  |  * http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02_01_02  | 
5  |  |  *  | 
6  |  |  * This file is Copyright 2010 by the GPSD project  | 
7  |  |  * SPDX-License-Identifier: BSD-2-clause  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include "../include/gpsd_config.h"  /* must be before all includes */  | 
11  |  |  | 
12  |  | #include <ctype.h>  | 
13  |  | #include <errno.h>  | 
14  |  | #include <math.h>  | 
15  |  | #include <stdio.h>  | 
16  |  | #include <stdlib.h>  | 
17  |  | #include <string.h>  | 
18  |  | #include <sys/time.h>  | 
19  |  | #include <time.h>  | 
20  |  |  | 
21  |  | #include "../include/timespec.h"  | 
22  |  |  | 
23  |  | /* Convert a normalized timespec to a nice string  | 
24  |  |  * put in it *buf, buf should be at least 22 bytes  | 
25  |  |  *  | 
26  |  |  * the returned buffer will look like, shortest case:  | 
27  |  |  *    sign character ' ' or '-'  | 
28  |  |  *    one digit of seconds  | 
29  |  |  *    decmal point '.'  | 
30  |  |  *    9 digits of nanoSec  | 
31  |  |  *  | 
32  |  |  * So 12 chars, like this: "-0.123456789"  | 
33  |  |  *  | 
34  |  |  * Probable worst case is 10 digits of seconds,  | 
35  |  |  * but standards do not provide hard limits to time_t  | 
36  |  |  * So 21 characters like this: "-2147483647.123456789"  | 
37  |  |  *  | 
38  |  |  * date --date='@2147483647' is: Mon Jan 18 19:14:07 PST 2038  | 
39  |  |  * date --date='@9999999999' is: Sat Nov 20 09:46:39 PST 2286  | 
40  |  |  *  | 
41  |  |  */  | 
42  |  | const char *timespec_str(const struct timespec *ts, char *buf, size_t buf_size)  | 
43  | 0  | { | 
44  | 0  |     char sign = ' ';  | 
45  |  | 
  | 
46  | 0  |     if (!TS_GEZ(ts)) { | 
47  | 0  |         sign = '-';  | 
48  | 0  |     }  | 
49  |  |  | 
50  |  |     /* %lld and (long long) because some time_t is bigger than a long  | 
51  |  |      * mostly on 32-bit systems. */  | 
52  | 0  |     (void)snprintf(buf, buf_size, "%c%lld.%09ld",  | 
53  | 0  |                    sign,  | 
54  | 0  |                    (long long)llabs(ts->tv_sec),  | 
55  | 0  |                    (long)labs(ts->tv_nsec));  | 
56  | 0  |     return  buf;  | 
57  | 0  | }  | 
58  |  |  | 
59  |  | /* end */  | 
60  |  | // vim: set expandtab shiftwidth=4  |