Date and time library - cppreference.com (original) (raw)
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:
Defined in header | |
---|---|
Defined in namespace std::chrono | |
system_clock(C++11) | wall clock time from the system-wide realtime clock (class) [edit] |
steady_clock(C++11) | monotonic clock that will never be adjusted (class) [edit] |
high_resolution_clock(C++11) | the clock with the shortest tick period available (class) [edit] |
is_clockis_clock_v(C++20) | determines if a type is a Clock(class template) (variable template)[edit] |
utc_clock(C++20) | Clock for Coordinated Universal Time (UTC) (class) [edit] |
tai_clock(C++20) | Clock for International Atomic Time (TAI) (class) [edit] |
gps_clock(C++20) | Clock for GPS time (class) [edit] |
file_clock(C++20) | Clock used for file time (typedef) [edit] |
local_t(C++20) | pseudo-clock representing local time (class) [edit] |
[edit] Time point
A time point is a duration of time that has passed since the epoch of a specific clock.
[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.
Defined in namespace 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.
[edit] Calendar (since C++20)
Defined in header | |
---|---|
Defined in namespace std::chrono | |
last_spec(C++20) | tag class indicating the last day or weekday in a month (class) [edit] |
day(C++20) | represents a day of a month (class) [edit] |
month(C++20) | represents a month of a year (class) [edit] |
year(C++20) | represents a year in the Gregorian calendar (class) [edit] |
weekday(C++20) | represents a day of the week in the Gregorian calendar (class) [edit] |
weekday_indexed(C++20) | represents the nth weekday of a month (class) [edit] |
weekday_last(C++20) | represents the last weekday of a month (class) [edit] |
month_day(C++20) | represents a specific day of a specific month (class) [edit] |
month_day_last(C++20) | represents the last day of a specific month (class) [edit] |
month_weekday(C++20) | represents the nth weekday of a specific month (class) [edit] |
month_weekday_last(C++20) | represents the last weekday of a specific month (class) [edit] |
year_month(C++20) | represents a specific month of a specific year (class) [edit] |
year_month_day(C++20) | represents a specific year, month, and day (class) [edit] |
year_month_day_last(C++20) | represents the last day of a specific year and month (class) [edit] |
year_month_weekday(C++20) | represents the nth weekday of a specific year and month (class) [edit] |
year_month_weekday_last(C++20) | represents the last weekday of a specific year and month (class) [edit] |
operator/(C++20) | conventional syntax for Gregorian calendar date creation (function) [edit] |
[edit] Time zone (since C++20)
Defined in header | |
---|---|
Defined in namespace std::chrono | |
tzdb(C++20) | describes a copy of the IANA time zone database (class) [edit] |
tzdb_list(C++20) | represents a linked list of tzdb (class) [edit] |
get_tzdbget_tzdb_listreload_tzdbremote_version(C++20) | accesses and controls the global time zone database information (function) [edit] |
locate_zone(C++20) | locates a time_zone based on its name (function) [edit] |
current_zone(C++20) | returns the current time_zone (function) [edit] |
time_zone(C++20) | represents a time zone (class) [edit] |
sys_info(C++20) | represents information about a time zone at a particular time point (class) [edit] |
local_info(C++20) | represents information about a local time to UNIX time conversion (class) [edit] |
choose(C++20) | selects how an ambiguous local time should be resolved (enum) [edit] |
zoned_traits(C++20) | traits class for time zone pointers used by zoned_time (class template) [edit] |
zoned_time(C++20) | represents a time zone and a time point (class) [edit] |
leap_second(C++20) | contains information about a leap second insertion (class) [edit] |
leap_second_info(C++20) | leap second insertion information (class) [edit] |
get_leap_second_info(C++20) | obtains leap second insertion information from a utc_time object (function template) [edit] |
time_zone_link(C++20) | represents an alternative name for a time zone (class) [edit] |
nonexistent_local_time(C++20) | exception thrown to report that a local time is nonexistent (class) [edit] |
ambiguous_local_time(C++20) | exception thrown to report that a local time is ambiguous (class) [edit] |
[edit] Literals (since C++14)
Defined in header | |
---|---|
Defined in inline namespace std::literals::chrono_literals | |
[ operator""y](chrono/operator%2522%2522y.html "cpp/chrono/operator""y")(C++20) | a std::chrono::year literal representing a particular year (function) [edit] |
[ operator""d](chrono/operator%2522%2522d.html "cpp/chrono/operator""d")(C++20) | a std::chrono::day literal representing a day of a month (function) [edit] |
[ operator""h](chrono/operator%2522%2522h.html "cpp/chrono/operator""h")(C++14) | a std::chrono::duration literal representing hours (function) [edit] |
[ operator""min](chrono/operator%2522%2522min.html "cpp/chrono/operator""min")(C++14) | a std::chrono::duration literal representing minutes (function) [edit] |
[ operator""s](chrono/operator%2522%2522s.html "cpp/chrono/operator""s")(C++14) | a std::chrono::duration literal representing seconds (function) [edit] |
[ operator""ms](chrono/operator%2522%2522ms.html "cpp/chrono/operator""ms")(C++14) | a std::chrono::duration literal representing milliseconds (function) [edit] |
[ operator""us](chrono/operator%2522%2522us.html "cpp/chrono/operator""us")(C++14) | a std::chrono::duration literal representing microseconds (function) [edit] |
[ operator""ns](chrono/operator%2522%2522ns.html "cpp/chrono/operator""ns")(C++14) | a std::chrono::duration literal representing nanoseconds (function) [edit] |
[edit] Chrono I/O (since C++20)
Defined in namespace 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 #include 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 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