WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
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 WPIUTIL_WPI_CHRONO_H
10#define WPIUTIL_WPI_CHRONO_H
11
12#include "wpi/Compiler.h"
13
14#include <chrono>
15#include <ctime>
16#include <ratio>
17
18namespace wpi {
19
20class raw_ostream;
21
22namespace sys {
23
24/// A time point on the system clock. This is provided for two reasons:
25/// - to insulate us against subtle differences in behavior to differences in
26/// system clock precision (which is implementation-defined and differs
27/// between platforms).
28/// - to shorten the type name
29/// The default precision is nanoseconds. If you need a specific precision
30/// specify it explicitly. If unsure, use the default. If you need a time point
31/// on a clock other than the system_clock, use std::chrono directly.
32template <typename D = std::chrono::nanoseconds>
33using TimePoint = std::chrono::time_point<std::chrono::system_clock, D>;
34
35// utc_clock and utc_time are only available since C++20. Add enough code to
36// support formatting date/time in UTC.
37class UtcClock : public std::chrono::system_clock {};
38
39template <typename D = std::chrono::nanoseconds>
40using UtcTime = std::chrono::time_point<UtcClock, D>;
41
42/// Convert a std::time_t to a UtcTime
44 using namespace std::chrono;
45 return UtcTime<seconds>(seconds(T));
46}
47
48/// Convert a TimePoint to std::time_t
49inline std::time_t toTimeT(TimePoint<> TP) {
50 using namespace std::chrono;
51 return system_clock::to_time_t(
52 time_point_cast<system_clock::time_point::duration>(TP));
53}
54
55/// Convert a UtcTime to std::time_t
56inline std::time_t toTimeT(UtcTime<> TP) {
57 using namespace std::chrono;
58 return system_clock::to_time_t(time_point<system_clock, seconds>(
59 duration_cast<seconds>(TP.time_since_epoch())));
60}
61
62/// Convert a std::time_t to a TimePoint
64toTimePoint(std::time_t T) {
65 using namespace std::chrono;
66 return time_point_cast<seconds>(system_clock::from_time_t(T));
67}
68
69/// Convert a std::time_t + nanoseconds to a TimePoint
70inline TimePoint<>
71toTimePoint(std::time_t T, uint32_t nsec) {
72 using namespace std::chrono;
73 return time_point_cast<nanoseconds>(system_clock::from_time_t(T))
74 + nanoseconds(nsec);
75}
76
77} // namespace sys
78
81
82} // namespace wpi
83
84#endif // WPIUTIL_WPI_CHRONO_H
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:43
Definition Chrono.h:37
UtcTime< std::chrono::seconds > toUtcTime(std::time_t T)
Convert a std::time_t to a UtcTime.
Definition Chrono.h:43
std::chrono::time_point< UtcClock, D > UtcTime
Definition Chrono.h:40
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition Chrono.h:64
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition Chrono.h:49
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition Chrono.h:33
Foonathan namespace.
Definition ntcore_cpp.h:26
raw_ostream & operator<<(raw_ostream &OS, sys::TimePoint<> TP)