[time.duration.cast]

# 30 Time library [[time]](./#time)

## 30.5 Class template duration [[time.duration]](time.duration#cast)

### 30.5.8 Conversions [time.duration.cast]

[ð](#lib:duration,duration_cast)

`template<class ToDuration, class Rep, class Period>
  constexpr ToDuration duration_cast(const duration<Rep, Period>& d);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1827)

*Constraints*: ToDuration is a specialization of duration[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1831)

*Returns*: Let CF be ratio_divide<Period, typename ToDuration​::​period>, and CR be common_type<typename ToDuration​::​rep, Rep, intmax_t>​::​type[.](#2.sentence-1)

- [(2.1)](#2.1)

If CF​::​num == 1 and CF​::​den == 1, returnsToDuration(static_cast<typename ToDuration::rep>(d.count()))

- [(2.2)](#2.2)

otherwise, if CF​::​num != 1 and CF​::​den == 1, returnsToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) * static_cast<CR>(CF::num)))

- [(2.3)](#2.3)

otherwise, if CF​::​num == 1 and CF​::​den != 1, returnsToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) / static_cast<CR>(CF::den)))

- [(2.4)](#2.4)

otherwise, returnsToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) * static_cast<CR>(CF::num) / static_cast<CR>(CF::den)))

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1861)

[*Note [1](#note-1)*:

This function does not use any implicit conversions; all conversions
are done with static_cast[.](#3.sentence-1)

It avoids multiplications and divisions when
it is known at compile time that one or more arguments is 1[.](#3.sentence-2)

Intermediate
computations are carried out in the widest representation and only converted to
the destination representation at the final step[.](#3.sentence-3)

â *end note*]

[ð](#lib:floor,duration)

`template<class ToDuration, class Rep, class Period>
  constexpr ToDuration floor(const duration<Rep, Period>& d);
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1878)

*Constraints*: ToDuration is a specialization of duration[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1882)

*Returns*: The greatest result t representable in ToDuration for which t <= d[.](#5.sentence-1)

[ð](#lib:ceil,duration)

`template<class ToDuration, class Rep, class Period>
  constexpr ToDuration ceil(const duration<Rep, Period>& d);
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1895)

*Constraints*: ToDuration is a specialization of duration[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1899)

*Returns*: The least result t representable in ToDuration for which t >= d[.](#7.sentence-1)

[ð](#lib:round,duration)

`template<class ToDuration, class Rep, class Period>
  constexpr ToDuration round(const duration<Rep, Period>& d);
`

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1912)

*Constraints*: ToDuration is a specialization of duration andtreat_as_floating_point_v<typename ToDuration​::​rep> is false[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/time.tex#L1917)

*Returns*: The value of ToDuration that is closest to d[.](#9.sentence-1)

If there are two closest values, then return the value t for which t % 2 == 0[.](#9.sentence-2)
