blob: cdadbd87997948dacc2c7151bb4acb71394f1d47 [file] [log] [blame]
Pavel Labath59838f72016-10-20 12:05:50 +00001//===- 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
15namespace llvm {
16
17using namespace sys;
18
19static 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
37raw_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