81 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410// <chrono>11 12// floor13 14// template <class ToDuration, class Clock, class Duration>15// time_point<Clock, ToDuration>16// floor(const time_point<Clock, Duration>& t);17 18#include <chrono>19#include <type_traits>20#include <cassert>21 22#include "test_macros.h"23 24template <class T, class = void>25inline constexpr bool has_floor_v = false;26 27template <class T>28inline constexpr bool has_floor_v<T, decltype((void)std::chrono::floor<T>(std::chrono::system_clock::now()))> = true;29 30static_assert(has_floor_v<std::chrono::seconds>);31static_assert(!has_floor_v<int>);32 33template <class FromDuration, class ToDuration>34void35test(const FromDuration& df, const ToDuration& d)36{37 typedef std::chrono::system_clock Clock;38 typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;39 typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;40 {41 FromTimePoint f(df);42 ToTimePoint t(d);43 typedef decltype(std::chrono::floor<ToDuration>(f)) R;44 static_assert((std::is_same<R, ToTimePoint>::value), "");45 assert(std::chrono::floor<ToDuration>(f) == t);46 }47}48 49template<class FromDuration, long long From, class ToDuration, long long To>50void test_constexpr ()51{52 typedef std::chrono::system_clock Clock;53 typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;54 typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;55 {56 constexpr FromTimePoint f{FromDuration{From}};57 constexpr ToTimePoint t{ToDuration{To}};58 static_assert(std::chrono::floor<ToDuration>(f) == t, "");59 }60}61 62int main(int, char**)63{64// 7290000ms is 2 hours, 1 minute, and 30 seconds65 test(std::chrono::milliseconds( 7290000), std::chrono::hours( 2));66 test(std::chrono::milliseconds(-7290000), std::chrono::hours(-3));67 test(std::chrono::milliseconds( 7290000), std::chrono::minutes( 121));68 test(std::chrono::milliseconds(-7290000), std::chrono::minutes(-122));69 70// 9000000ms is 2 hours and 30 minutes71 test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::hours, 2> ();72 test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::hours, -3> ();73 test_constexpr<std::chrono::milliseconds, 9000001, std::chrono::minutes, 150> ();74 test_constexpr<std::chrono::milliseconds,-9000001, std::chrono::minutes,-151> ();75 76 test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::seconds, 9000> ();77 test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::seconds,-9000> ();78 79 return 0;80}81