WPILibC++ 2024.3.2
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/// Convert a TimePoint to std::time_t
36inline std::time_t toTimeT(TimePoint<> TP) {
37 using namespace std::chrono;
38 return system_clock::to_time_t(
39 time_point_cast<system_clock::time_point::duration>(TP));
40}
41
42/// Convert a std::time_t to a TimePoint
43inline TimePoint<std::chrono::seconds>
44toTimePoint(std::time_t T) {
45 using namespace std::chrono;
46 return time_point_cast<seconds>(system_clock::from_time_t(T));
47}
48
49/// Convert a std::time_t + nanoseconds to a TimePoint
50inline TimePoint<>
51toTimePoint(std::time_t T, uint32_t nsec) {
52 using namespace std::chrono;
53 return time_point_cast<nanoseconds>(system_clock::from_time_t(T))
54 + nanoseconds(nsec);
55}
56
57} // namespace sys
58
60
61} // namespace wpi
62
63#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
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:44
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:36
Definition: ntcore_cpp.h:26
std::enable_if_t<!std::is_reference_v< OStream > &&std::is_base_of_v< raw_ostream, OStream >, OStream && > operator<<(OStream &&OS, const T &Value)
Call the appropriate insertion operator, given an rvalue reference to a raw_ostream object and return...
Definition: raw_ostream.h:398