rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rtime.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2015 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_TIME_H__
19#define __R_TIME_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
31#include <rlib/rtypes.h>
32
53#define R_CLOCK_TIME_IS_VALID(time) (((RClockTime)(time)) != R_CLOCK_TIME_NONE)
54
56#define R_NSECOND RINT64_CONSTANT (1)
58#define R_USECOND RINT64_CONSTANT (1000)
60#define R_MSECOND RINT64_CONSTANT (1000000)
62#define R_SECOND RINT64_CONSTANT (1000000000)
64#define R_MSEC_PER_SEC 1000
66#define R_USEC_PER_SEC 1000000
68#define R_NSEC_PER_SEC 1000000000
70#define R_TIME_AS_HOURS(time) ((time) / (R_SECOND * 60 * 60))
72#define R_TIME_AS_MINUTES(time) ((time) / (R_SECOND * 60))
74#define R_TIME_AS_SECONDS(time) ((time) / R_SECOND)
76#define R_TIME_AS_MSECONDS(time) ((time) / R_MSECOND)
78#define R_TIME_AS_USECONDS(time) ((time) / R_USECOND)
80#define R_TIME_AS_NSECONDS(time) ((time) / R_NSECOND)
81
83#define R_CLOCK_DIFF(s, e) (RClockTimeDiff)((e) - (s))
84
86#define R_TIMESPEC_TO_TIME(ts) (RClockTime)((ts).tv_sec * R_SECOND + (ts).tv_nsec * R_NSECOND)
88#define R_TIME_TO_TIMESPEC(t,ts) \
89R_STMT_START { \
90 (ts).tv_sec = (rlong) ( (t) / R_SECOND); \
91 (ts).tv_nsec = (rlong) (((t) % R_SECOND) / R_NSECOND); \
92} R_STMT_END
94#define R_TIME_TIMESPEC_INIT(t) { (rlong) ( (t) / R_SECOND), \
95 (rlong) (((t) % R_SECOND) / R_NSECOND) }
96
98#define R_TIMEVAL_TO_TIME(tv) (RClockTime)((tv).tv_sec * R_SECOND + (tv).tv_usec * R_USECOND)
100#define R_TIME_TO_TIMEVAL(t,tv) \
101R_STMT_START { \
102 (tv).tv_sec = (rlong) ( (t) / R_SECOND); \
103 (tv).tv_usec = (rlong) (((t) % R_SECOND) / R_USECOND); \
104} R_STMT_END
106#define R_TIME_TIMEVAL_INIT(t) { (rlong) ( (t) / R_SECOND), \
107 (rlong) (((t) % R_SECOND) / R_USECOND) }
108
116#define R_TIME_FORMAT "u:%02u:%02u.%09u"
118#define R_TIME_ARGS(t) \
119 R_CLOCK_TIME_IS_VALID (t) ? (ruint) R_TIME_AS_HOURS (t) : 99, \
120 R_CLOCK_TIME_IS_VALID (t) ? (ruint) R_TIME_AS_MINUTES (t) % 60 : 99, \
121 R_CLOCK_TIME_IS_VALID (t) ? (ruint) R_TIME_AS_SECONDS (t) % 60 : 99, \
122 R_CLOCK_TIME_IS_VALID (t) ? (ruint) ((t) % R_SECOND) : 999999999
123
125
143
146
161 ruint8 hour, ruint8 minute, ruint8 second);
168 ruint16 * year, ruint8 * month, ruint8 * day,
169 ruint8 * hour, ruint8 * minute, ruint8 * second);
170
174#define r_time_get_time_since_epoch() r_time_get_unix_time ()
175
176R_END_DECLS
177
180#endif /* __R_TIME_H__ */
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
RClockTime r_time_get_ts_monotonic(void)
Read the monotonic timestamp (CLOCK_MONOTONIC); strictly non-decreasing across calls in a single boot...
RClockTime r_time_get_ts_raw(void)
Read the raw monotonic timestamp (CLOCK_MONOTONIC_RAW on Linux); not subject to NTP slewing.
rboolean r_time_parse_unix_time(ruint64 time, ruint16 *year, ruint8 *month, ruint8 *day, ruint8 *hour, ruint8 *minute, ruint8 *second)
Split a Unix timestamp into broken-down date components.
ruint64 r_time_create_unix_time(ruint16 year, ruint8 month, ruint8 day, ruint8 hour, ruint8 minute, ruint8 second)
Convert a broken-down date to a Unix timestamp (seconds since 1970-01-01 UTC).
ruint16 r_time_leap_years(ruint16 from, ruint16 to)
Count leap years in the half-open range [from, to), with from clamped up to 1753.
ruint64 r_time_get_uptime(void)
Return system uptime in seconds.
RClockTime r_time_get_ts_wallclock(void)
Read the wall-clock real-time timestamp (CLOCK_REALTIME).
rint8 r_time_days_in_month(ruint16 year, ruint8 month)
Days in month (1-12) of year, accounting for leap years; -1 if month is out of range or year is befor...
rboolean r_time_is_leap_year(ruint16 year)
TRUE if year is a Gregorian leap year. Years before 1753 always return FALSE.
ruint64 r_time_get_unix_time(void)
Return the current Unix timestamp (seconds since 1970-01-01 UTC).
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
signed char rint8
Signed 8-bit integer.
Definition rtypes.h:185
unsigned char ruint8
Unsigned 8-bit integer.
Definition rtypes.h:190
unsigned long ruint64
Unsigned 64-bit integer.
Definition rtypes.h:193
unsigned short ruint16
Unsigned 16-bit integer.
Definition rtypes.h:191
ruint64 RClockTime
Unsigned 64-bit timestamp / duration, in nanoseconds.
Definition rtypes.h:353
Foundational type aliases used by every rlib header.