1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14
10// <chrono>
11
12// floor
13
14// template <class ToDuration, class Rep, class Period>
15// constexpr
16// ToDuration
17// floor(const duration<Rep, Period>& d);
18
19#include <chrono>
20#include <type_traits>
21#include <cassert>
22
23#include "test_macros.h"
24
25template <class ToDuration, class FromDuration>
26void
27test(const FromDuration& f, const ToDuration& d)
28{
29 {
30 typedef decltype(std::chrono::floor<ToDuration>(f)) R;
31 static_assert((std::is_same<R, ToDuration>::value), "");
32 assert(std::chrono::floor<ToDuration>(f) == d);
33 }
34}
35
36int main(int, char**)
37{
38// 7290000ms is 2 hours, 1 minute, and 30 seconds
39 test(f: std::chrono::milliseconds( 7290000), d: std::chrono::hours( 2));
40 test(f: std::chrono::milliseconds(-7290000), d: std::chrono::hours(-3));
41 test(f: std::chrono::milliseconds( 7290000), d: std::chrono::minutes( 121));
42 test(f: std::chrono::milliseconds(-7290000), d: std::chrono::minutes(-122));
43
44 {
45// 9000000ms is 2 hours and 30 minutes
46 constexpr std::chrono::hours h1 = std::chrono::floor<std::chrono::hours>(d: std::chrono::milliseconds(9000000));
47 static_assert(h1.count() == 2, "");
48 constexpr std::chrono::hours h2 = std::chrono::floor<std::chrono::hours>(d: std::chrono::milliseconds(-9000000));
49 static_assert(h2.count() == -3, "");
50 }
51
52 return 0;
53}
54

source code of libcxx/test/std/time/time.duration/time.duration.cast/floor.pass.cpp