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