From 66a5aa482e97a9b2ad89d50657a52f94ae492dc6 Mon Sep 17 00:00:00 2001 From: nanoric Date: Wed, 18 Dec 2019 00:21:54 +0800 Subject: [PATCH] Add constexpr for clock_cast. --- include/date/tz.h | 19 +++++++ test/clock_cast_test/constexpr.pass.cpp | 75 +++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 test/clock_cast_test/constexpr.pass.cpp diff --git a/include/date/tz.h b/include/date/tz.h index 22eac55..45fab11 100644 --- a/include/date/tz.h +++ b/include/date/tz.h @@ -2288,6 +2288,7 @@ template <> struct clock_time_conversion { template + CONSTCD14 sys_time operator()(const sys_time& st) const { @@ -2299,6 +2300,7 @@ template <> struct clock_time_conversion { template + CONSTCD14 utc_time operator()(const utc_time& ut) const { @@ -2310,6 +2312,7 @@ template<> struct clock_time_conversion { template + CONSTCD14 local_time operator()(const local_time& lt) const { @@ -2343,6 +2346,7 @@ template<> struct clock_time_conversion { template + CONSTCD14 local_time operator()(const sys_time& st) const { @@ -2354,6 +2358,7 @@ template<> struct clock_time_conversion { template + CONSTCD14 sys_time operator()(const local_time& lt) const { @@ -2387,6 +2392,7 @@ template struct clock_time_conversion { template + CONSTCD14 std::chrono::time_point operator()(const std::chrono::time_point& tp) const { @@ -2533,6 +2539,7 @@ template struct clock_time_conversion { template + CONSTCD14 typename ctc_detail::return_to_sys::type operator()(const std::chrono::time_point& tp) const { @@ -2544,6 +2551,7 @@ template struct clock_time_conversion { template + CONSTCD14 typename ctc_detail::return_from_sys::type operator()(const sys_time& st) const { @@ -2555,6 +2563,7 @@ template struct clock_time_conversion { template + CONSTCD14 typename ctc_detail::return_to_utc::type operator()(const std::chrono::time_point& tp) const { @@ -2566,6 +2575,7 @@ template struct clock_time_conversion { template + CONSTCD14 typename ctc_detail::return_from_utc::type operator()(const utc_time& ut) const { @@ -2577,6 +2587,7 @@ template struct clock_time_conversion { template + CONSTCD14 typename ctc_detail::return_to_local::type operator()(const std::chrono::time_point& tp) const { @@ -2588,6 +2599,7 @@ template struct clock_time_conversion { template + CONSTCD14 typename ctc_detail::return_from_local::type operator()(const local_time& lt) const { @@ -2603,6 +2615,7 @@ template using std::chrono::system_clock; template +CONSTCD14 auto conv_clock(const time_point& t) -> decltype(std::declval>()(t)) @@ -2612,6 +2625,7 @@ conv_clock(const time_point& t) //direct trait conversion, 1st candidate template +CONSTCD14 auto cc_impl(const time_point& t, const time_point*) -> decltype(conv_clock(t)) @@ -2621,6 +2635,7 @@ cc_impl(const time_point& t, const time_point +CONSTCD14 auto cc_impl(const time_point& t, const void*) -> decltype(conv_clock(conv_clock(t))) @@ -2630,6 +2645,7 @@ cc_impl(const time_point& t, const void*) //conversion through utc, 2nd candidate template +CONSTCD14 auto cc_impl(const time_point& t, const void*) -> decltype(0, // MSVC_WORKAROUND @@ -2640,6 +2656,7 @@ cc_impl(const time_point& t, const void*) //conversion through sys and utc, 3rd candidate template +CONSTCD14 auto cc_impl(const time_point& t, ...) -> decltype(conv_clock(conv_clock(conv_clock(t)))) @@ -2649,6 +2666,7 @@ cc_impl(const time_point& t, ...) //conversion through utc and sys, 3rd candidate template +CONSTCD14 auto cc_impl(const time_point& t, ...) -> decltype(0, // MSVC_WORKAROUND @@ -2660,6 +2678,7 @@ cc_impl(const time_point& t, ...) } // namespace clock_cast_detail template +CONSTCD14 auto clock_cast(const std::chrono::time_point& tp) -> decltype(clock_cast_detail::cc_impl(tp, &tp)) diff --git a/test/clock_cast_test/constexpr.pass.cpp b/test/clock_cast_test/constexpr.pass.cpp new file mode 100644 index 0000000..5fdb758 --- /dev/null +++ b/test/clock_cast_test/constexpr.pass.cpp @@ -0,0 +1,75 @@ +// The MIT License (MIT) +// +// Copyright (c) 2019 nanoric +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "tz.h" +#include +#include + +struct const_clock { + using duration = + typename std::common_type::type; + using rep = duration::rep; + using period = duration::period; + using time_point = std::chrono::time_point; + + static constexpr date::sys_days epoch { date::days { 1000 } }; + + template + static std::chrono::time_point::type> + CONSTCD11 to_sys(std::chrono::time_point const& tp) + { + return epoch + tp.time_since_epoch(); + } + + template + static std::chrono::time_point::type> + CONSTCD11 from_sys( + std::chrono::time_point const& + tp) + { + using res = std::chrono::time_point::type>; + return res(tp - epoch); + } +}; + +int main() +{ + using namespace date; + using namespace std::chrono; + using const_days = time_point; + + CONSTCD14 sys_days sys { days { 1024 } }; + static_assert(sys.time_since_epoch().count() == 1024, ""); + + CONSTCD14 const_days c {clock_cast(sys)}; + CONSTCD14 sys_days sys2 {clock_cast(c)}; + CONSTCD14 sys_days sys3 { clock_cast(const_days(days(48))) }; +#if __cplusplus >= 201402L + static_assert(c.time_since_epoch().count() == 24, ""); + static_assert(sys2.time_since_epoch().count() == 1024, ""); + static_assert(sys3.time_since_epoch().count() == 1048, ""); +#endif +}