diff options
Diffstat (limited to 'src/xtime.h')
-rw-r--r-- | src/xtime.h | 114 |
1 files changed, 68 insertions, 46 deletions
diff --git a/src/xtime.h b/src/xtime.h index ceff48f..b76fef2 100644 --- a/src/xtime.h +++ b/src/xtime.h @@ -1,18 +1,5 @@ -/**************************************************************************** - * bfs * - * Copyright (C) 2020-2022 Tavian Barnes <tavianator@tavianator.com> * - * * - * Permission to use, copy, modify, and/or distribute this software for any * - * purpose with or without fee is hereby granted. * - * * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - ****************************************************************************/ +// Copyright © Tavian Barnes <tavianator@tavianator.com> +// SPDX-License-Identifier: 0BSD /** * Date/time handling. @@ -24,63 +11,98 @@ #include <time.h> /** - * localtime_r() wrapper that calls tzset() first. + * mktime() wrapper that reports errors more reliably. * - * @param[in] timep - * The time_t to convert. - * @param[out] result - * Buffer to hold the result. + * @tm[in,out] + * The struct tm to convert and normalize. + * @timep[out] + * A pointer to the result. * @return * 0 on success, -1 on failure. */ -int xlocaltime(const time_t *timep, struct tm *result); +int xmktime(struct tm *tm, time_t *timep); /** - * gmtime_r() wrapper that calls tzset() first. + * A portable timegm(), the inverse of gmtime(). * - * @param[in] timep - * The time_t to convert. - * @param[out] result - * Buffer to hold the result. + * @tm[in,out] + * The struct tm to convert and normalize. + * @timep[out] + * A pointer to the result. * @return * 0 on success, -1 on failure. */ -int xgmtime(const time_t *timep, struct tm *result); +int xtimegm(struct tm *tm, time_t *timep); /** - * mktime() wrapper that reports errors more reliably. + * Parse an ISO 8601-style timestamp. * - * @param[in,out] tm - * The struct tm to convert. - * @param[out] timep + * @str + * The string to parse. + * @result[out] * A pointer to the result. * @return * 0 on success, -1 on failure. */ -int xmktime(struct tm *tm, time_t *timep); +int xgetdate(const char *str, struct timespec *result); /** - * A portable timegm(), the inverse of gmtime(). + * Add to a timespec. + */ +void timespec_add(struct timespec *lhs, const struct timespec *rhs); + +/** + * Subtract from a timespec. + */ +void timespec_sub(struct timespec *lhs, const struct timespec *rhs); + +/** + * Compare two timespecs. * - * @param[in,out] tm - * The struct tm to convert. - * @param[out] timep - * A pointer to the result. * @return - * 0 on success, -1 on failure. + * An integer with the sign of (*lhs - *rhs). */ -int xtimegm(struct tm *tm, time_t *timep); +int timespec_cmp(const struct timespec *lhs, const struct timespec *rhs); /** - * Parse an ISO 8601-style timestamp. + * Update a minimum timespec. + */ +void timespec_min(struct timespec *dest, const struct timespec *src); + +/** + * Update a maximum timespec. + */ +void timespec_max(struct timespec *dest, const struct timespec *src); + +/** + * Convert a timespec to floating point. * - * @param[in] str - * The string to parse. - * @param[out] result - * A pointer to the result. * @return - * 0 on success, -1 on failure. + * The value in nanoseconds. + */ +double timespec_ns(const struct timespec *ts); + +/** + * A timer. + */ +struct timer; + +/** + * Start a timer. + * + * @interval + * The regular interval at which to send SIGALRM. + * @return + * The new timer on success, otherwise NULL. + */ +struct timer *xtimer_start(const struct timespec *interval); + +/** + * Stop a timer. + * + * @timer + * The timer to stop. */ -int parse_timestamp(const char *str, struct timespec *result); +void xtimer_stop(struct timer *timer); #endif // BFS_XTIME_H |