Date and time library
C++ includes support for two types of time manipulation:
- The chrono library, a flexible collection of types that track time with varying degrees of precision (e.g., std::chrono::time_point ).
- C-style date and time library (e.g., std::time ).
Contents
[edit] Chrono library (since C++11)
The chrono
library defines several main types as well as utility functions and common typedefs:
[edit] Clocks
A clock consists of a starting point (or epoch) and a tick rate. For example, a clock may have an epoch of January 1, 1970 and tick every second. C++ defines several clock types:
<chrono>
std::chrono
[edit] Time point
A time point is a duration of time that has passed since the epoch of a specific clock.
<chrono>
std::chrono
(class template) [edit]
[edit] Duration
A duration consists of a span of time, defined as some number of ticks of some time unit. For example, "42 seconds" could be represented by a duration consisting of 42 ticks of a 1-second time unit.
<chrono>
std::chrono
[edit] Time of day (since C++20)
hh_mm_ss
splits a duration representing time elapsed since midnight into hours, minutes, seconds, and fractional seconds, as applicable. It is primarily a formatting tool.
<chrono>
std::chrono
[edit] Calendar (since C++20)
<chrono>
std::chrono
[edit] Time zone (since C++20)
<chrono>
std::chrono
[edit] Literals (since C++14)
<chrono>
std::literals::chrono_literals
[edit] Chrono I/O (since C++20)
<chrono>
std::chrono
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_chrono |
201510L |
(C++17) | Rounding functions for std::chrono::duration and std::chrono::time_point |
201611L |
(C++17) | constexpr for all the member functions of std::chrono::duration and std::chrono::time_point | |
201907L |
(C++20) | Calendars and Time zones | |
202306L |
(C++26) | Hashing support for std::chrono value classes
|
[edit] C-style date and time library
Also provided are the C-style date and time functions, such as std::time_t , std::difftime , and CLOCKS_PER_SEC .
[edit] Example
#include <chrono> #include <iostream> long Fibonacci(unsigned n) { return n < 2 ? n : Fibonacci(n - 1) + Fibonacci(n - 2); } int main() { // Measures and displays an execution time of a function call. const auto start{std::chrono::steady_clock::now ()}; const auto fb{Fibonacci(42)}; const auto finish{std::chrono::steady_clock::now ()}; const std::chrono::duration <double> elapsed_seconds{finish - start}; std::cout << "Fibonacci(42): " << fb << "\nElapsed time: "; // std::cout << elapsed_seconds.count() << "s\n"; // Before C++20 std::cout << elapsed_seconds << '\n'; // C++20's chrono::duration operator<< // Prints UTC and local time. const auto tp_utc{std::chrono::system_clock::now ()}; std::cout << "Current time 'UTC' is: " << tp_utc << "\n" "Current time 'Local' is: " << std::chrono::current_zone ()->to_local(tp_utc) << '\n'; }
Possible output:
Fibonacci(42): 267914296 Elapsed time: 0.728532s Current time 'UTC' is: 2025年02月10日 06:22:39.420666960 Current time 'Local' is: 2025年02月10日 09:22:39.420666960