1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifdef __linux__
# include <sys/timex.h>
#endif
size_t
libpatch_create_timestamp_now(char *buf, size_t bufsize, signed frac, int zone, enum libpatch_style style)
{
struct timespec ts;
#ifdef __linux__
struct timex timex;
struct tm tm;
int r;
#endif
if (frac < 0) {
if (clock_gettime(CLOCK_REALTIME, &ts)) {
frac = 9;
} else {
frac = 0;
while (ts.tv_nsec < 1000000000L) {
ts.tv_nsec *= 10;
frac += 1;
}
}
}
#ifdef __linux__
memset(&timex, 0, sizeof(timex));
if (frac > 6)
timex.status = ADJ_NANO;
r = adjtimex(&timex);
if (r == -1) {
if (errno == ENOSYS)
goto fallback;
return 0;
}
if (timex.time.tv_sec % (24 * 60 * 60) == 0) {
if (r == TIME_INS) {
timex.time.tv_sec -= 1;
if (!localtime_r(&timex.time.tv_sec, &tm))
return 0;
tm.tm_sec += 1;
} else if (r == TIME_DEL) {
timex.time.tv_sec += 1;
if (!localtime_r(&timex.time.tv_sec, &tm))
return 0;
} else {
if (!localtime_r(&timex.time.tv_sec, &tm))
return 0;
}
} else if (r == TIME_OOP) {
if (!localtime_r(&timex.time.tv_sec, &tm))
return 0;
tm.tm_sec += 1;
} else {
if (!localtime_r(&timex.time.tv_sec, &tm))
return 0;
}
return libpatch_create_timestamp(buf, bufsize, &tm, (uintmax_t)timex.time.tv_usec,
frac > 6 ? 9U : 6U, (unsigned)frac, zone, style);
fallback:
#endif
if (clock_gettime(CLOCK_REALTIME, &ts))
return 0;
return libpatch_create_timestamp_ts(buf, bufsize, &ts, (unsigned)frac, zone, style);
}
|