blob: ce7de0dfc3638f9aa2ab3bfc1607abb277f23e80 [file] [log] [blame]
Elliott Hughes11952072013-10-24 15:15:14 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hugheseabc47b2024-11-08 22:05:00 +000029#pragma once
Elliott Hughes11952072013-10-24 15:15:14 -070030
Yabin Cuic9a659c2015-11-05 15:36:08 -080031#include <errno.h>
Elliott Hughes11952072013-10-24 15:15:14 -070032#include <time.h>
33#include <sys/cdefs.h>
34
Yabin Cuic9a659c2015-11-05 15:36:08 -080035#include "private/bionic_constants.h"
36
Elliott Hugheseabc47b2024-11-08 22:05:00 +000037bool timespec_from_timeval(timespec& ts, const timeval& tv);
38void timespec_from_ms(timespec& ts, const int ms);
Elliott Hughes11952072013-10-24 15:15:14 -070039
Elliott Hugheseabc47b2024-11-08 22:05:00 +000040void timeval_from_timespec(timeval& tv, const timespec& ts);
Elliott Hughes11952072013-10-24 15:15:14 -070041
Elliott Hugheseabc47b2024-11-08 22:05:00 +000042void monotonic_time_from_realtime_time(timespec& monotonic_time, const timespec& realtime_time);
43void realtime_time_from_monotonic_time(timespec& realtime_time, const timespec& monotonic_time);
Elliott Hughes11952072013-10-24 15:15:14 -070044
Elliott Hugheseabc47b2024-11-08 22:05:00 +000045static inline int64_t to_ns(const timespec& ts) {
46 return ts.tv_sec * NS_PER_S + ts.tv_nsec;
47}
Tom Cherryac49ced2017-08-17 13:18:52 -070048
Elliott Hugheseabc47b2024-11-08 22:05:00 +000049static inline int64_t to_us(const timeval& tv) {
50 return tv.tv_sec * US_PER_S + tv.tv_usec;
51}
Elliott Hughes11952072013-10-24 15:15:14 -070052
Elliott Hughesdd586f22015-12-16 15:15:58 -080053static inline int check_timespec(const timespec* ts, bool null_allowed) {
54 if (null_allowed && ts == nullptr) {
55 return 0;
56 }
57 // glibc just segfaults if you pass a null timespec.
58 // That seems a lot more likely to catch bad code than returning EINVAL.
59 if (ts->tv_nsec < 0 || ts->tv_nsec >= NS_PER_S) {
60 return EINVAL;
61 }
62 if (ts->tv_sec < 0) {
63 return ETIMEDOUT;
Yabin Cuic9a659c2015-11-05 15:36:08 -080064 }
65 return 0;
66}
67
Elliott Hughes5db4b6e2016-04-04 16:15:37 -070068#if !defined(__LP64__)
69static inline void absolute_timespec_from_timespec(timespec& abs_ts, const timespec& ts, clockid_t clock) {
70 clock_gettime(clock, &abs_ts);
71 abs_ts.tv_sec += ts.tv_sec;
72 abs_ts.tv_nsec += ts.tv_nsec;
73 if (abs_ts.tv_nsec >= NS_PER_S) {
74 abs_ts.tv_nsec -= NS_PER_S;
75 abs_ts.tv_sec++;
76 }
77}
78#endif