libmdbx/test/chrono.h++

89 lines
1.9 KiB
C++
Raw Normal View History

/// \author Леонид Юрьев aka Leonid Yuriev <leo@yuriev.ru> \date 2015-2024
/// \copyright SPDX-License-Identifier: Apache-2.0
2017-04-11 19:00:26 +03:00
#pragma once
#include "base.h++"
#include "utils.h++"
2017-04-11 19:00:26 +03:00
namespace chrono {
#pragma pack(push, 4)
2017-04-11 19:00:26 +03:00
typedef union time {
uint64_t fixedpoint;
__anonymous_struct_extension__ struct {
2017-04-11 19:00:26 +03:00
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint32_t fractional;
2017-04-21 18:33:35 +03:00
union {
uint32_t utc;
uint32_t integer;
};
2017-04-11 19:00:26 +03:00
#else
2017-04-21 18:33:35 +03:00
union {
uint32_t utc;
uint32_t integer;
};
2017-04-11 19:00:26 +03:00
uint32_t fractional;
#endif
};
2017-04-21 18:33:35 +03:00
void reset() { fixedpoint = 0; }
uint32_t seconds() const { return utc; }
2017-04-11 19:00:26 +03:00
} time;
#pragma pack(pop)
2017-04-11 19:00:26 +03:00
uint32_t ns2fractional(uint32_t);
uint32_t fractional2ns(uint32_t);
uint32_t us2fractional(uint32_t);
uint32_t fractional2us(uint32_t);
uint32_t ms2fractional(uint32_t);
uint32_t fractional2ms(uint32_t);
time from_ns(uint64_t us);
time from_us(uint64_t ns);
time from_ms(uint64_t ms);
2017-04-21 18:33:35 +03:00
inline time from_seconds(uint64_t seconds) {
assert(seconds < UINT32_MAX);
time result;
result.fixedpoint = seconds << 32;
return result;
}
2017-04-11 19:00:26 +03:00
inline time from_utc(time_t utc) {
2017-04-21 18:33:35 +03:00
assert(utc >= 0);
return from_seconds((uint64_t)utc);
2017-04-21 18:33:35 +03:00
}
inline time infinite() {
2017-04-11 19:00:26 +03:00
time result;
2017-04-21 18:33:35 +03:00
result.fixedpoint = UINT64_MAX;
2017-04-11 19:00:26 +03:00
return result;
}
#if defined(HAVE_TIMESPEC_TV_NSEC) || defined(__timespec_defined) || \
defined(CLOCK_REALTIME)
inline time from_timespec(const struct timespec &ts) {
time result;
result.fixedpoint =
((uint64_t)ts.tv_sec << 32) | ns2fractional((uint32_t)ts.tv_nsec);
return result;
}
#endif /* HAVE_TIMESPEC_TV_NSEC */
#if defined(HAVE_TIMEVAL_TV_USEC) || defined(_STRUCT_TIMEVAL)
inline time from_timeval(const struct timeval &tv) {
time result;
result.fixedpoint =
((uint64_t)tv.tv_sec << 32) | us2fractional((uint32_t)tv.tv_usec);
return result;
}
#endif /* HAVE_TIMEVAL_TV_USEC */
2017-04-21 18:33:35 +03:00
time now_realtime();
time now_monotonic();
2017-04-11 19:00:26 +03:00
} /* namespace chrono */