Created
March 13, 2017 20:24
-
-
Save davidalbertonogueira/8f9374c2050ec0bbb772704e6748e1b8 to your computer and use it in GitHub Desktop.
std::chrono::system_clock with microseconds duration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @brief Created on top of std::chrono::system_clock, | |
// MicroSecResolutionClock offers a clock with microseconds duration. | |
struct MicroSecResolutionClock : public std::chrono::system_clock { | |
typedef std::chrono::microseconds duration; | |
typedef duration::rep rep; | |
typedef duration::period period; | |
typedef std::chrono::time_point<MicroSecResolutionClock, duration> time_point; | |
static const bool is_steady = false; | |
static const int ticks_per_time_t = period::den; | |
static time_point now() noexcept { | |
auto system_clock_duration = | |
std::chrono::duration< std::chrono::system_clock::rep, | |
std::chrono::system_clock::period> | |
(std::chrono::system_clock::now().time_since_epoch()); | |
return time_point( | |
std::chrono::duration_cast <duration>(system_clock_duration) | |
); | |
} | |
static | |
#if defined(_MSC_VER) | |
__time64_t to_time_t(const time_point& _Time) noexcept { | |
return | |
((__time64_t) | |
#endif | |
#ifdef __linux__ | |
std::time_t to_time_t(const time_point& _Time) noexcept { | |
return | |
((std::time_t) | |
#endif | |
(std::chrono::duration_cast <std::chrono::seconds>(_Time.time_since_epoch()) | |
.count())); | |
} | |
static | |
#if defined(_MSC_VER) | |
time_point from_time_t(__time64_t _Tm) noexcept { | |
#endif | |
#ifdef __linux__ | |
time_point from_time_t(std::time_t _Tm) noexcept { | |
#endif | |
return | |
(time_point(std::chrono::duration_cast <duration>( | |
std::chrono::duration<std::chrono::seconds::rep>(_Tm)))); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment