1223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow//===----------------------------------------------------------------------===// 2223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// 3223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// The LLVM Compiler Infrastructure 4223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// 5223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// This file is dual licensed under the MIT and the University of Illinois Open 6223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// Source Licenses. See LICENSE.TXT for details. 7223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// 8223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow//===----------------------------------------------------------------------===// 9223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 101575e3e813f7f7cde31e699802fa1fcf8e84531cAsiri Rathnayake// UNSUPPORTED: c++98, c++03, c++11, c++14 11223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// <chrono> 12223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 13223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// round 14223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 15223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// template <class ToDuration, class Clock, class Duration> 16223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// time_point<Clock, ToDuration> 17223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// round(const time_point<Clock, Duration>& t); 18223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 19223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow#include <chrono> 20223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow#include <type_traits> 21223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow#include <cassert> 22223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 23223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clowtemplate <class FromDuration, class ToDuration> 24223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clowvoid 25223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clowtest(const FromDuration& df, const ToDuration& d) 26223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow{ 27223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow typedef std::chrono::system_clock Clock; 28223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint; 29223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint; 30223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow { 31223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow FromTimePoint f(df); 32223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow ToTimePoint t(d); 33223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow typedef decltype(std::chrono::round<ToDuration>(f)) R; 34223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow static_assert((std::is_same<R, ToTimePoint>::value), ""); 35223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow assert(std::chrono::round<ToDuration>(f) == t); 36223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow } 37223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow} 38223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 39223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clowtemplate<class FromDuration, long long From, class ToDuration, long long To> 40223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clowvoid test_constexpr () 41223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow{ 42223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow typedef std::chrono::system_clock Clock; 43223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint; 44223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint; 45223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow { 46223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow constexpr FromTimePoint f{FromDuration{From}}; 47223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow constexpr ToTimePoint t{ToDuration{To}}; 48223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow static_assert(std::chrono::round<ToDuration>(f) == t, ""); 49223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow } 50223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow} 51223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 52223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clowint main() 53223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow{ 54223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// 7290000ms is 2 hours, 1 minute, and 30 seconds 55223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test(std::chrono::milliseconds( 7290000), std::chrono::hours( 2)); 56223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test(std::chrono::milliseconds(-7290000), std::chrono::hours(-2)); 57223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test(std::chrono::milliseconds( 7290000), std::chrono::minutes( 122)); 58223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test(std::chrono::milliseconds(-7290000), std::chrono::minutes(-122)); 59223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 60223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow// 9000000ms is 2 hours and 30 minutes 61223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::hours, 2> (); 62223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::hours, -2> (); 63223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test_constexpr<std::chrono::milliseconds, 9000001, std::chrono::minutes, 150> (); 64223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test_constexpr<std::chrono::milliseconds,-9000001, std::chrono::minutes,-150> (); 65223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow 66223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::seconds, 9000> (); 67223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::seconds,-9000> (); 68223df2ef0f543f7fef9270b3b34b9c73c869a006Marshall Clow} 69