1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 Copyright 2010 Lennart Poettering
15 typedef uint64_t usec_t;
16 typedef uint64_t nsec_t;
18 #define PRI_NSEC PRIu64
19 #define PRI_USEC PRIu64
20 #define NSEC_FMT "%" PRI_NSEC
21 #define USEC_FMT "%" PRI_USEC
25 typedef struct dual_timestamp {
30 typedef struct triple_timestamp {
36 #define USEC_INFINITY ((usec_t) -1)
37 #define NSEC_INFINITY ((nsec_t) -1)
39 #define MSEC_PER_SEC 1000ULL
40 #define USEC_PER_SEC ((usec_t) 1000000ULL)
41 #define USEC_PER_MSEC ((usec_t) 1000ULL)
42 #define NSEC_PER_SEC ((nsec_t) 1000000000ULL)
43 #define NSEC_PER_MSEC ((nsec_t) 1000000ULL)
44 #define NSEC_PER_USEC ((nsec_t) 1000ULL)
46 #define USEC_PER_MINUTE ((usec_t) (60ULL*USEC_PER_SEC))
47 #define NSEC_PER_MINUTE ((nsec_t) (60ULL*NSEC_PER_SEC))
48 #define USEC_PER_HOUR ((usec_t) (60ULL*USEC_PER_MINUTE))
49 #define NSEC_PER_HOUR ((nsec_t) (60ULL*NSEC_PER_MINUTE))
50 #define USEC_PER_DAY ((usec_t) (24ULL*USEC_PER_HOUR))
51 #define NSEC_PER_DAY ((nsec_t) (24ULL*NSEC_PER_HOUR))
52 #define USEC_PER_WEEK ((usec_t) (7ULL*USEC_PER_DAY))
53 #define NSEC_PER_WEEK ((nsec_t) (7ULL*NSEC_PER_DAY))
54 #define USEC_PER_MONTH ((usec_t) (2629800ULL*USEC_PER_SEC))
55 #define NSEC_PER_MONTH ((nsec_t) (2629800ULL*NSEC_PER_SEC))
56 #define USEC_PER_YEAR ((usec_t) (31557600ULL*USEC_PER_SEC))
57 #define NSEC_PER_YEAR ((nsec_t) (31557600ULL*NSEC_PER_SEC))
59 /* We assume a maximum timezone length of 6. TZNAME_MAX is not defined on Linux, but glibc internally initializes this
60 * to 6. Let's rely on that. */
61 #define FORMAT_TIMESTAMP_MAX (3+1+10+1+8+1+6+1+6+1)
62 #define FORMAT_TIMESTAMP_WIDTH 28 /* when outputting, assume this width */
63 #define FORMAT_TIMESTAMP_RELATIVE_MAX 256
64 #define FORMAT_TIMESPAN_MAX 64
66 #define TIME_T_MAX (time_t)((UINTMAX_C(1) << ((sizeof(time_t) << 3) - 1)) - 1)
68 #define DUAL_TIMESTAMP_NULL ((struct dual_timestamp) {})
69 #define TRIPLE_TIMESTAMP_NULL ((struct triple_timestamp) {})
71 usec_t now(clockid_t clock);
72 #if 0 /// UNNEEDED by elogind
73 nsec_t now_nsec(clockid_t clock);
76 dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
77 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u);
78 #if 0 /// UNNEEDED by elogind
79 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u);
80 dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u);
83 triple_timestamp* triple_timestamp_get(triple_timestamp *ts);
84 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u);
86 #define DUAL_TIMESTAMP_HAS_CLOCK(clock) \
87 IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC)
89 #define TRIPLE_TIMESTAMP_HAS_CLOCK(clock) \
90 IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM)
92 static inline bool dual_timestamp_is_set(const dual_timestamp *ts) {
93 return ((ts->realtime > 0 && ts->realtime != USEC_INFINITY) ||
94 (ts->monotonic > 0 && ts->monotonic != USEC_INFINITY));
97 static inline bool triple_timestamp_is_set(const triple_timestamp *ts) {
98 return ((ts->realtime > 0 && ts->realtime != USEC_INFINITY) ||
99 (ts->monotonic > 0 && ts->monotonic != USEC_INFINITY) ||
100 (ts->boottime > 0 && ts->boottime != USEC_INFINITY));
103 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock);
105 usec_t timespec_load(const struct timespec *ts) _pure_;
106 #if 0 /// UNNEEDED by elogind
107 nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
109 struct timespec *timespec_store(struct timespec *ts, usec_t u);
111 usec_t timeval_load(const struct timeval *tv) _pure_;
112 struct timeval *timeval_store(struct timeval *tv, usec_t u);
114 char *format_timestamp(char *buf, size_t l, usec_t t);
115 #if 0 /// UNNEEDED by elogind
116 char *format_timestamp_utc(char *buf, size_t l, usec_t t);
118 char *format_timestamp_us(char *buf, size_t l, usec_t t);
119 #if 0 /// UNNEEDED by elogind
120 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t);
122 char *format_timestamp_relative(char *buf, size_t l, usec_t t);
123 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy);
125 #if 0 /// UNNEEDED by elogind
126 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t);
127 int dual_timestamp_deserialize(const char *value, dual_timestamp *t);
129 int timestamp_deserialize(const char *value, usec_t *timestamp);
131 #if 0 /// UNNEEDED by elogind
132 int parse_timestamp(const char *t, usec_t *usec);
135 int parse_sec(const char *t, usec_t *usec);
136 #if 0 /// UNNEEDED by elogind
137 int parse_sec_fix_0(const char *t, usec_t *usec);
139 int parse_time(const char *t, usec_t *usec, usec_t default_unit);
140 #if 0 /// UNNEEDED by elogind
141 int parse_nsec(const char *t, nsec_t *nsec);
143 bool ntp_synced(void);
145 int get_timezones(char ***l);
146 bool timezone_is_valid(const char *name, int log_level);
149 bool clock_boottime_supported(void);
150 bool clock_supported(clockid_t clock);
151 #if 0 /// UNNEEDED by elogind
152 clockid_t clock_boottime_or_monotonic(void);
154 usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);
157 #if 0 /// UNNEEDED by elogind
158 int get_timezone(char **timezone);
160 time_t mktime_or_timegm(struct tm *tm, bool utc);
162 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc);
164 #if 0 /// UNNEEDED by elogind
165 unsigned long usec_to_jiffies(usec_t usec);
168 bool in_utc_timezone(void);
170 static inline usec_t usec_add(usec_t a, usec_t b) {
173 /* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output, and doesn't
177 if (c < a || c < b) /* overflow check */
178 return USEC_INFINITY;
183 static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
185 if (timestamp == USEC_INFINITY) /* Make sure infinity doesn't degrade */
186 return USEC_INFINITY;
187 if (timestamp < delta)
190 return timestamp - delta;
193 static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
195 return usec_add(timestamp, (usec_t) (-delta));
197 return usec_sub_unsigned(timestamp, (usec_t) delta);
200 #if SIZEOF_TIME_T == 8
201 /* The last second we can format is 31. Dec 9999, 1s before midnight, because otherwise we'd enter 5 digit year
202 * territory. However, since we want to stay away from this in all timezones we take one day off. */
203 #define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 253402214399000000)
204 #elif SIZEOF_TIME_T == 4
205 /* With a 32bit time_t we can't go beyond 2038... */
206 #define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 2147483647000000)
208 #error "Yuck, time_t is neither 4 nor 8 bytes wide?"
211 int time_change_fd(void);