| 1 | //===- Support/Chrono.cpp - Utilities for Timing Manipulation ---*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Support/Chrono.h" |
| 10 | #include "llvm/Config/llvm-config.h" |
| 11 | #include "llvm/Support/Compiler.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 | LLVM_ABI const char llvm::detail::unit<std::ratio<3600>>::value[] = "h" ; |
| 20 | LLVM_ABI const char llvm::detail::unit<std::ratio<60>>::value[] = "m" ; |
| 21 | LLVM_ABI const char llvm::detail::unit<std::ratio<1>>::value[] = "s" ; |
| 22 | LLVM_ABI const char llvm::detail::unit<std::milli>::value[] = "ms" ; |
| 23 | LLVM_ABI const char llvm::detail::unit<std::micro>::value[] = "us" ; |
| 24 | LLVM_ABI const char llvm::detail::unit<std::nano>::value[] = "ns" ; |
| 25 | |
| 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(timer: &OurTime, tp: &Storage); |
| 32 | assert(LT); |
| 33 | (void)LT; |
| 34 | #endif |
| 35 | #if defined(_WIN32) |
| 36 | int Error = ::localtime_s(&Storage, &OurTime); |
| 37 | assert(!Error); |
| 38 | (void)Error; |
| 39 | #endif |
| 40 | |
| 41 | return Storage; |
| 42 | } |
| 43 | |
| 44 | static inline struct tm getStructTMUtc(UtcTime<> TP) { |
| 45 | struct tm Storage; |
| 46 | std::time_t OurTime = toTimeT(TP); |
| 47 | |
| 48 | #if defined(LLVM_ON_UNIX) |
| 49 | struct tm *LT = ::gmtime_r(timer: &OurTime, tp: &Storage); |
| 50 | assert(LT); |
| 51 | (void)LT; |
| 52 | #endif |
| 53 | #if defined(_WIN32) |
| 54 | int Error = ::gmtime_s(&Storage, &OurTime); |
| 55 | assert(!Error); |
| 56 | (void)Error; |
| 57 | #endif |
| 58 | |
| 59 | return Storage; |
| 60 | } |
| 61 | |
| 62 | raw_ostream &operator<<(raw_ostream &OS, TimePoint<> TP) { |
| 63 | struct tm LT = getStructTM(TP); |
| 64 | char Buffer[sizeof("YYYY-MM-DD HH:MM:SS" )]; |
| 65 | strftime(s: Buffer, maxsize: sizeof(Buffer), format: "%Y-%m-%d %H:%M:%S" , tp: <); |
| 66 | return OS << Buffer << '.' |
| 67 | << format(Fmt: "%.9lu" , |
| 68 | Vals: long((TP.time_since_epoch() % std::chrono::seconds(1)) |
| 69 | .count())); |
| 70 | } |
| 71 | |
| 72 | template <class T> |
| 73 | static void format(const T &Fractional, struct tm <, raw_ostream &OS, |
| 74 | StringRef Style) { |
| 75 | using namespace std::chrono; |
| 76 | // Handle extensions first. strftime mangles unknown %x on some platforms. |
| 77 | if (Style.empty()) Style = "%Y-%m-%d %H:%M:%S.%N" ; |
| 78 | std::string Format; |
| 79 | raw_string_ostream FStream(Format); |
| 80 | for (unsigned I = 0; I < Style.size(); ++I) { |
| 81 | if (Style[I] == '%' && Style.size() > I + 1) switch (Style[I + 1]) { |
| 82 | case 'L': // Milliseconds, from Ruby. |
| 83 | FStream << llvm::format( |
| 84 | Fmt: "%.3lu" , Vals: (long)duration_cast<milliseconds>(Fractional).count()); |
| 85 | ++I; |
| 86 | continue; |
| 87 | case 'f': // Microseconds, from Python. |
| 88 | FStream << llvm::format( |
| 89 | Fmt: "%.6lu" , Vals: (long)duration_cast<microseconds>(Fractional).count()); |
| 90 | ++I; |
| 91 | continue; |
| 92 | case 'N': // Nanoseconds, from date(1). |
| 93 | FStream << llvm::format( |
| 94 | Fmt: "%.9lu" , Vals: (long)duration_cast<nanoseconds>(Fractional).count()); |
| 95 | ++I; |
| 96 | continue; |
| 97 | case '%': // Consume %%, so %%f parses as (%%)f not %(%f) |
| 98 | FStream << "%%" ; |
| 99 | ++I; |
| 100 | continue; |
| 101 | } |
| 102 | FStream << Style[I]; |
| 103 | } |
| 104 | FStream.flush(); |
| 105 | char Buffer[256]; // Should be enough for anywhen. |
| 106 | size_t Len = strftime(s: Buffer, maxsize: sizeof(Buffer), format: Format.c_str(), tp: <); |
| 107 | OS << (Len ? Buffer : "BAD-DATE-FORMAT" ); |
| 108 | } |
| 109 | |
| 110 | void format_provider<UtcTime<std::chrono::seconds>>::format( |
| 111 | const UtcTime<std::chrono::seconds> &T, raw_ostream &OS, StringRef Style) { |
| 112 | using namespace std::chrono; |
| 113 | UtcTime<seconds> Truncated = |
| 114 | UtcTime<seconds>(duration_cast<seconds>(d: T.time_since_epoch())); |
| 115 | auto Fractional = T - Truncated; |
| 116 | struct tm LT = getStructTMUtc(TP: Truncated); |
| 117 | llvm::format(Fractional, LT, OS, Style); |
| 118 | } |
| 119 | |
| 120 | void format_provider<TimePoint<>>::format(const TimePoint<> &T, raw_ostream &OS, |
| 121 | StringRef Style) { |
| 122 | using namespace std::chrono; |
| 123 | TimePoint<seconds> Truncated = time_point_cast<seconds>(t: T); |
| 124 | auto Fractional = T - Truncated; |
| 125 | struct tm LT = getStructTM(TP: Truncated); |
| 126 | llvm::format(Fractional, LT, OS, Style); |
| 127 | } |
| 128 | |
| 129 | } // namespace llvm |
| 130 | |