xref: /llvm-project/llvm/lib/Support/Chrono.cpp (revision 59838f7ea675e5b3251d730b59325786b3f6e68c)
1 //===- Support/Chrono.cpp - 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 #include "llvm/Support/Chrono.h"
11 #include "llvm/Config/config.h"
12 #include "llvm/Support/Format.h"
13 #include "llvm/Support/raw_ostream.h"
14 
15 namespace llvm {
16 
17 using namespace sys;
18 
19 static inline struct tm getStructTM(TimePoint<> TP) {
20   struct tm Storage;
21   std::time_t OurTime = toTimeT(TP);
22 
23 #if defined(LLVM_ON_UNIX)
24   struct tm *LT = ::localtime_r(&OurTime, &Storage);
25   assert(LT);
26   (void)LT;
27 #endif
28 #if defined(LLVM_ON_WIN32)
29   int Error = ::localtime_s(&Storage, &OurTime);
30   assert(!Error);
31   (void)Error;
32 #endif
33 
34   return Storage;
35 }
36 
37 raw_ostream &operator<<(raw_ostream &OS, TimePoint<> TP) {
38   struct tm LT = getStructTM(TP);
39   char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
40   strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", &LT);
41   return OS << Buffer << '.'
42             << format("%.9lu",
43                       long((TP.time_since_epoch() % std::chrono::seconds(1))
44                                .count()));
45 }
46 
47 } // namespace llvm
48