Date and time utilities

From cppreference.com
< cpp
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
Date and time utilities
Time point
(C++11)
(C++20)
Duration
(C++11)
Clocks
(C++11)      
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time of day
(C++20)(C++20)
(C++20)(C++20)
(C++20)

Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
Time zones
(C++20)
(C++20)
(C++20)
chrono I/O
(C++20)
C-style date and time
 

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).

std::chrono library

The chrono library defines three main types as well as utility functions and common typedefs:

  • clocks,
  • time points,
  • durations.

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 <chrono>
Defined in namespace std::chrono
wall clock time from the system-wide realtime clock
(class)
monotonic clock that will never be adjusted
(class)
the clock with the shortest tick period available
(class)
determines if a type is a Clock
(class template) (variable template)
(C++20)
Clock for Coordinated Universal Time (UTC)
(class)
(C++20)
Clock for International Atomic Time (TAI)
(class)
(C++20)
Clock for GPS time
(class)
Clock used for file time
(typedef)
(C++20)
pseudo-clock representing local time
(class)

Time point

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
a point in time
(class template)
traits class defining how to convert time points of one clock to another
(class template)
convert time points of one clock to another
(function template)

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 header <chrono>
Defined in namespace std::chrono
(C++11)
a time interval
(class template)

Time of day

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)
translates between a 12h/24h format time of day
(function)

Calendar

Defined in header <chrono>
Defined in namespace std::chrono
(C++20)
tag class indicating the last day or weekday in a month
(class)
(C++20)
represents a day of a month
(class)
(C++20)
represents a month of a year
(class)
(C++20)
represents a year in the Gregorian calendar
(class)
(C++20)
represents a day of the week in the Gregorian calendar
(class)
represents the n-th weekday of a month
(class)
represents the last weekday of a month
(class)
(C++20)
represents a specific day of a specific month
(class)
represents the last day of a specific month
(class)
represents the n-th weekday of a specific month
(class)
represents the last weekday of a specific month
(class)
represents a specific month of a specific year
(class)
represents a specific year, month, and day
(class)
represents the last day of a specific year and month
(class)
represents the n-th weekday of a specific year and month
(class)
represents the last weekday of a specific year and month
(class)
(C++20)
conventional syntax for Gregorian calendar date creation
(function)

Time zone

Defined in header <chrono>
Defined in namespace std::chrono
(C++20)
describes a copy of the IANA time zone database
(class)
(C++20)
represents a linked list of tzdb
(class)
accesses and controls the global time zone database information
(function)
locates a time_zone based on its name
(function)
returns the current time_zone
(function)
(C++20)
represents a time zone
(class)
(C++20)
represents information about a time zone at a particular time point
(class)
represents information about a local time to UNIX time conversion
(class)
(C++20)
selects how an ambiguous local time should be resolved
(enum)
traits class for time zone pointers used by zoned_time
(class template)
represents a time zone and a time point
(class)
contains information about a leap second insertion
(class)
leap second insertion information
(class)
obtains leap second insertion information from a utc_time object
(function template)
represents an alternative name for a time zone
(class)
exception thrown to report that a local time is nonexistent
(class)
exception thrown to report that a local time is ambiguous
(class)

chrono I/O

Defined in header <chrono>
Defined in namespace std::chrono
(C++20)
parses a chrono object from a stream
(function template)

Notes

Feature-test macro Value Std Comment
__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

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.

Example

This example displays information about the execution time of a function call:

#include <chrono>
#include <iostream>
 
long fibonacci(unsigned n)
{
    if (n < 2)
        return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}
 
int main()
{
    const auto start = std::chrono::steady_clock::now();
    const auto fb = fibonacci(42);
    const auto end = std::chrono::steady_clock::now();
    const std::chrono::duration<double> elapsed_seconds = end - start;
 
    std::cout << "f(42) = " << fb << '\n' << "elapsed time: ";
//  std::cout << elapsed_seconds.count() << "s\n"; // Before C++20
    std::cout << elapsed_seconds << '\n'; // C++20: operator<< chrono::duration
}

Possible output:

f(42) = 267914296
elapsed time: 0.758056s