chiark / gitweb /
94534794a2c3287bfd017c9d38a04d927d0ab220
[elogind.git] / src / basic / time-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8 ***/
9
10 #include <inttypes.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <time.h>
16
17 typedef uint64_t usec_t;
18 typedef uint64_t nsec_t;
19
20 #define PRI_NSEC PRIu64
21 #define PRI_USEC PRIu64
22 #define NSEC_FMT "%" PRI_NSEC
23 #define USEC_FMT "%" PRI_USEC
24
25 #include "macro.h"
26
27 typedef struct dual_timestamp {
28         usec_t realtime;
29         usec_t monotonic;
30 } dual_timestamp;
31
32 typedef struct triple_timestamp {
33         usec_t realtime;
34         usec_t monotonic;
35         usec_t boottime;
36 } triple_timestamp;
37
38 #define USEC_INFINITY ((usec_t) -1)
39 #define NSEC_INFINITY ((nsec_t) -1)
40
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)
47
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))
60
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
67
68 #define TIME_T_MAX (time_t)((UINTMAX_C(1) << ((sizeof(time_t) << 3) - 1)) - 1)
69
70 #define DUAL_TIMESTAMP_NULL ((struct dual_timestamp) {})
71 #define TRIPLE_TIMESTAMP_NULL ((struct triple_timestamp) {})
72
73 usec_t now(clockid_t clock);
74 #if 0 /// UNNEEDED by elogind
75 nsec_t now_nsec(clockid_t clock);
76 #endif // 0
77
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);
83 #endif // 0
84
85 triple_timestamp* triple_timestamp_get(triple_timestamp *ts);
86 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u);
87
88 #define DUAL_TIMESTAMP_HAS_CLOCK(clock)                               \
89         IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC)
90
91 #define TRIPLE_TIMESTAMP_HAS_CLOCK(clock)                               \
92         IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM)
93
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));
97 }
98
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));
103 }
104
105 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock);
106
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_;
110 #endif // 0
111 struct timespec *timespec_store(struct timespec *ts, usec_t u);
112
113 usec_t timeval_load(const struct timeval *tv) _pure_;
114 struct timeval *timeval_store(struct timeval *tv, usec_t u);
115
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);
119 #endif // 0
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);
123 #endif // 0
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);
126
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);
130 #endif // 0
131 int timestamp_deserialize(const char *value, usec_t *timestamp);
132
133 #if 0 /// UNNEEDED by elogind
134 int parse_timestamp(const char *t, usec_t *usec);
135 #endif // 0
136
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);
140 #endif // 0
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);
144
145 bool ntp_synced(void);
146
147 int get_timezones(char ***l);
148 bool timezone_is_valid(const char *name, int log_level);
149
150 #endif // 0
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);
155
156 usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);
157 #endif // 0
158
159 #if 0 /// UNNEEDED by elogind
160 int get_timezone(char **timezone);
161
162 time_t mktime_or_timegm(struct tm *tm, bool utc);
163 #endif // 0
164 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc);
165
166 #if 0 /// UNNEEDED by elogind
167 unsigned long usec_to_jiffies(usec_t usec);
168 #endif // 0
169
170 bool in_utc_timezone(void);
171
172 static inline usec_t usec_add(usec_t a, usec_t b) {
173         usec_t c;
174
175         /* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output, and doesn't
176          * overflow. */
177
178         c = a + b;
179         if (c < a || c < b) /* overflow check */
180                 return USEC_INFINITY;
181
182         return c;
183 }
184
185 static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
186
187         if (timestamp == USEC_INFINITY) /* Make sure infinity doesn't degrade */
188                 return USEC_INFINITY;
189         if (timestamp < delta)
190                 return 0;
191
192         return timestamp - delta;
193 }
194
195 static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
196         if (delta < 0)
197                 return usec_add(timestamp, (usec_t) (-delta));
198         else
199                 return usec_sub_unsigned(timestamp, (usec_t) delta);
200 }
201
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)
209 #else
210 #error "Yuck, time_t is neither 4 nor 8 bytes wide?"
211 #endif
212
213 int time_change_fd(void);