libmdbx/test/chrono.h

101 lines
2.2 KiB
C
Raw Normal View History

2017-04-12 00:00:26 +08:00
/*
* Copyright 2017-2019 Leonid Yuriev <leo@yuriev.ru>
2017-04-12 00:00:26 +08:00
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#pragma once
#include "base.h"
#include "log.h"
#include "utils.h"
namespace chrono {
#pragma pack(push, 1)
2017-04-12 00:00:26 +08:00
typedef union time {
uint64_t fixedpoint;
struct {
2017-04-12 00:00:26 +08:00
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint32_t fractional;
2017-04-21 23:33:35 +08:00
union {
uint32_t utc;
uint32_t integer;
};
2017-04-12 00:00:26 +08:00
#else
2017-04-21 23:33:35 +08:00
union {
uint32_t utc;
uint32_t integer;
};
2017-04-12 00:00:26 +08:00
uint32_t fractional;
#endif
};
2017-04-21 23:33:35 +08:00
void reset() { fixedpoint = 0; }
uint32_t seconds() const { return utc; }
2017-04-12 00:00:26 +08:00
} time;
#pragma pack(pop)
2017-04-12 00:00:26 +08: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 23:33:35 +08:00
inline time from_seconds(uint64_t seconds) {
assert(seconds < UINT32_MAX);
time result;
result.fixedpoint = seconds << 32;
return result;
}
2017-04-12 00:00:26 +08:00
inline time from_utc(time_t utc) {
2017-04-21 23:33:35 +08:00
assert(utc >= 0);
return from_seconds((uint64_t)utc);
2017-04-21 23:33:35 +08:00
}
inline time infinite() {
2017-04-12 00:00:26 +08:00
time result;
2017-04-21 23:33:35 +08:00
result.fixedpoint = UINT64_MAX;
2017-04-12 00:00:26 +08: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 23:33:35 +08:00
time now_realtime();
time now_motonic();
2017-04-12 00:00:26 +08:00
} /* namespace chrono */