1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
17 typedef uint64_t usec_t;
18 typedef uint64_t nsec_t;
20 #define PRI_NSEC PRIu64
21 #define PRI_USEC PRIu64
22 #define NSEC_FMT "%" PRI_NSEC
23 #define USEC_FMT "%" PRI_USEC
27 typedef struct dual_timestamp {
32 typedef struct triple_timestamp {
38 #define USEC_INFINITY ((usec_t) -1)
39 #define NSEC_INFINITY ((nsec_t) -1)
41 #define MSEC_PER_SEC 1000ULL
42 #define USEC_PER_SEC ((usec_t) 1000000ULL)
43 #define USEC_PER_MSEC ((usec_t) 1000ULL)
44 #define NSEC_PER_SEC ((nsec_t) 1000000000ULL)
45 #define NSEC_PER_MSEC ((nsec_t) 1000000ULL)
46 #define NSEC_PER_USEC ((nsec_t) 1000ULL)
48 #define USEC_PER_MINUTE ((usec_t) (60ULL*USEC_PER_SEC))
49 #define NSEC_PER_MINUTE ((nsec_t) (60ULL*NSEC_PER_SEC))
50 #define USEC_PER_HOUR ((usec_t) (60ULL*USEC_PER_MINUTE))
51 #define NSEC_PER_HOUR ((nsec_t) (60ULL*NSEC_PER_MINUTE))
52 #define USEC_PER_DAY ((usec_t) (24ULL*USEC_PER_HOUR))
53 #define NSEC_PER_DAY ((nsec_t) (24ULL*NSEC_PER_HOUR))
54 #define USEC_PER_WEEK ((usec_t) (7ULL*USEC_PER_DAY))
55 #define NSEC_PER_WEEK ((nsec_t) (7ULL*NSEC_PER_DAY))
56 #define USEC_PER_MONTH ((usec_t) (2629800ULL*USEC_PER_SEC))
57 #define NSEC_PER_MONTH ((nsec_t) (2629800ULL*NSEC_PER_SEC))
58 #define USEC_PER_YEAR ((usec_t) (31557600ULL*USEC_PER_SEC))
59 #define NSEC_PER_YEAR ((nsec_t) (31557600ULL*NSEC_PER_SEC))
61 /* We assume a maximum timezone length of 6. TZNAME_MAX is not defined on Linux, but glibc internally initializes this
62 * to 6. Let's rely on that. */
63 #define FORMAT_TIMESTAMP_MAX (3+1+10+1+8+1+6+1+6+1)
64 #define FORMAT_TIMESTAMP_WIDTH 28 /* when outputting, assume this width */
65 #define FORMAT_TIMESTAMP_RELATIVE_MAX 256
66 #define FORMAT_TIMESPAN_MAX 64
68 #define TIME_T_MAX (time_t)((UINTMAX_C(1) << ((sizeof(time_t) << 3) - 1)) - 1)
70 #define DUAL_TIMESTAMP_NULL ((struct dual_timestamp) {})
71 #define TRIPLE_TIMESTAMP_NULL ((struct triple_timestamp) {})
73 usec_t now(clockid_t clock);
74 #if 0 /// UNNEEDED by elogind
75 nsec_t now_nsec(clockid_t clock);
78 dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
79 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u);
80 #if 0 /// UNNEEDED by elogind
81 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u);
82 dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u);
85 triple_timestamp* triple_timestamp_get(triple_timestamp *ts);
86 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u);
88 #define DUAL_TIMESTAMP_HAS_CLOCK(clock) \
89 IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC)
91 #define TRIPLE_TIMESTAMP_HAS_CLOCK(clock) \
92 IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM)
94 static inline bool dual_timestamp_is_set(const dual_timestamp *ts) {
95 return ((ts->realtime > 0 && ts->realtime != USEC_INFINITY) ||
96 (ts->monotonic > 0 && ts->monotonic != USEC_INFINITY));
99 static inline bool triple_timestamp_is_set(const triple_timestamp *ts) {
100 return ((ts->realtime > 0 && ts->realtime != USEC_INFINITY) ||
101 (ts->monotonic > 0 && ts->monotonic != USEC_INFINITY) ||
102 (ts->boottime > 0 && ts->boottime != USEC_INFINITY));
105 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock);
107 usec_t timespec_load(const struct timespec *ts) _pure_;
108 #if 0 /// UNNEEDED by elogind
109 nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
111 struct timespec *timespec_store(struct timespec *ts, usec_t u);
113 usec_t timeval_load(const struct timeval *tv) _pure_;
114 struct timeval *timeval_store(struct timeval *tv, usec_t u);
116 char *format_timestamp(char *buf, size_t l, usec_t t);
117 #if 0 /// UNNEEDED by elogind
118 char *format_timestamp_utc(char *buf, size_t l, usec_t t);
120 char *format_timestamp_us(char *buf, size_t l, usec_t t);
121 #if 0 /// UNNEEDED by elogind
122 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t);
124 char *format_timestamp_relative(char *buf, size_t l, usec_t t);
125 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy);
127 #if 0 /// UNNEEDED by elogind
128 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t);
129 int dual_timestamp_deserialize(const char *value, dual_timestamp *t);
131 int timestamp_deserialize(const char *value, usec_t *timestamp);
133 #if 0 /// UNNEEDED by elogind
134 int parse_timestamp(const char *t, usec_t *usec);
137 int parse_sec(const char *t, usec_t *usec);
138 #if 0 /// UNNEEDED by elogind
139 int parse_sec_fix_0(const char *t, usec_t *usec);
141 int parse_time(const char *t, usec_t *usec, usec_t default_unit);
142 #if 0 /// UNNEEDED by elogind
143 int parse_nsec(const char *t, nsec_t *nsec);
145 bool ntp_synced(void);
147 int get_timezones(char ***l);
148 bool timezone_is_valid(const char *name, int log_level);
151 bool clock_boottime_supported(void);
152 bool clock_supported(clockid_t clock);
153 #if 0 /// UNNEEDED by elogind
154 clockid_t clock_boottime_or_monotonic(void);
156 usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);
159 #if 0 /// UNNEEDED by elogind
160 int get_timezone(char **timezone);
162 time_t mktime_or_timegm(struct tm *tm, bool utc);
164 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc);
166 #if 0 /// UNNEEDED by elogind
167 unsigned long usec_to_jiffies(usec_t usec);
170 bool in_utc_timezone(void);
172 static inline usec_t usec_add(usec_t a, usec_t b) {
175 /* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output, and doesn't
179 if (c < a || c < b) /* overflow check */
180 return USEC_INFINITY;
185 static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
187 if (timestamp == USEC_INFINITY) /* Make sure infinity doesn't degrade */
188 return USEC_INFINITY;
189 if (timestamp < delta)
192 return timestamp - delta;
195 static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
197 return usec_add(timestamp, (usec_t) (-delta));
199 return usec_sub_unsigned(timestamp, (usec_t) delta);
202 #if SIZEOF_TIME_T == 8
203 /* The last second we can format is 31. Dec 9999, 1s before midnight, because otherwise we'd enter 5 digit year
204 * territory. However, since we want to stay away from this in all timezones we take one day off. */
205 #define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 253402214399000000)
206 #elif SIZEOF_TIME_T == 4
207 /* With a 32bit time_t we can't go beyond 2038... */
208 #define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 2147483647000000)
210 #error "Yuck, time_t is neither 4 nor 8 bytes wide?"
213 int time_change_fd(void);