diff options
Diffstat (limited to 'llvm/include')
-rw-r--r-- | llvm/include/llvm/Support/Chrono.h | 55 | ||||
-rw-r--r-- | llvm/include/llvm/Support/TimeValue.h | 11 |
2 files changed, 66 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/Chrono.h b/llvm/include/llvm/Support/Chrono.h new file mode 100644 index 00000000000..203439cab91 --- /dev/null +++ b/llvm/include/llvm/Support/Chrono.h @@ -0,0 +1,55 @@ +//===- llvm/Support/Chrono.h - Utilities for Timing Manipulation-*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_CHRONO_H +#define LLVM_SUPPORT_CHRONO_H + +#include "llvm/Support/Compiler.h" + +#include <chrono> +#include <ctime> + +namespace llvm { + +class raw_ostream; + +namespace sys { + +/// A time point on the system clock. This is provided for two reasons: +/// - to insulate us agains subtle differences in behavoir to differences in +/// system clock precision (which is implementation-defined and differs between +/// platforms). +/// - to shorten the type name +/// The default precision is nanoseconds. If need a specific precision specify +/// it explicitly. If unsure, use the default. If you need a time point on a +/// clock other than the system_clock, use std::chrono directly. +template <typename D = std::chrono::nanoseconds> +using TimePoint = std::chrono::time_point<std::chrono::system_clock, D>; + +/// Convert a TimePoint to std::time_t +LLVM_ATTRIBUTE_ALWAYS_INLINE inline std::time_t toTimeT(TimePoint<> TP) { + using namespace std::chrono; + return system_clock::to_time_t( + time_point_cast<system_clock::time_point::duration>(TP)); +} + +/// Convert a std::time_t to a TimePoint +LLVM_ATTRIBUTE_ALWAYS_INLINE inline TimePoint<std::chrono::seconds> +toTimePoint(std::time_t T) { + using namespace std::chrono; + return time_point_cast<seconds>(system_clock::from_time_t(T)); +} + +} // namespace sys + +raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP); + +} // namespace llvm + +#endif // LLVM_SUPPORT_CHRONO_H diff --git a/llvm/include/llvm/Support/TimeValue.h b/llvm/include/llvm/Support/TimeValue.h index 6bca58b6bc2..4b6dd15c4bb 100644 --- a/llvm/include/llvm/Support/TimeValue.h +++ b/llvm/include/llvm/Support/TimeValue.h @@ -14,6 +14,7 @@ #ifndef LLVM_SUPPORT_TIMEVALUE_H #define LLVM_SUPPORT_TIMEVALUE_H +#include "llvm/Support/Chrono.h" #include "llvm/Support/DataTypes.h" #include <string> @@ -112,6 +113,11 @@ namespace sys { this->normalize(); } + template<typename D> + TimeValue(TimePoint<D> TP) + : seconds_(sys::toTimeT(TP) + PosixZeroTimeSeconds), + nanos_((TimePoint<>(TP).time_since_epoch() % std::chrono::seconds(1)).count()) {} + /// This is a static constructor that returns a TimeValue that represents /// the current time. /// @brief Creates a TimeValue with the current time (UTC). @@ -121,6 +127,11 @@ namespace sys { /// @name Operators /// @{ public: + operator TimePoint<>() const { + return toTimePoint(seconds_ - PosixZeroTimeSeconds) + + std::chrono::nanoseconds(nanos_); + } + /// Add \p that to \p this. /// @returns this /// @brief Incrementing assignment operator. |