LLVM 22.0.0git
Chrono.h
Go to the documentation of this file.
1//===- llvm/Support/Chrono.h - 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#ifndef LLVM_SUPPORT_CHRONO_H
10#define LLVM_SUPPORT_CHRONO_H
11
14
15#include <chrono>
16#include <ctime>
17#include <ratio>
18
19namespace llvm {
20
21class raw_ostream;
22
23namespace sys {
24
25/// A time point on the system clock. This is provided for two reasons:
26/// - to insulate us against subtle differences in behavior to differences in
27/// system clock precision (which is implementation-defined and differs
28/// between platforms).
29/// - to shorten the type name
30/// The default precision is nanoseconds. If you need a specific precision
31/// specify it explicitly. If unsure, use the default. If you need a time point
32/// on a clock other than the system_clock, use std::chrono directly.
33template <typename D = std::chrono::nanoseconds>
34using TimePoint = std::chrono::time_point<std::chrono::system_clock, D>;
35
36// utc_clock and utc_time are only available since C++20. Add enough code to
37// support formatting date/time in UTC.
39
40template <typename D = std::chrono::nanoseconds>
41using UtcTime = std::chrono::time_point<UtcClock, D>;
42
43/// Convert a std::time_t to a UtcTime
45 using namespace std::chrono;
46 return UtcTime<seconds>(seconds(T));
47}
48
49/// Convert a TimePoint to std::time_t
50inline std::time_t toTimeT(TimePoint<> TP) {
51 using namespace std::chrono;
52 return system_clock::to_time_t(
53 time_point_cast<system_clock::time_point::duration>(TP));
54}
55
56/// Convert a UtcTime to std::time_t
57inline std::time_t toTimeT(UtcTime<> TP) {
58 using namespace std::chrono;
59 return system_clock::to_time_t(time_point<system_clock, seconds>(
60 duration_cast<seconds>(TP.time_since_epoch())));
61}
62
63/// Convert a std::time_t to a TimePoint
65toTimePoint(std::time_t T) {
66 using namespace std::chrono;
67 return time_point_cast<seconds>(system_clock::from_time_t(T));
68}
69
70/// Convert a std::time_t + nanoseconds to a TimePoint
71inline TimePoint<>
72toTimePoint(std::time_t T, uint32_t nsec) {
73 using namespace std::chrono;
74 return time_point_cast<nanoseconds>(system_clock::from_time_t(T))
75 + nanoseconds(nsec);
76}
77
78} // namespace sys
79
80LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP);
82
83/// Format provider for TimePoint<>
84///
85/// The options string is a strftime format string, with extensions:
86/// - %L is millis: 000-999
87/// - %f is micros: 000000-999999
88/// - %N is nanos: 000000000 - 999999999
89///
90/// If no options are given, the default format is "%Y-%m-%d %H:%M:%S.%N".
91template <>
92struct format_provider<sys::TimePoint<>> {
94 StringRef Style);
95};
96
97template <> struct format_provider<sys::UtcTime<std::chrono::seconds>> {
100};
101
102namespace detail {
103template <typename Period> struct unit { static const char value[]; };
104template <typename Period> const char unit<Period>::value[] = "";
105
106template <> struct unit<std::ratio<3600>> {
107 LLVM_ABI static const char value[];
108};
109template <> struct unit<std::ratio<60>> {
110 LLVM_ABI static const char value[];
111};
112template <> struct unit<std::ratio<1>> {
113 LLVM_ABI static const char value[];
114};
115template <> struct unit<std::milli> {
116 LLVM_ABI static const char value[];
117};
118template <> struct unit<std::micro> {
119 LLVM_ABI static const char value[];
120};
121template <> struct unit<std::nano> {
122 LLVM_ABI static const char value[];
123};
124} // namespace detail
125
126/// Implementation of format_provider<T> for duration types.
127///
128/// The options string of a duration type has the grammar:
129///
130/// duration_options ::= [unit][show_unit [number_options]]
131/// unit ::= `h`|`m`|`s`|`ms|`us`|`ns`
132/// show_unit ::= `+` | `-`
133/// number_options ::= options string for a integral or floating point type
134///
135/// Examples
136/// =================================
137/// | options | Input | Output |
138/// =================================
139/// | "" | 1s | 1 s |
140/// | "ms" | 1s | 1000 ms |
141/// | "ms-" | 1s | 1000 |
142/// | "ms-n" | 1s | 1,000 |
143/// | "" | 1.0s | 1.00 s |
144/// =================================
145///
146/// If the unit of the duration type is not one of the units specified above,
147/// it is still possible to format it, provided you explicitly request a
148/// display unit or you request that the unit is not displayed.
149
150template <typename Rep, typename Period>
151struct format_provider<std::chrono::duration<Rep, Period>> {
152private:
153 typedef std::chrono::duration<Rep, Period> Dur;
154 typedef std::conditional_t<std::chrono::treat_as_floating_point<Rep>::value,
155 double, intmax_t>
156 InternalRep;
157
158 template <typename AsPeriod> static InternalRep getAs(const Dur &D) {
159 using namespace std::chrono;
160 return duration_cast<duration<InternalRep, AsPeriod>>(D).count();
161 }
162
163 static std::pair<InternalRep, StringRef> consumeUnit(StringRef &Style,
164 const Dur &D) {
165 using namespace std::chrono;
166 if (Style.consume_front("ns"))
167 return {getAs<std::nano>(D), "ns"};
168 if (Style.consume_front("us"))
169 return {getAs<std::micro>(D), "us"};
170 if (Style.consume_front("ms"))
171 return {getAs<std::milli>(D), "ms"};
172 if (Style.consume_front("s"))
173 return {getAs<std::ratio<1>>(D), "s"};
174 if (Style.consume_front("m"))
175 return {getAs<std::ratio<60>>(D), "m"};
176 if (Style.consume_front("h"))
177 return {getAs<std::ratio<3600>>(D), "h"};
178 return {D.count(), detail::unit<Period>::value};
179 }
180
181 static bool consumeShowUnit(StringRef &Style) {
182 if (Style.empty())
183 return true;
184 if (Style.consume_front("-"))
185 return false;
186 if (Style.consume_front("+"))
187 return true;
188 assert(0 && "Unrecognised duration format");
189 return true;
190 }
191
192public:
193 static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style) {
194 InternalRep count;
195 StringRef unit;
196 std::tie(count, unit) = consumeUnit(Style, D);
197 bool show_unit = consumeShowUnit(Style);
198
200
201 if (show_unit) {
202 assert(!unit.empty());
203 Stream << " " << unit;
204 }
205 }
206};
207
208} // namespace llvm
209
210#endif // LLVM_SUPPORT_CHRONO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ABI
Definition: Compiler.h:213
raw_pwrite_stream & OS
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:34
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:65
std::chrono::time_point< UtcClock, D > UtcTime
Definition: Chrono.h:41
UtcTime< std::chrono::seconds > toUtcTime(std::time_t T)
Convert a std::time_t to a UtcTime.
Definition: Chrono.h:44
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1973
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
static const char value[]
Definition: Chrono.h:103
static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style)
Definition: Chrono.h:193
static LLVM_ABI void format(const sys::TimePoint<> &TP, llvm::raw_ostream &OS, StringRef Style)
static LLVM_ABI void format(const sys::UtcTime< std::chrono::seconds > &TP, llvm::raw_ostream &OS, StringRef Style)