diff options
Diffstat (limited to 'src/buildtool/system/system.hpp')
-rw-r--r-- | src/buildtool/system/system.hpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/buildtool/system/system.hpp b/src/buildtool/system/system.hpp index 2038d5c7..fc67471b 100644 --- a/src/buildtool/system/system.hpp +++ b/src/buildtool/system/system.hpp @@ -1,10 +1,46 @@ #ifndef INCLUDED_SRC_BUILDTOOL_SYSTEM_SYSTEM_HPP #define INCLUDED_SRC_BUILDTOOL_SYSTEM_SYSTEM_HPP +#include <chrono> + +#include "src/utils/cpp/concepts.hpp" + namespace System { void ExitWithoutCleanup(int exit_code); +/// \brief Obtain POSIX epoch time for a given clock. +/// Clocks may have different epoch times. To obtain the POSIX epoch time +/// (1970-01-01 00:00:00 UTC) for a given clock, it must be converted. +template <class T_Clock> +static auto GetPosixEpoch() -> std::chrono::time_point<T_Clock> { + // Since C++20, the system clock's default value is the POSIX epoch time. + std::chrono::time_point<std::chrono::system_clock> sys_epoch{}; + if constexpr (std::is_same_v<T_Clock, std::chrono::system_clock>) { + // No conversion necessary for the system clock. + return sys_epoch; + } + else if constexpr (ClockHasFromSys<T_Clock>) { + // The correct C++20 way to perform the time point conversion. + return T_Clock::from_sys(sys_epoch); + } + else if constexpr (ClockHasFromTime<T_Clock>) { + // Older releases of libcxx did not implement the standard conversion + // function from_sys() for std::chrono::file_clock. Instead the + // non-standard function file_clock::from_time_t() must be used. Since + // libcxx 14.0.0, this has been fixed by: + // - https://reviews.llvm.org/D113027 + // - https://reviews.llvm.org/D113430 + // TODO(modernize): remove this once we require clang version >= 14.0.0 + return T_Clock::from_time_t( + std::chrono::system_clock::to_time_t(sys_epoch)); + } + static_assert(std::is_same_v<T_Clock, std::chrono::system_clock> or + ClockHasFromSys<T_Clock> or ClockHasFromTime<T_Clock>, + "Time point conversion function unavailable."); + return {}; } +} // namespace System + #endif // INCLUDED_SRC_BUILDTOOL_SYSTEM_SYSTEM_HPP |