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