2017-04-12 00:00:26 +08:00
|
|
|
/*
|
2021-01-26 12:26:09 +08:00
|
|
|
* Copyright 2017-2021 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 "utils.h"
|
|
|
|
|
|
|
|
namespace chrono {
|
|
|
|
|
2017-10-29 05:56:05 +08:00
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
2017-04-12 00:00:26 +08:00
|
|
|
typedef union time {
|
|
|
|
uint64_t fixedpoint;
|
2019-10-26 05:02:28 +08:00
|
|
|
__anonymous_struct_extension__ 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;
|
|
|
|
|
2017-10-29 05:56:05 +08:00
|
|
|
#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);
|
2017-07-26 20:56:39 +08:00
|
|
|
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();
|
2020-09-22 11:51:47 +08:00
|
|
|
time now_monotonic();
|
2017-04-12 00:00:26 +08:00
|
|
|
|
|
|
|
} /* namespace chrono */
|