Chrono.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "llvm/Support/Chrono.h"
  9. #include "llvm/Config/llvm-config.h"
  10. #include "llvm/Support/Format.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. namespace llvm {
  13. using namespace sys;
  14. const char llvm::detail::unit<std::ratio<3600>>::value[] = "h";
  15. const char llvm::detail::unit<std::ratio<60>>::value[] = "m";
  16. const char llvm::detail::unit<std::ratio<1>>::value[] = "s";
  17. const char llvm::detail::unit<std::milli>::value[] = "ms";
  18. const char llvm::detail::unit<std::micro>::value[] = "us";
  19. const char llvm::detail::unit<std::nano>::value[] = "ns";
  20. static inline struct tm getStructTM(TimePoint<> TP) {
  21. struct tm Storage;
  22. std::time_t OurTime = toTimeT(TP);
  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(_WIN32)
  29. int Error = ::localtime_s(&Storage, &OurTime);
  30. assert(!Error);
  31. (void)Error;
  32. #endif
  33. return Storage;
  34. }
  35. raw_ostream &operator<<(raw_ostream &OS, TimePoint<> TP) {
  36. struct tm LT = getStructTM(TP);
  37. char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
  38. strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", &LT);
  39. return OS << Buffer << '.'
  40. << format("%.9lu",
  41. long((TP.time_since_epoch() % std::chrono::seconds(1))
  42. .count()));
  43. }
  44. void format_provider<TimePoint<>>::format(const TimePoint<> &T, raw_ostream &OS,
  45. StringRef Style) {
  46. using namespace std::chrono;
  47. TimePoint<seconds> Truncated = time_point_cast<seconds>(T);
  48. auto Fractional = T - Truncated;
  49. struct tm LT = getStructTM(Truncated);
  50. // Handle extensions first. strftime mangles unknown %x on some platforms.
  51. if (Style.empty()) Style = "%Y-%m-%d %H:%M:%S.%N";
  52. std::string Format;
  53. raw_string_ostream FStream(Format);
  54. for (unsigned I = 0; I < Style.size(); ++I) {
  55. if (Style[I] == '%' && Style.size() > I + 1) switch (Style[I + 1]) {
  56. case 'L': // Milliseconds, from Ruby.
  57. FStream << llvm::format(
  58. "%.3lu", (long)duration_cast<milliseconds>(Fractional).count());
  59. ++I;
  60. continue;
  61. case 'f': // Microseconds, from Python.
  62. FStream << llvm::format(
  63. "%.6lu", (long)duration_cast<microseconds>(Fractional).count());
  64. ++I;
  65. continue;
  66. case 'N': // Nanoseconds, from date(1).
  67. FStream << llvm::format(
  68. "%.6lu", (long)duration_cast<nanoseconds>(Fractional).count());
  69. ++I;
  70. continue;
  71. case '%': // Consume %%, so %%f parses as (%%)f not %(%f)
  72. FStream << "%%";
  73. ++I;
  74. continue;
  75. }
  76. FStream << Style[I];
  77. }
  78. FStream.flush();
  79. char Buffer[256]; // Should be enough for anywhen.
  80. size_t Len = strftime(Buffer, sizeof(Buffer), Format.c_str(), &LT);
  81. OS << (Len ? Buffer : "BAD-DATE-FORMAT");
  82. }
  83. } // namespace llvm