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
11 #define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL
12 #include "time_util.h"
16 #include <wsutil/epochs.h>
20 #include <sys/resource.h>
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.
35 mktime_utc(struct tm
*tm
)
40 * We don't have timegm(), so use code copied from Glib source
43 static const int days_before
[] =
45 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
50 if (tm
->tm_mon
< 0 || tm
->tm_mon
> 11) {
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
))
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.
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.
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) ||
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").
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) {
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
])) {
137 if (tm
->tm_hour
< 0 || tm
->tm_hour
> 23) {
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) {
146 if (tm
->tm_sec
< 0 || tm
->tm_sec
> 60) {
147 /* 60, not 59, to account for leap seconds */
153 void get_resource_usage(double *user_time
, double *sys_time
) {
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);
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;
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
, ...) {
182 GString
*log_str
= g_string_new("");
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
);
201 ws_warning("%s", log_str
->str
);
202 g_string_free(log_str
, TRUE
);
206 /* Copied from pcapio.c pcapng_write_interface_statistics_block()*/
208 create_timestamp(void) {
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,
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;
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
);
258 ws_clock_get_realtime(struct timespec
*ts
)
260 #if defined(HAVE_CLOCK_GETTIME)
261 if (clock_gettime(CLOCK_REALTIME
, ts
) == 0)
263 #elif defined(HAVE_TIMESPEC_GET)
264 if (timespec_get(ts
, TIME_UTC
) == TIME_UTC
)
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;
276 /* Fall back on time(). */
277 ts
->tv_sec
= time(NULL
);
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
);
294 struct tm
*aux
= localtime(timep
);
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
);
320 struct tm
*aux
= gmtime(timep
);
329 * Editor modelines - https://www.wireshark.org/tools/modelines.html
334 * indent-tabs-mode: t
337 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
338 * :indentSize=8:tabSize=8:noTabs=false: