|
|
|
C++ includes support for two types of time manipulation:
Contents |
Thechrono
library defines several main types as well as utility functions and common typedefs:
(since C++20) |
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 <chrono> | |
Defined in namespace std::chrono | |
(C++11) | wall clock time from the system-wide realtime clock (class)[edit] |
(C++11) | monotonic clock that will never be adjusted (class)[edit] |
(C++11) | the clock with the shortest tick period available (class)[edit] |
(C++20) | determines if a type is aClock (class template)(variable template)[edit] |
(C++20) | Clock for Coordinated Universal Time (UTC) (class)[edit] |
(C++20) | Clock for International Atomic Time (TAI) (class)[edit] |
(C++20) | Clock for GPS time (class)[edit] |
(C++20) | Clock used forfile time (typedef)[edit] |
(C++20) | pseudo-clock representing local time (class)[edit] |
A time point is a duration of time that has passed since the epoch of a specific clock.
Defined in header <chrono> | |
Defined in namespace std::chrono | |
(C++11) | a point in time (class template)[edit] |
(C++20) | traits class defining how to convert time points of one clock to another (class template)[edit] |
(C++20) | convert time points of one clock to another (function template)[edit] |
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 header <chrono> | |
Defined in namespace std::chrono | |
(C++11) | a time interval (class template)[edit] |
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.
Defined in header <chrono> | |
Defined in namespace std::chrono | |
(C++20) | represents a time of day (class template)[edit] |
(C++20) | translates between a 12h/24h format time of day (function)[edit] |
Defined in header <chrono> | |
Defined in namespace std::chrono | |
(C++20) | tag class indicating thelast day or weekday in a month (class)[edit] |
(C++20) | represents a day of a month (class)[edit] |
(C++20) | represents a month of a year (class)[edit] |
(C++20) | represents a year in the Gregorian calendar (class)[edit] |
(C++20) | represents a day of the week in the Gregorian calendar (class)[edit] |
(C++20) | represents the nthweekday of a month (class)[edit] |
(C++20) | represents the lastweekday of a month (class)[edit] |
(C++20) | represents a specificday of a specificmonth (class)[edit] |
(C++20) | represents the last day of a specificmonth (class)[edit] |
(C++20) | represents the nthweekday of a specificmonth (class)[edit] |
(C++20) | represents the lastweekday of a specificmonth (class)[edit] |
(C++20) | represents a specificmonth of a specificyear (class)[edit] |
(C++20) | represents a specificyear,month, andday (class)[edit] |
(C++20) | represents the last day of a specificyear andmonth (class)[edit] |
(C++20) | represents the nthweekday of a specificyear andmonth (class)[edit] |
(C++20) | represents the lastweekday of a specificyear andmonth (class)[edit] |
(C++20) | conventional syntax for Gregorian calendar date creation (function)[edit] |
Defined in header <chrono> | |
Defined in namespace std::chrono | |
(C++20) | describes a copy of theIANA time zone database (class)[edit] |
(C++20) | represents a linked list oftzdb (class)[edit] |
accesses and controls the global time zone database information (function)[edit] | |
(C++20) | locates atime_zone based on its name (function)[edit] |
(C++20) | returns the currenttime_zone (function)[edit] |
(C++20) | represents a time zone (class)[edit] |
(C++20) | represents information about a time zone at a particular time point (class)[edit] |
(C++20) | represents information about a local time to UNIX time conversion (class)[edit] |
(C++20) | selects how an ambiguous local time should be resolved (enum)[edit] |
(C++20) | traits class for time zone pointers used byzoned_time (class template)[edit] |
(C++20) | represents a time zone and a time point (class)[edit] |
(C++20) | contains information about a leap second insertion (class)[edit] |
(C++20) | leap second insertion information (class)[edit] |
(C++20) | obtains leap second insertion information from autc_time object(function template)[edit] |
(C++20) | represents an alternative name for a time zone (class)[edit] |
(C++20) | exception thrown to report that a local time is nonexistent (class)[edit] |
(C++20) | exception thrown to report that a local time is ambiguous (class)[edit] |
Defined in header <chrono> | |
Defined in inline namespace std::literals::chrono_literals | |
(C++20) | astd::chrono::year literal representing a particular year (function)[edit] |
(C++20) | astd::chrono::day literal representing a day of a month (function)[edit] |
(C++14) | astd::chrono::duration literal representing hours (function)[edit] |
(C++14) | astd::chrono::duration literal representing minutes (function)[edit] |
(C++14) | astd::chrono::duration literal representing seconds (function)[edit] |
(C++14) | astd::chrono::duration literal representing milliseconds (function)[edit] |
(C++14) | astd::chrono::duration literal representing microseconds (function)[edit] |
(C++14) | astd::chrono::duration literal representing nanoseconds (function)[edit] |
Defined in header <chrono> | |
Defined in namespace std::chrono | |
(C++20) | parses achrono object from a stream(function template)[edit] |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_chrono | 201510L | (C++17) | Rounding functions forstd::chrono::duration andstd::chrono::time_point |
201611L | (C++17) | constexpr for all the member functions ofstd::chrono::duration andstd::chrono::time_point | |
201907L | (C++20) | Calendars andTime zones | |
202306L | (C++26) | Hashing support forstd::chrono value classes |
Also provided are the C-style date and time functions, such asstd::time_t,std::difftime, andCLOCKS_PER_SEC.
#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.constauto start{std::chrono::steady_clock::now()};constauto fb{Fibonacci(42)};constauto finish{std::chrono::steady_clock::now()};conststd::chrono::duration<double> elapsed_seconds{finish- start}; std::cout<<"Fibonacci(42): "<< fb<<"\nElapsed time: ";// std::cout << elapsed_seconds.count() << "s\n"; // Before C++20std::cout<< elapsed_seconds<<'\n';// C++20's chrono::duration operator<< // Prints UTC and local time.constauto 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): 267914296Elapsed time: 0.728532sCurrent time 'UTC' is: 2025-02-10 06:22:39.420666960Current time 'Local' is: 2025-02-10 09:22:39.420666960