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