[time.duration] # 30 Time library [[time]](./#time) ## 30.5 Class template duration [time.duration] ### [30.5.1](#general) General [[time.duration.general]](time.duration.general) [1](#general-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1238) A duration type measures time between two points in time (time_points)[.](#general-1.sentence-1) A duration has a representation which holds a count of ticks and a tick period[.](#general-1.sentence-2) The tick period is the amount of time which occurs from one tick to the next, in units of seconds[.](#general-1.sentence-3) It is expressed as a rational constant using the template ratio[.](#general-1.sentence-4) [🔗](#lib:duration) namespace std::chrono {template>class duration {public:using rep = Rep; using period = typename Period::type; private: rep rep_; // *exposition only*public:// [[time.duration.cons]](#cons "30.5.2 Constructors"), construct/copy/destroyconstexpr duration() = default; templateconstexpr explicit duration(const Rep2& r); templateconstexpr duration(const duration& d); ~duration() = default; duration(const duration&) = default; duration& operator=(const duration&) = default; // [[time.duration.observer]](#observer "30.5.3 Observer"), observerconstexpr rep count() const; // [[time.duration.arithmetic]](#arithmetic "30.5.4 Arithmetic"), arithmeticconstexpr common_type_t operator+() const; constexpr common_type_t operator-() const; constexpr duration& operator++(); constexpr duration operator++(int); constexpr duration& operator--(); constexpr duration operator--(int); constexpr duration& operator+=(const duration& d); constexpr duration& operator-=(const duration& d); constexpr duration& operator*=(const rep& rhs); constexpr duration& operator/=(const rep& rhs); constexpr duration& operator%=(const rep& rhs); constexpr duration& operator%=(const duration& rhs); // [[time.duration.special]](#special "30.5.5 Special values"), special valuesstatic constexpr duration zero() noexcept; static constexpr duration min() noexcept; static constexpr duration max() noexcept; };} [2](#general-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1294) Rep shall be an arithmetic type or a class emulating an arithmetic type[.](#general-2.sentence-1) If duration is instantiated with a duration type as the argument for the template parameter Rep, the program is ill-formed[.](#general-2.sentence-2) [3](#general-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1299) If Period is not a specialization of ratio, the program is ill-formed[.](#general-3.sentence-1) If Period​::​num is not positive, the program is ill-formed[.](#general-3.sentence-2) [4](#general-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1303) Members of duration do not throw exceptions other than those thrown by the indicated operations on their representations[.](#general-4.sentence-1) [5](#general-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1307) The defaulted copy constructor of duration shall be a constexpr function if and only if the required initialization of the member rep_ for copy and move, respectively, would be constexpr-suitable ([[dcl.constexpr]](dcl.constexpr "9.2.6 The constexpr and consteval specifiers"))[.](#general-5.sentence-1) [6](#general-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1313) [*Example [1](#general-example-1)*: duration> d0; // holds a count of minutes using a long duration d1; // holds a count of milliseconds using a long long duration> d2; // holds a count with a tick period of 130 of a second// (30 Hz) using a double — *end example*] ### [30.5.2](#cons) Constructors [[time.duration.cons]](time.duration.cons) [🔗](#lib:duration,constructor) `template constexpr explicit duration(const Rep2& r); ` [1](#cons-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1332) *Constraints*: is_convertible_v is true and - [(1.1)](#cons-1.1) treat_as_floating_point_v is true or - [(1.2)](#cons-1.2) treat_as_floating_point_v is false[.](#cons-1.sentence-1) [*Example [1](#cons-example-1)*: duration d(3); // OK duration d2(3.5); // error — *end example*] [2](#cons-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1346) *Effects*: Initializes rep_ with r[.](#cons-2.sentence-1) [🔗](#lib:duration,constructor_) `template constexpr duration(const duration& d); ` [3](#cons-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1358) *Constraints*: No overflow is induced in the conversion andtreat_as_floating_point_v is true or bothratio_divide​::​den is 1 andtreat_as_floating_point_v is false[.](#cons-3.sentence-1) [*Note [1](#cons-note-1)*: This requirement prevents implicit truncation error when converting between integral-based duration types[.](#cons-3.sentence-2) Such a construction could easily lead to confusion about the value of the duration[.](#cons-3.sentence-3) — *end note*] [*Example [2](#cons-example-2)*: duration ms(3); duration us = ms; // OK duration ms2 = us; // error — *end example*] [4](#cons-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1378) *Effects*: Initializes rep_ with duration_cast(d).count()[.](#cons-4.sentence-1) ### [30.5.3](#observer) Observer [[time.duration.observer]](time.duration.observer) [🔗](#lib:count,duration) `constexpr rep count() const; ` [1](#observer-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1391) *Returns*: rep_[.](#observer-1.sentence-1) ### [30.5.4](#arithmetic) Arithmetic [[time.duration.arithmetic]](time.duration.arithmetic) [🔗](#lib:operator+,duration) `constexpr common_type_t operator+() const; ` [1](#arithmetic-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1404) *Returns*: common_type_t(*this)[.](#arithmetic-1.sentence-1) [🔗](#lib:operator-,duration) `constexpr common_type_t operator-() const; ` [2](#arithmetic-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1415) *Returns*: common_type_t(-rep_)[.](#arithmetic-2.sentence-1) [🔗](#lib:operator++,duration) `constexpr duration& operator++(); ` [3](#arithmetic-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1426) *Effects*: Equivalent to: ++rep_[.](#arithmetic-3.sentence-1) [4](#arithmetic-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1430) *Returns*: *this[.](#arithmetic-4.sentence-1) [🔗](#lib:operator++,duration_) `constexpr duration operator++(int); ` [5](#arithmetic-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1441) *Effects*: Equivalent to: return duration(rep_++); [🔗](#lib:operator--,duration) `constexpr duration& operator--(); ` [6](#arithmetic-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1452) *Effects*: Equivalent to: --rep_[.](#arithmetic-6.sentence-1) [7](#arithmetic-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1456) *Returns*: *this[.](#arithmetic-7.sentence-1) [🔗](#lib:operator--,duration_) `constexpr duration operator--(int); ` [8](#arithmetic-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1467) *Effects*: Equivalent to: return duration(rep_--); [🔗](#lib:operator+=,duration) `constexpr duration& operator+=(const duration& d); ` [9](#arithmetic-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1478) *Effects*: Equivalent to: rep_ += d.count()[.](#arithmetic-9.sentence-1) [10](#arithmetic-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1482) *Returns*: *this[.](#arithmetic-10.sentence-1) [🔗](#lib:operator-=,duration) `constexpr duration& operator-=(const duration& d); ` [11](#arithmetic-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1493) *Effects*: Equivalent to: rep_ -= d.count()[.](#arithmetic-11.sentence-1) [12](#arithmetic-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1497) *Returns*: *this[.](#arithmetic-12.sentence-1) [🔗](#lib:operator*=,duration) `constexpr duration& operator*=(const rep& rhs); ` [13](#arithmetic-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1508) *Effects*: Equivalent to: rep_ *= rhs[.](#arithmetic-13.sentence-1) [14](#arithmetic-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1512) *Returns*: *this[.](#arithmetic-14.sentence-1) [🔗](#lib:operator/=,duration) `constexpr duration& operator/=(const rep& rhs); ` [15](#arithmetic-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1523) *Effects*: Equivalent to: rep_ /= rhs[.](#arithmetic-15.sentence-1) [16](#arithmetic-16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1527) *Returns*: *this[.](#arithmetic-16.sentence-1) [🔗](#lib:operator%25=,duration) `constexpr duration& operator%=(const rep& rhs); ` [17](#arithmetic-17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1538) *Effects*: Equivalent to: rep_ %= rhs[.](#arithmetic-17.sentence-1) [18](#arithmetic-18) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1542) *Returns*: *this[.](#arithmetic-18.sentence-1) [🔗](#lib:operator%25=,duration_) `constexpr duration& operator%=(const duration& rhs); ` [19](#arithmetic-19) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1553) *Effects*: Equivalent to: rep_ %= rhs.count()[.](#arithmetic-19.sentence-1) [20](#arithmetic-20) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1557) *Returns*: *this[.](#arithmetic-20.sentence-1) ### [30.5.5](#special) Special values [[time.duration.special]](time.duration.special) [🔗](#lib:zero,duration) `static constexpr duration zero() noexcept; ` [1](#special-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1571) *Returns*: duration(duration_values​::​zero())[.](#special-1.sentence-1) [🔗](#lib:min,duration) `static constexpr duration min() noexcept; ` [2](#special-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1582) *Returns*: duration(duration_values​::​min())[.](#special-2.sentence-1) [🔗](#lib:max,duration) `static constexpr duration max() noexcept; ` [3](#special-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1593) *Returns*: duration(duration_values​::​max())[.](#special-3.sentence-1) ### [30.5.6](#nonmember) Non-member arithmetic [[time.duration.nonmember]](time.duration.nonmember) [1](#nonmember-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1600) In the function descriptions that follow, unless stated otherwise, let CD represent the return type of the function[.](#nonmember-1.sentence-1) [🔗](#lib:operator+,duration_) `template constexpr common_type_t, duration> operator+(const duration& lhs, const duration& rhs); ` [2](#nonmember-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1612) *Returns*: CD(CD(lhs).count() + CD(rhs).count())[.](#nonmember-2.sentence-1) [🔗](#lib:operator-,duration_) `template constexpr common_type_t, duration> operator-(const duration& lhs, const duration& rhs); ` [3](#nonmember-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1625) *Returns*: CD(CD(lhs).count() - CD(rhs).count())[.](#nonmember-3.sentence-1) [🔗](#lib:operator*,duration) `template constexpr duration, Period> operator*(const duration& d, const Rep2& s); ` [4](#nonmember-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1638) *Constraints*: is_convertible_v> is true[.](#nonmember-4.sentence-1) [5](#nonmember-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1642) *Returns*: CD(CD(d).count() * s)[.](#nonmember-5.sentence-1) [🔗](#lib:operator*,duration_) `template constexpr duration, Period> operator*(const Rep1& s, const duration& d); ` [6](#nonmember-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1655) *Constraints*: is_convertible_v> is true[.](#nonmember-6.sentence-1) [7](#nonmember-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1659) *Returns*: d * s[.](#nonmember-7.sentence-1) [🔗](#lib:operator/,duration) `template constexpr duration, Period> operator/(const duration& d, const Rep2& s); ` [8](#nonmember-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1672) *Constraints*: is_convertible_v> is true and Rep2 is not a specialization of duration[.](#nonmember-8.sentence-1) [9](#nonmember-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1677) *Returns*: CD(CD(d).count() / s)[.](#nonmember-9.sentence-1) [🔗](#lib:operator/,duration_) `template constexpr common_type_t operator/(const duration& lhs, const duration& rhs); ` [10](#nonmember-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1690) Let CD becommon_type_t, duration>[.](#nonmember-10.sentence-1) [11](#nonmember-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1694) *Returns*: CD(lhs).count() / CD(rhs).count()[.](#nonmember-11.sentence-1) [🔗](#lib:operator%25,duration) `template constexpr duration, Period> operator%(const duration& d, const Rep2& s); ` [12](#nonmember-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1707) *Constraints*: is_convertible_v> is true andRep2 is not a specialization of duration[.](#nonmember-12.sentence-1) [13](#nonmember-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1712) *Returns*: CD(CD(d).count() % s)[.](#nonmember-13.sentence-1) [🔗](#lib:operator%25,duration_) `template constexpr common_type_t, duration> operator%(const duration& lhs, const duration& rhs); ` [14](#nonmember-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1725) *Returns*: CD(CD(lhs).count() % CD(rhs).count())[.](#nonmember-14.sentence-1) ### [30.5.7](#comparisons) Comparisons [[time.duration.comparisons]](time.duration.comparisons) [1](#comparisons-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1733) In the function descriptions that follow, CT representscommon_type_t, where A and B are the types of the two arguments to the function[.](#comparisons-1.sentence-1) [🔗](#lib:operator==,duration) `template constexpr bool operator==(const duration& lhs, const duration& rhs); ` [2](#comparisons-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1746) *Returns*: CT(lhs).count() == CT(rhs).count()[.](#comparisons-2.sentence-1) [🔗](#lib:operator%3c,duration) `template constexpr bool operator<(const duration& lhs, const duration& rhs); ` [3](#comparisons-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1759) *Returns*: CT(lhs).count() < CT(rhs).count()[.](#comparisons-3.sentence-1) [🔗](#lib:operator%3e,duration) `template constexpr bool operator>(const duration& lhs, const duration& rhs); ` [4](#comparisons-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1772) *Returns*: rhs < lhs[.](#comparisons-4.sentence-1) [🔗](#lib:operator%3c=,duration) `template constexpr bool operator<=(const duration& lhs, const duration& rhs); ` [5](#comparisons-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1785) *Returns*: !(rhs < lhs)[.](#comparisons-5.sentence-1) [🔗](#lib:operator%3e=,duration) `template constexpr bool operator>=(const duration& lhs, const duration& rhs); ` [6](#comparisons-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1798) *Returns*: !(lhs < rhs)[.](#comparisons-6.sentence-1) [🔗](#lib:operator%3c=%3e,duration) `template requires [three_way_comparable](cmp.concept#concept:three_way_comparable "17.12.4 Concept three_­way_­comparable [cmp.concept]") constexpr auto operator<=>(const duration& lhs, const duration& rhs); ` [7](#comparisons-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1812) *Returns*: CT(lhs).count() <=> CT(rhs).count()[.](#comparisons-7.sentence-1) ### [30.5.8](#cast) Conversions [[time.duration.cast]](time.duration.cast) [🔗](#lib:duration,duration_cast) `template constexpr ToDuration duration_cast(const duration& d); ` [1](#cast-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1827) *Constraints*: ToDuration is a specialization of duration[.](#cast-1.sentence-1) [2](#cast-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1831) *Returns*: Let CF be ratio_divide, and CR be common_type​::​type[.](#cast-2.sentence-1) - [(2.1)](#cast-2.1) If CF​::​num == 1 and CF​::​den == 1, returnsToDuration(static_cast(d.count())) - [(2.2)](#cast-2.2) otherwise, if CF​::​num != 1 and CF​::​den == 1, returnsToDuration(static_cast(static_cast(d.count()) * static_cast(CF::num))) - [(2.3)](#cast-2.3) otherwise, if CF​::​num == 1 and CF​::​den != 1, returnsToDuration(static_cast(static_cast(d.count()) / static_cast(CF::den))) - [(2.4)](#cast-2.4) otherwise, returnsToDuration(static_cast(static_cast(d.count()) * static_cast(CF::num) / static_cast(CF::den))) [3](#cast-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1861) [*Note [1](#cast-note-1)*: This function does not use any implicit conversions; all conversions are done with static_cast[.](#cast-3.sentence-1) It avoids multiplications and divisions when it is known at compile time that one or more arguments is 1[.](#cast-3.sentence-2) Intermediate computations are carried out in the widest representation and only converted to the destination representation at the final step[.](#cast-3.sentence-3) — *end note*] [🔗](#lib:floor,duration) `template constexpr ToDuration floor(const duration& d); ` [4](#cast-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1878) *Constraints*: ToDuration is a specialization of duration[.](#cast-4.sentence-1) [5](#cast-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1882) *Returns*: The greatest result t representable in ToDuration for which t <= d[.](#cast-5.sentence-1) [🔗](#lib:ceil,duration) `template constexpr ToDuration ceil(const duration& d); ` [6](#cast-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1895) *Constraints*: ToDuration is a specialization of duration[.](#cast-6.sentence-1) [7](#cast-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1899) *Returns*: The least result t representable in ToDuration for which t >= d[.](#cast-7.sentence-1) [🔗](#lib:round,duration) `template constexpr ToDuration round(const duration& d); ` [8](#cast-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1912) *Constraints*: ToDuration is a specialization of duration andtreat_as_floating_point_v is false[.](#cast-8.sentence-1) [9](#cast-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1917) *Returns*: The value of ToDuration that is closest to d[.](#cast-9.sentence-1) If there are two closest values, then return the value t for which t % 2 == 0[.](#cast-9.sentence-2) ### [30.5.9](#literals) Suffixes for duration literals [[time.duration.literals]](time.duration.literals) [1](#literals-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1926) This subclause describes literal suffixes for constructing duration literals[.](#literals-1.sentence-1) The suffixes h, min, s, ms, us, ns denote duration values of the corresponding types hours, minutes,seconds, milliseconds, microseconds, and nanoseconds respectively if they are applied to [*integer-literal*](lex.icon#nt:integer-literal "5.13.2 Integer literals [lex.icon]")*s*[.](#literals-1.sentence-2) [2](#literals-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1933) If any of these suffixes are applied to a [*floating-point-literal*](lex.fcon#nt:floating-point-literal "5.13.4 Floating-point literals [lex.fcon]") the result is achrono​::​duration literal with an unspecified floating-point representation[.](#literals-2.sentence-1) [3](#literals-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1938) If any of these suffixes are applied to an [*integer-literal*](lex.icon#nt:integer-literal "5.13.2 Integer literals [lex.icon]") and the resultingchrono​::​duration value cannot be represented in the result type because of overflow, the program is ill-formed[.](#literals-3.sentence-1) [4](#literals-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1944) [*Example [1](#literals-example-1)*: The following code shows some duration literals[.](#literals-4.sentence-1) using namespace std::chrono_literals;auto constexpr aday=24h;auto constexpr lesson=45min;auto constexpr halfanhour=0.5h; — *end example*] [🔗](#lib:operator%22%22h,duration) `constexpr chrono::hours operator""h(unsigned long long hours); constexpr chrono::duration> operator""h(long double hours); ` [5](#literals-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1962) *Returns*: A duration literal representing hours hours[.](#literals-5.sentence-1) [🔗](#lib:operator%22%22min,duration) `constexpr chrono::minutes operator""min(unsigned long long minutes); constexpr chrono::duration> operator""min(long double minutes); ` [6](#literals-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1974) *Returns*: A duration literal representing minutes minutes[.](#literals-6.sentence-1) [🔗](#lib:operator%22%22s,duration) `constexpr chrono::seconds operator""s(unsigned long long sec); constexpr chrono::duration operator""s(long double sec); ` [7](#literals-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1986) *Returns*: A duration literal representing sec seconds[.](#literals-7.sentence-1) [8](#literals-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1990) [*Note [1](#literals-note-1)*: The same suffix s is used for basic_string but there is no conflict, since duration suffixes apply to numbers and string literal suffixes apply to character array literals[.](#literals-8.sentence-1) — *end note*] [🔗](#lib:operator%22%22ms,duration) `constexpr chrono::milliseconds operator""ms(unsigned long long msec); constexpr chrono::duration operator""ms(long double msec); ` [9](#literals-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2005) *Returns*: A duration literal representing msec milliseconds[.](#literals-9.sentence-1) [🔗](#lib:operator%22%22us,duration) `constexpr chrono::microseconds operator""us(unsigned long long usec); constexpr chrono::duration operator""us(long double usec); ` [10](#literals-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2017) *Returns*: A duration literal representing usec microseconds[.](#literals-10.sentence-1) [🔗](#lib:operator%22%22ns,duration) `constexpr chrono::nanoseconds operator""ns(unsigned long long nsec); constexpr chrono::duration operator""ns(long double nsec); ` [11](#literals-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2029) *Returns*: A duration literal representing nsec nanoseconds[.](#literals-11.sentence-1) ### [30.5.10](#alg) Algorithms [[time.duration.alg]](time.duration.alg) [🔗](#lib:abs,duration) `template constexpr duration abs(duration d); ` [1](#alg-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2043) *Constraints*: numeric_limits​::​is_signed is true[.](#alg-1.sentence-1) [2](#alg-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2047) *Returns*: If d >= d.zero(), return d, otherwise return -d[.](#alg-2.sentence-1) ### [30.5.11](#io) I/O [[time.duration.io]](time.duration.io) [🔗](#lib:operator%3c%3c,duration) `template basic_ostream& operator<<(basic_ostream& os, const duration& d); ` [1](#io-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2063) *Effects*: Inserts the duration d onto the stream os as if it were implemented as follows:basic_ostringstream s; s.flags(os.flags()); s.imbue(os.getloc()); s.precision(os.precision()); s << d.count() << *units-suffix*;return os << s.str(); where *units-suffix* depends on the type Period​::​type as follows: - [(1.1)](#io-1.1) If Period​::​type is atto,*units-suffix* is "as"[.](#io-1.1.sentence-1) - [(1.2)](#io-1.2) Otherwise, if Period​::​type is femto,*units-suffix* is "fs"[.](#io-1.2.sentence-1) - [(1.3)](#io-1.3) Otherwise, if Period​::​type is pico,*units-suffix* is "ps"[.](#io-1.3.sentence-1) - [(1.4)](#io-1.4) Otherwise, if Period​::​type is nano,*units-suffix* is "ns"[.](#io-1.4.sentence-1) - [(1.5)](#io-1.5) Otherwise, if Period​::​type is micro, it isimplementation-defined whether *units-suffix* is"μs" ("\u00b5\u0073") or"us"[.](#io-1.5.sentence-1) - [(1.6)](#io-1.6) Otherwise, if Period​::​type is milli,*units-suffix* is "ms"[.](#io-1.6.sentence-1) - [(1.7)](#io-1.7) Otherwise, if Period​::​type is centi,*units-suffix* is "cs"[.](#io-1.7.sentence-1) - [(1.8)](#io-1.8) Otherwise, if Period​::​type is deci,*units-suffix* is "ds"[.](#io-1.8.sentence-1) - [(1.9)](#io-1.9) Otherwise, if Period​::​type is ratio<1>,*units-suffix* is "s"[.](#io-1.9.sentence-1) - [(1.10)](#io-1.10) Otherwise, if Period​::​type is deca,*units-suffix* is "das"[.](#io-1.10.sentence-1) - [(1.11)](#io-1.11) Otherwise, if Period​::​type is hecto,*units-suffix* is "hs"[.](#io-1.11.sentence-1) - [(1.12)](#io-1.12) Otherwise, if Period​::​type is kilo,*units-suffix* is "ks"[.](#io-1.12.sentence-1) - [(1.13)](#io-1.13) Otherwise, if Period​::​type is mega,*units-suffix* is "Ms"[.](#io-1.13.sentence-1) - [(1.14)](#io-1.14) Otherwise, if Period​::​type is giga,*units-suffix* is "Gs"[.](#io-1.14.sentence-1) - [(1.15)](#io-1.15) Otherwise, if Period​::​type is tera,*units-suffix* is "Ts"[.](#io-1.15.sentence-1) - [(1.16)](#io-1.16) Otherwise, if Period​::​type is peta,*units-suffix* is "Ps"[.](#io-1.16.sentence-1) - [(1.17)](#io-1.17) Otherwise, if Period​::​type is exa,*units-suffix* is "Es"[.](#io-1.17.sentence-1) - [(1.18)](#io-1.18) Otherwise, if Period​::​type is ratio<60>,*units-suffix* is "min"[.](#io-1.18.sentence-1) - [(1.19)](#io-1.19) Otherwise, if Period​::​type is ratio<3600>,*units-suffix* is "h"[.](#io-1.19.sentence-1) - [(1.20)](#io-1.20) Otherwise, if Period​::​type is ratio<86400>,*units-suffix* is "d"[.](#io-1.20.sentence-1) - [(1.21)](#io-1.21) Otherwise, if Period​::​type​::​den == 1,*units-suffix* is "[*num*]s"[.](#io-1.21.sentence-1) - [(1.22)](#io-1.22) Otherwise, *units-suffix* is"[*num*/*den*]s"[.](#io-1.22.sentence-1) In the list above, the use of *num* and *den* refers to the static data members of Period​::​type, which are converted to arrays of charT using a decimal conversion with no leading zeroes[.](#io-1.sentence-2) [2](#io-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2176) *Returns*: os[.](#io-2.sentence-1) [🔗](#lib:from_stream,duration) `template> basic_istream& from_stream(basic_istream& is, const charT* fmt, duration& d, basic_string* abbrev = nullptr, minutes* offset = nullptr); ` [3](#io-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2192) *Effects*: Attempts to parse the input stream is into the duration d using the format flags given in the NTCTS fmt as specified in [[time.parse]](time.parse "30.13 Parsing")[.](#io-3.sentence-1) If the parse fails to decode a valid duration,is.setstate(ios_base​::​failbit) is called and d is not modified[.](#io-3.sentence-2) If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null[.](#io-3.sentence-3) If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null[.](#io-3.sentence-4) [4](#io-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L2205) *Returns*: is[.](#io-4.sentence-1)