2 * Copyright (c) 2021 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
12 timespec_to_ms(const struct timespec *ts)
16 if (ts->tv_sec < 0 || ts->tv_nsec < 0 ||
17 ts->tv_nsec >= 1000000000LL)
20 if ((uint64_t)ts->tv_sec >= INT64_MAX / 1000LL)
23 x = ts->tv_sec * 1000LL;
24 y = ts->tv_nsec / 1000000LL;
26 if (INT64_MAX - x < y || x + y > INT_MAX)
33 fido_time_now(struct timespec *ts_now)
35 if (clock_gettime(CLOCK_MONOTONIC, ts_now) != 0) {
36 fido_log_error(errno, "%s: clock_gettime", __func__);
44 fido_time_delta(const struct timespec *ts_start, int *ms_remain)
46 struct timespec ts_end, ts_delta;
52 if (clock_gettime(CLOCK_MONOTONIC, &ts_end) != 0) {
53 fido_log_error(errno, "%s: clock_gettime", __func__);
57 if (timespeccmp(&ts_end, ts_start, <)) {
58 fido_log_debug("%s: timespeccmp", __func__);
62 timespecsub(&ts_end, ts_start, &ts_delta);
64 if ((ms = timespec_to_ms(&ts_delta)) < 0) {
65 fido_log_debug("%s: timespec_to_ms", __func__);