TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / wsutil / time_util.c
blob1fda2c57416a7ca91445f5c98e05b9d307498cee
1 /* time_util.c
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "config.h"
11 #define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL
12 #include "time_util.h"
14 #include <errno.h>
16 #include <wsutil/epochs.h>
18 #ifndef _WIN32
19 #include <sys/time.h>
20 #include <sys/resource.h>
21 #else
22 #include <windows.h>
23 #endif
25 /* Test if the given year is a leap year */
26 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
28 /* converts a broken down date representation, relative to UTC,
29 * to a timestamp; it uses timegm() if it's available.
31 * Returns -1 and sets errno to EINVAL on error; returns the timestamp
32 * and sets errno to 0 on success.
34 time_t
35 mktime_utc(struct tm *tm)
37 time_t retval;
38 #ifndef HAVE_TIMEGM
40 * We don't have timegm(), so use code copied from Glib source
41 * gtimer.c.
43 static const int days_before[] =
45 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
48 int yr;
50 if (tm->tm_mon < 0 || tm->tm_mon > 11) {
51 errno = EINVAL;
52 return (time_t) -1;
55 retval = (tm->tm_year - 70) * 365;
57 /* count number of leap years */
58 yr = tm->tm_year + 1900;
59 if (tm->tm_mon + 1 < 3 && isleap(yr))
60 yr--;
61 retval += (((yr / 4) - (yr / 100) + (yr / 400)) - 477); /* 477 = ((1970 / 4) - (1970 / 100) + (1970 / 400)) */
63 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
65 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
68 * Just in case somebody asked for 1969-12-31 23:59:59 UTC,
69 * which is one second before the Unix epoch.
71 errno = 0;
72 return retval;
73 #else
74 retval = timegm(tm);
76 * If passed a struct tm for 2013-03-01 00:00:00, both
77 * macOS and FreeBSD timegm() return the epoch time
78 * value for 2013-03-01 00:00:00 UTC, but also set
79 * errno to EOVERFLOW. This may be true of other
80 * implementations based on the tzcode reference
81 * impelementation of timegm().
83 * The macOS and FreeBSD documentation for timegm() neither
84 * commit to leaving errno alone nor commit to setting it
85 * to a particular value.
87 * Force errno to 0, and check for an error and set it to
88 * EINVAL iff we got an error.
90 errno = 0;
91 if (retval == (time_t)-1) {
93 * Did somebody ask for 1969-12-31 23:59:59 UTC,
94 * which is one second before the Unix epoch?
96 * If so, timegm() happened to return the correct
97 * timestamp (whether because it calculated it or
98 * because it failed in some fashion).
100 * If not, set errno to EINVAL.
102 if (tm->tm_year != (1969 - 1900) ||
103 tm->tm_mon != (12 - 1) ||
104 tm->tm_mday != 31 ||
105 tm->tm_hour != 23 ||
106 tm->tm_min != 59 ||
107 tm->tm_sec != 59)
108 errno = EINVAL;
110 return retval;
111 #endif /* !HAVE_TIMEGM */
114 /* Validate the values in a time_t
115 * Currently checks tm_year, tm_mon, tm_mday, tm_hour, tm_min, and tm_sec;
116 * disregards tm_wday, tm_yday, and tm_isdst.
117 * Use this in situations where you wish to return an error rather than
118 * normalizing invalid dates; otherwise you could specify, for example,
119 * 2020-10-40 (to quote the macOS and probably *BSD manual
120 * page for ctime()/localtime()/mktime()/etc., "October 40
121 * is changed into November 9").
123 bool
124 tm_is_valid(struct tm *tm)
126 static const int8_t days_in_month[12] = {
127 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
130 if (tm->tm_mon < 0 || tm->tm_mon > 11) {
131 return false;
133 if (tm->tm_mday < 0 || tm->tm_mday >
134 ((tm->tm_mon == 1 && isleap(tm->tm_year)) ? 29 : days_in_month[tm->tm_mon])) {
135 return false;
137 if (tm->tm_hour < 0 || tm->tm_hour > 23) {
138 return false;
140 /* XXX: ISO 8601 and others allow 24:00:00 for end of day, perhaps that
141 * one case should be allowed?
143 if (tm->tm_min < 0 || tm->tm_min > 59) {
144 return false;
146 if (tm->tm_sec < 0 || tm->tm_sec > 60) {
147 /* 60, not 59, to account for leap seconds */
148 return false;
150 return true;
153 void get_resource_usage(double *user_time, double *sys_time) {
154 #ifndef _WIN32
155 struct rusage ru;
157 getrusage(RUSAGE_SELF, &ru);
159 *user_time = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 1000000.0);
160 *sys_time = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 1000000.0);
161 #else /* _WIN32 */
162 HANDLE h_proc = GetCurrentProcess();
163 FILETIME cft, eft, kft, uft;
164 ULARGE_INTEGER uli_time;
166 GetProcessTimes(h_proc, &cft, &eft, &kft, &uft);
168 uli_time.LowPart = uft.dwLowDateTime;
169 uli_time.HighPart = uft.dwHighDateTime;
170 *user_time = uli_time.QuadPart / 10000000.0;
171 uli_time.LowPart = kft.dwLowDateTime;
172 uli_time.HighPart = kft.dwHighDateTime;
173 *sys_time = uli_time.QuadPart / 1000000000.0;
174 #endif /* _WIN32 */
177 static double last_user_time = 0.0;
178 static double last_sys_time = 0.0;
180 void log_resource_usage(bool reset_delta, const char *format, ...) {
181 va_list ap;
182 GString *log_str = g_string_new("");
183 double user_time;
184 double sys_time;
186 get_resource_usage(&user_time, &sys_time);
188 if (reset_delta || last_user_time == 0.0) {
189 last_user_time = user_time;
190 last_sys_time = sys_time;
193 g_string_append_printf(log_str, "user %.3f +%.3f sys %.3f +%.3f ",
194 user_time, user_time - last_user_time,
195 sys_time, sys_time - last_sys_time);
197 va_start(ap, format);
198 g_string_append_vprintf(log_str, format, ap);
199 va_end(ap);
201 ws_warning("%s", log_str->str);
202 g_string_free(log_str, TRUE);
206 /* Copied from pcapio.c pcapng_write_interface_statistics_block()*/
207 uint64_t
208 create_timestamp(void) {
209 uint64_t timestamp;
210 #ifdef _WIN32
211 FILETIME now;
212 #else
213 struct timeval now;
214 #endif
216 #ifdef _WIN32
218 * Current time, represented as 100-nanosecond intervals since
219 * January 1, 1601, 00:00:00 UTC.
221 * I think DWORD might be signed, so cast both parts of "now"
222 * to uint32_t so that the sign bit doesn't get treated specially.
224 * Windows 8 provides GetSystemTimePreciseAsFileTime which we
225 * might want to use instead.
227 GetSystemTimeAsFileTime(&now);
228 timestamp = (((uint64_t)(uint32_t)now.dwHighDateTime) << 32) +
229 (uint32_t)now.dwLowDateTime;
232 * Convert to same thing but as 1-microsecond, i.e. 1000-nanosecond,
233 * intervals.
235 timestamp /= 10;
238 * Subtract difference, in microseconds, between January 1, 1601
239 * 00:00:00 UTC and January 1, 1970, 00:00:00 UTC.
241 timestamp -= EPOCH_DELTA_1601_01_01_00_00_00_UTC*1000000;
242 #else
244 * Current time, represented as seconds and microseconds since
245 * January 1, 1970, 00:00:00 UTC.
247 gettimeofday(&now, NULL);
250 * Convert to delta in microseconds.
252 timestamp = (uint64_t)(now.tv_sec) * 1000000 + (uint64_t)(now.tv_usec);
253 #endif
254 return timestamp;
257 struct timespec *
258 ws_clock_get_realtime(struct timespec *ts)
260 #if defined(HAVE_CLOCK_GETTIME)
261 if (clock_gettime(CLOCK_REALTIME, ts) == 0)
262 return ts;
263 #elif defined(HAVE_TIMESPEC_GET)
264 if (timespec_get(ts, TIME_UTC) == TIME_UTC)
265 return ts;
266 #endif
268 #ifndef _WIN32
269 /* Fall back on gettimeofday(). */
270 struct timeval usectimenow;
271 gettimeofday(&usectimenow, NULL);
272 ts->tv_sec = usectimenow.tv_sec;
273 ts->tv_nsec = usectimenow.tv_usec*1000;
274 return ts;
275 #else
276 /* Fall back on time(). */
277 ts->tv_sec = time(NULL);
278 ts->tv_nsec = 0;
279 return ts;
280 #endif
283 struct tm *
284 ws_localtime_r(const time_t *timep, struct tm *result)
286 #if defined(HAVE_LOCALTIME_R)
287 return localtime_r(timep, result);
288 #elif defined(_MSC_VER)
289 errno_t err = localtime_s(result, timep);
290 if (err == 0)
291 return result;
292 return NULL;
293 #else
294 struct tm *aux = localtime(timep);
295 if (aux == NULL)
296 return NULL;
297 *result = *aux;
298 return result;
299 #endif
302 void ws_tzset(void)
304 #ifdef HAVE_TZSET
305 tzset();
306 #endif
309 struct tm *
310 ws_gmtime_r(const time_t *timep, struct tm *result)
312 #if defined(HAVE_GMTIME_R)
313 return gmtime_r(timep, result);
314 #elif defined(_MSC_VER)
315 errno_t err = gmtime_s(result, timep);
316 if (err == 0)
317 return result;
318 return NULL;
319 #else
320 struct tm *aux = gmtime(timep);
321 if (aux == NULL)
322 return NULL;
323 *result = *aux;
324 return result;
325 #endif
329 * Editor modelines - https://www.wireshark.org/tools/modelines.html
331 * Local variables:
332 * c-basic-offset: 8
333 * tab-width: 8
334 * indent-tabs-mode: t
335 * End:
337 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
338 * :indentSize=8:tabSize=8:noTabs=false: