1 //===- llvm/Support/Chrono.h - Utilities for Timing Manipulation-*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_CHRONO_H
11 #define LLVM_SUPPORT_CHRONO_H
12 
13 #include "llvm/Support/Compiler.h"
14 
15 #include <chrono>
16 #include <ctime>
17 
18 namespace llvm {
19 
20 class raw_ostream;
21 
22 namespace sys {
23 
24 /// A time point on the system clock. This is provided for two reasons:
25 /// - to insulate us agains subtle differences in behavoir to differences in
26 ///   system clock precision (which is implementation-defined and differs between
27 ///   platforms).
28 /// - to shorten the type name
29 /// The default precision is nanoseconds. If need a specific precision specify
30 /// it explicitly. If unsure, use the default. If you need a time point on a
31 /// clock other than the system_clock, use std::chrono directly.
32 template <typename D = std::chrono::nanoseconds>
33 using TimePoint = std::chrono::time_point<std::chrono::system_clock, D>;
34 
35 /// Convert a TimePoint to std::time_t
toTimeT(TimePoint<> TP)36 LLVM_ATTRIBUTE_ALWAYS_INLINE inline std::time_t toTimeT(TimePoint<> TP) {
37   using namespace std::chrono;
38   return system_clock::to_time_t(
39       time_point_cast<system_clock::time_point::duration>(TP));
40 }
41 
42 /// Convert a std::time_t to a TimePoint
43 LLVM_ATTRIBUTE_ALWAYS_INLINE inline TimePoint<std::chrono::seconds>
toTimePoint(std::time_t T)44 toTimePoint(std::time_t T) {
45   using namespace std::chrono;
46   return time_point_cast<seconds>(system_clock::from_time_t(T));
47 }
48 
49 } // namespace sys
50 
51 raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP);
52 
53 } // namespace llvm
54 
55 #endif // LLVM_SUPPORT_CHRONO_H
56