| Pavel Labath | 59838f7 | 2016-10-20 12:05:50 +0000 | [diff] [blame] | 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" | 
| Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 11 | #include "llvm/Config/llvm-config.h" | 
| Pavel Labath | 59838f7 | 2016-10-20 12:05:50 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Format.h" | 
|  | 13 | #include "llvm/Support/raw_ostream.h" | 
|  | 14 |  | 
|  | 15 | namespace llvm { | 
|  | 16 |  | 
|  | 17 | using namespace sys; | 
|  | 18 |  | 
| Kristof Beyls | 77ce4f6 | 2017-03-31 13:06:40 +0000 | [diff] [blame] | 19 | const char llvm::detail::unit<std::ratio<3600>>::value[] = "h"; | 
|  | 20 | const char llvm::detail::unit<std::ratio<60>>::value[] = "m"; | 
|  | 21 | const char llvm::detail::unit<std::ratio<1>>::value[] = "s"; | 
|  | 22 | const char llvm::detail::unit<std::milli>::value[] = "ms"; | 
|  | 23 | const char llvm::detail::unit<std::micro>::value[] = "us"; | 
|  | 24 | const char llvm::detail::unit<std::nano>::value[] = "ns"; | 
| Pavel Labath | 24cb654 | 2017-02-07 18:11:33 +0000 | [diff] [blame] | 25 |  | 
| Pavel Labath | 59838f7 | 2016-10-20 12:05:50 +0000 | [diff] [blame] | 26 | static inline struct tm getStructTM(TimePoint<> TP) { | 
|  | 27 | struct tm Storage; | 
|  | 28 | std::time_t OurTime = toTimeT(TP); | 
|  | 29 |  | 
|  | 30 | #if defined(LLVM_ON_UNIX) | 
|  | 31 | struct tm *LT = ::localtime_r(&OurTime, &Storage); | 
|  | 32 | assert(LT); | 
|  | 33 | (void)LT; | 
|  | 34 | #endif | 
| Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 35 | #if defined(_WIN32) | 
| Pavel Labath | 59838f7 | 2016-10-20 12:05:50 +0000 | [diff] [blame] | 36 | int Error = ::localtime_s(&Storage, &OurTime); | 
|  | 37 | assert(!Error); | 
|  | 38 | (void)Error; | 
|  | 39 | #endif | 
|  | 40 |  | 
|  | 41 | return Storage; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | raw_ostream &operator<<(raw_ostream &OS, TimePoint<> TP) { | 
|  | 45 | struct tm LT = getStructTM(TP); | 
|  | 46 | char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")]; | 
|  | 47 | strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", <); | 
|  | 48 | return OS << Buffer << '.' | 
|  | 49 | << format("%.9lu", | 
|  | 50 | long((TP.time_since_epoch() % std::chrono::seconds(1)) | 
|  | 51 | .count())); | 
|  | 52 | } | 
|  | 53 |  | 
| Sam McCall | fb4a9b7 | 2017-10-24 08:30:19 +0000 | [diff] [blame] | 54 | void format_provider<TimePoint<>>::format(const TimePoint<> &T, raw_ostream &OS, | 
|  | 55 | StringRef Style) { | 
|  | 56 | using namespace std::chrono; | 
|  | 57 | TimePoint<seconds> Truncated = time_point_cast<seconds>(T); | 
|  | 58 | auto Fractional = T - Truncated; | 
|  | 59 | struct tm LT = getStructTM(Truncated); | 
|  | 60 | // Handle extensions first. strftime mangles unknown %x on some platforms. | 
|  | 61 | if (Style.empty()) Style = "%Y-%m-%d %H:%M:%S.%N"; | 
|  | 62 | std::string Format; | 
|  | 63 | raw_string_ostream FStream(Format); | 
|  | 64 | for (unsigned I = 0; I < Style.size(); ++I) { | 
|  | 65 | if (Style[I] == '%' && Style.size() > I + 1) switch (Style[I + 1]) { | 
|  | 66 | case 'L':  // Milliseconds, from Ruby. | 
|  | 67 | FStream << llvm::format( | 
| Simon Dardis | 8bdbff3 | 2017-11-06 23:01:46 +0000 | [diff] [blame] | 68 | "%.3lu", (long)duration_cast<milliseconds>(Fractional).count()); | 
| Sam McCall | fb4a9b7 | 2017-10-24 08:30:19 +0000 | [diff] [blame] | 69 | ++I; | 
|  | 70 | continue; | 
|  | 71 | case 'f':  // Microseconds, from Python. | 
|  | 72 | FStream << llvm::format( | 
| Simon Dardis | 8bdbff3 | 2017-11-06 23:01:46 +0000 | [diff] [blame] | 73 | "%.6lu", (long)duration_cast<microseconds>(Fractional).count()); | 
| Sam McCall | fb4a9b7 | 2017-10-24 08:30:19 +0000 | [diff] [blame] | 74 | ++I; | 
|  | 75 | continue; | 
|  | 76 | case 'N':  // Nanoseconds, from date(1). | 
|  | 77 | FStream << llvm::format( | 
| Simon Dardis | 8bdbff3 | 2017-11-06 23:01:46 +0000 | [diff] [blame] | 78 | "%.6lu", (long)duration_cast<nanoseconds>(Fractional).count()); | 
| Sam McCall | fb4a9b7 | 2017-10-24 08:30:19 +0000 | [diff] [blame] | 79 | ++I; | 
|  | 80 | continue; | 
|  | 81 | case '%':  // Consume %%, so %%f parses as (%%)f not %(%f) | 
|  | 82 | FStream << "%%"; | 
|  | 83 | ++I; | 
|  | 84 | continue; | 
|  | 85 | } | 
|  | 86 | FStream << Style[I]; | 
|  | 87 | } | 
|  | 88 | FStream.flush(); | 
|  | 89 | char Buffer[256];  // Should be enough for anywhen. | 
|  | 90 | size_t Len = strftime(Buffer, sizeof(Buffer), Format.c_str(), <); | 
|  | 91 | OS << (Len ? Buffer : "BAD-DATE-FORMAT"); | 
|  | 92 | } | 
|  | 93 |  | 
| Pavel Labath | 59838f7 | 2016-10-20 12:05:50 +0000 | [diff] [blame] | 94 | } // namespace llvm |