Time library
A guide to Clause 30: <chrono> builds time units into the type system with three types, duration, time_point and clock, and C++20 adds calendars, time zones and formatting on top; the synopsis specifies the minimum bit widths of the duration aliases and how the literals are brought in; C++26 adds std::hash for chrono types.
Key points
- The skeleton of
<chrono>is three types: a duration is a tick count times a tick period, a time_point is a duration plus a clock, and a clock provides an epoch andnow(). Units are written into the types, so using the wrong unit fails to compile. system_clockis specified to measure Unix Time (every day is exactly 86400 seconds), so C++20 calendar types and time points convert by pure arithmetic.- The aliases from
nanosecondstohoursare all guaranteed to cover at least about ±292 years;days,monthsandyearscover the whole range ofyear, [−32767, 32767]. using namespace std::chrono;also brings in literals like1sand500ms.- Since C++26,
duration,time_pointand the calendar types havestd::hashspecializations and can be used directly as keys in unordered containers.
Why put units in the type#
#include <chrono>
using namespace std::chrono;
milliseconds timeout = 2s; // OK: seconds to milliseconds just multiplies by 1000
// seconds s = 1500ms; // error: needs division, which truncates
seconds s = duration_cast<seconds>(1500ms); // explicit truncation: 1s
static_assert(1s + 1ms == 1001ms); // different units add directly
int main() {}In C interfaces, whether sleep(5) is 5 seconds or 5 milliseconds, and whether a time_t and a clock_t can be subtracted, depend entirely on documentation and convention. <chrono> encodes this information into types: milliseconds{1500} stores only the integer 1500, and the millisecond unit lives in the type. The multiplication and division factors needed to change units are computed by the compiler at compile time; conversions that may lose precision must be written explicitly.
The skeleton: three core types#
C++11's <chrono> (N2661) had only three things, and they remain the skeleton of the whole clause:
clock ──now()──▶ time_point<Clock, Duration> ──time_since_epoch()──▶ duration<Rep, Period>- duration: an arithmetic value
count()plus a compile-time rational periodPeriod([ratio]). - time_point: a duration plus a clock type. The clock type serves here only as a tag saying which instant the duration is measured from; that instant is the clock's epoch (epoch (of a clock)). time_points of different clocks are different types and can be neither compared nor subtracted.
- clock: a type providing
now(), meeting the Cpp17Clock requirements requirements ([time.clock.req]).
C++20's P0355R7 added three parts to the skeleton, largely adopting Howard Hinnant's date/tz library:
- Calendars ([time.cal]): dates stored as year/month/day fields, such as
year_month_day, convertible to and fromsys_days(a time_point in units of days).system_clockis specified as Unix time, with every day exactly 86400 seconds, so conversion between dates and time points is pure arithmetic with no table lookups. - Time zones ([time.zone]): the IANA time zone database, conversion between
local_timeandsys_time, leap seconds andutc_clock. - Text I/O ([time.format], [time.parse]):
%Y-%m-%d-style format specs instd::format(P1361R2) and the correspondingparse.
Contents of the clause ([time.general])#
¶1 This clause describes the chrono library and the C time functions; Table 130 lists the subclauses:
| Subclause | Contents | Notes on this site |
|---|---|---|
| [time.clock.req] | the Cpp17Clock and Cpp17TrivialClock requirements | [time.clock.req] |
| [time.traits] | treat_as_floating_point, duration_values, common_type specializations, is_clock |
[time.traits] |
| [time.duration] | the class template duration |
[time.duration] |
| [time.point] | the class template time_point |
[time.point] |
| [time.clock] | clocks such as system_clock, steady_clock, utc_clock and file_clock, and clock_cast |
[time.clock] |
| [time.cal] | the civil calendar: types such as year, month and year_month_day, and the / syntax |
[time.cal] |
| [time.hms], [time.12] | hh_mm_ss; 12/24-hour functions such as is_am and make12 |
[time.hms] |
| [time.zone] | the time zone database, time_zone, zoned_time, leap seconds |
[time.zone] |
| [time.format], [time.parse] | formatting and parsing | [time.format], [time.parse] |
| [time.hash] | hash support C++26 | this page |
| [ctime.syn] | the C time functions, header <ctime> |
[ctime.syn] |
Except for [ctime.syn], these facilities are all in <chrono>. Some related material is not in this clause: the rules for the _for and _until timeout functions of the thread library are in [thread.req.timing]; filesystem::file_time_type is just time_point<file_clock> ([fs.filesystem.syn]); and time_get and time_put in <locale> are a separate, older interface unrelated to <chrono> ([category.time]).
What the synopsis specifies ([time.syn])#
Most of the <chrono> synopsis just collects declarations from the subclauses, but the following points are specified only in the synopsis.
Minimum bit widths of the duration aliases#
using nanoseconds = duration<signed integer type of at least 64 bits, nano>;
using microseconds = duration<signed integer type of at least 55 bits, micro>;
using milliseconds = duration<signed integer type of at least 45 bits, milli>;
using seconds = duration<signed integer type of at least 35 bits>;
using minutes = duration<signed integer type of at least 29 bits, ratio< 60>>;
using hours = duration<signed integer type of at least 23 bits, ratio<3600>>;
using days = duration<signed integer type of at least 25 bits, ...>;
using weeks = duration<signed integer type of at least 22 bits, ...>;
using years = duration<signed integer type of at least 17 bits, ...>;
using months = duration<signed integer type of at least 20 bits, ...>;These widths are calculated:
- From
nanosecondstohours, each alias can represent at least about ±292 years (2⁶³ ns ≈ 292 years, 2⁵⁴ µs ≈ 571 years, 2³⁴ s ≈ 544 years). That is, every small unit covers at least a few centuries on either side of 1970. - The widths of
days,monthsandyearsare chosen to cover the range ofyear, [−32767, 32767] ([time.cal]): 2²⁴ days is about 45,900 years, 2¹⁹ months about 43,690 years, and 2¹⁶ years is 65,536 years. - The period of
yearsis 146097/400 days, the average Gregorian year length (400 years have 146097 days);monthsis 1/12 of it. Soyears{1}is neither 365 nor 366 days but 365.2425 days, that is, 31,556,952 seconds. Adding it to asys_daysgives a result with hours, minutes and seconds; adding a calendar year should be done on ayear_month_day([time.cal]).
nanoseconds guarantees only ±292 years, and this is the most common source of overflow in chrono; see [time.duration].
Literals and constants#
- The duration literals
h,min,s,ms,usandns, and the calendar literalsdandy, are all declared instd::inline literals::inline chrono_literals, so bothusing namespace std::literals;andusing namespace std::chrono_literals;bring them in. - The synopsis also has the line
namespace std::chrono { using namespace literals::chrono_literals; }, sousing namespace std::chrono;brings the literals in too. Most examples need only that one line. - The calendar constants
last,SundaythroughSaturday, andJanuarythroughDecemberare allinline constexprvariables.
The suffix s does not conflict with std::string's ""s: the former applies only to number literals, the latter only to string literals ([time.duration.literals]/8).
Hash support ([time.hash]) C++26#
#include <cassert>
#include <chrono>
#include <unordered_set>
using namespace std::chrono;
int main() {
std::unordered_set<seconds> s{1s, 2s, 1s};
assert(s.size() == 2);
// February 30, 2024 is !ok(), but hashes all the same
std::unordered_set<year_month_day> d{
2024y/February/29, 2024y/February/30};
assert(d.size() == 2);
}C++26's P2592R3 added these specializations. Before that, putting seconds or year_month_day in an unordered_set required writing your own hasher.
- ¶1
hash<duration<Rep, Period>>is enabled if and only ifhash<Rep>is enabled ([unord.hash]). The member function is not guaranteednoexcept, becauseRepcan be a class type that throws. - ¶2
hash<time_point<Clock, Duration>>is enabled if and only ifhash<Duration>is enabled. - ¶3 The specializations for the 15 calendar types are always enabled. Note 1 points out that they meet the Cpp17Hash requirements even when called on objects for which
ok()isfalse. Calendar types are allowed to hold invalid values likeFebruary/30, and hashing must not break because of it. - ¶4
hash<zoned_time<Duration, TimeZonePtr>>is enabled if and only if bothhash<Duration>andhash<TimeZonePtr>are enabled. - ¶5
hash<leap_second>is always enabled.
duration's == can compare across units (1s == 1000ms), but hash<seconds> and hash<milliseconds> are two different specializations, and the standard does not require them to give the same hash value for equal values. An unordered container's key type is fixed, so this does not cause errors; just don't expect hash values to agree across types.
The state of libc++#
The examples on this site are verified with Homebrew Clang 23 + libc++. In our tests:
| Part | Status |
|---|---|
duration, time_point, system_clock, steady_clock, file_clock |
available |
calendar types, hh_mm_ss, 12/24-hour functions |
available |
std::format and operator<< for chrono |
available (except the time-zone-related parts) |
hash specializations |
available |
utc_clock, tai_clock, gps_clock, tzdb, time_zone, zoned_time, leap_second |
unavailable: the __config_site of our build defines _LIBCPP_HAS_TIME_ZONE_DATABASE as 0, and -fexperimental-library does not change that |
clock_cast, parse |
not implemented |
So the examples in [time.zone] and [time.parse] cannot run in our setup, as those pages note separately. They can be tried on Compiler Explorer with a recent GCC (libstdc++) or MSVC.
Evolution#
| Version | Change |
|---|---|
| C++11 | duration, time_point, system_clock, steady_clock, high_resolution_clock (N2661) |
| C++14 | duration literals; more functions become constexpr |
| C++17 | floor, ceil, round, abs |
| C++20 | calendars, time zones, new clocks such as utc_clock, clock_cast, hh_mm_ss, the aliases days through years, is_clock (P0355R7); integration with std::format (P1361R2); system_clock specified as Unix Time |
| C++23 | chrono formatting defaults to the "C" locale, using localized formats only with the L option (as a defect fix against C++20) |
| C++26 | hash support (P2592R3) |
| new wording after C++23 | <ctime> aligned with C23: timegm, gmtime_r, localtime_r and timespec_getres added; asctime and ctime moved to Annex D ([depr.ctime]) |
Contents
- 30.1General[time.general]
- 30.2Header <chrono> synopsis[time.syn]
- 30.3Cpp17Clock requirements[time.clock.req]done
- 30.4Time-related traits[time.traits]done
- 30.5Class template duration[time.duration]done
- 30.5.1General[time.duration.general]
- 30.5.2Constructors[time.duration.cons]
- 30.5.3Observer[time.duration.observer]
- 30.5.4Arithmetic[time.duration.arithmetic]
- 30.5.5Special values[time.duration.special]
- 30.5.6Non-member arithmetic[time.duration.nonmember]
- 30.5.7Comparisons[time.duration.comparisons]
- 30.5.8Conversions[time.duration.cast]
- 30.5.9Suffixes for duration literals[time.duration.literals]
- 30.5.10Algorithms[time.duration.alg]
- 30.5.11I/O[time.duration.io]
- 30.6Class template time_point[time.point]done
- 30.6.1General[time.point.general]
- 30.6.2Constructors[time.point.cons]
- 30.6.3Observer[time.point.observer]
- 30.6.4Arithmetic[time.point.arithmetic]
- 30.6.5Special values[time.point.special]
- 30.6.6Non-member arithmetic[time.point.nonmember]
- 30.6.7Comparisons[time.point.comparisons]
- 30.6.8Conversions[time.point.cast]
- 30.7Clocks[time.clock]done
- 30.7.1General[time.clock.general]
- 30.7.2Class system_clock[time.clock.system]
- 30.7.3Class utc_clock[time.clock.utc]
- 30.7.4Class tai_clock[time.clock.tai]
- 30.7.5Class gps_clock[time.clock.gps]
- 30.7.6Type file_clock[time.clock.file]
- 30.7.7Class steady_clock[time.clock.steady]
- 30.7.8Class high_resolution_clock[time.clock.hires]
- 30.7.9Local time[time.clock.local]
- 30.7.10time_point conversions[time.clock.cast]
- 30.7.10.1Class template clock_time_conversion[time.clock.conv]
- 30.7.10.2Identity conversions[time.clock.cast.id]
- 30.7.10.3Conversions between system_clock and utc_clock[time.clock.cast.sys.utc]
- 30.7.10.4Conversions between system_clock and other clocks[time.clock.cast.sys]
- 30.7.10.5Conversions between utc_clock and other clocks[time.clock.cast.utc]
- 30.7.10.6Function template clock_cast[time.clock.cast.fn]
- 30.8The civil calendar[time.cal]done
- 30.8.1General[time.cal.general]
- 30.8.2Class last_spec[time.cal.last]
- 30.8.3Class day[time.cal.day]
- 30.8.4Class month[time.cal.month]
- 30.8.5Class year[time.cal.year]
- 30.8.6Class weekday[time.cal.wd]
- 30.8.7Class weekday_indexed[time.cal.wdidx]
- 30.8.8Class weekday_last[time.cal.wdlast]
- 30.8.9Class month_day[time.cal.md]
- 30.8.10Class month_day_last[time.cal.mdlast]
- 30.8.11Class month_weekday[time.cal.mwd]
- 30.8.12Class month_weekday_last[time.cal.mwdlast]
- 30.8.13Class year_month[time.cal.ym]
- 30.8.14Class year_month_day[time.cal.ymd]
- 30.8.15Class year_month_day_last[time.cal.ymdlast]
- 30.8.16Class year_month_weekday[time.cal.ymwd]
- 30.8.17Class year_month_weekday_last[time.cal.ymwdlast]
- 30.8.18Conventional syntax operators[time.cal.operators]
- 30.9Class template hh_mm_ss[time.hms]done
- 30.1012/24 hours functions[time.12]
- 30.11Time zones[time.zone]done
- 30.11.1General[time.zone.general]
- 30.11.2Time zone database[time.zone.db]
- 30.11.3Exception classes[time.zone.exception]
- 30.11.4Information classes[time.zone.info]
- 30.11.5Class time_zone[time.zone.timezone]
- 30.11.6Class template zoned_traits[time.zone.zonedtraits]
- 30.11.7Class template zoned_time[time.zone.zonedtime]
- 30.11.8Class leap_second[time.zone.leap]
- 30.11.9Class time_zone_link[time.zone.link]
- 30.12Formatting[time.format]done
- 30.13Parsing[time.parse]done
- 30.14Hash support[time.hash]
- 30.15Header <ctime> synopsis[ctime.syn]done