mirror of
https://github.com/HowardHinnant/date.git
synced 2024-12-27 00:14:07 +08:00
Add constexpr for clock_cast.
This commit is contained in:
parent
b9cd9c3fde
commit
66a5aa482e
@ -2288,6 +2288,7 @@ template <>
|
||||
struct clock_time_conversion<std::chrono::system_clock, std::chrono::system_clock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
sys_time<Duration>
|
||||
operator()(const sys_time<Duration>& st) const
|
||||
{
|
||||
@ -2299,6 +2300,7 @@ template <>
|
||||
struct clock_time_conversion<utc_clock, utc_clock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
utc_time<Duration>
|
||||
operator()(const utc_time<Duration>& ut) const
|
||||
{
|
||||
@ -2310,6 +2312,7 @@ template<>
|
||||
struct clock_time_conversion<local_t, local_t>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
local_time<Duration>
|
||||
operator()(const local_time<Duration>& lt) const
|
||||
{
|
||||
@ -2343,6 +2346,7 @@ template<>
|
||||
struct clock_time_conversion<local_t, std::chrono::system_clock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
local_time<Duration>
|
||||
operator()(const sys_time<Duration>& st) const
|
||||
{
|
||||
@ -2354,6 +2358,7 @@ template<>
|
||||
struct clock_time_conversion<std::chrono::system_clock, local_t>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
sys_time<Duration>
|
||||
operator()(const local_time<Duration>& lt) const
|
||||
{
|
||||
@ -2387,6 +2392,7 @@ template<typename Clock>
|
||||
struct clock_time_conversion<Clock, Clock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
std::chrono::time_point<Clock, Duration>
|
||||
operator()(const std::chrono::time_point<Clock, Duration>& tp) const
|
||||
{
|
||||
@ -2533,6 +2539,7 @@ template <class SrcClock>
|
||||
struct clock_time_conversion<std::chrono::system_clock, SrcClock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
typename ctc_detail::return_to_sys<SrcClock, Duration>::type
|
||||
operator()(const std::chrono::time_point<SrcClock, Duration>& tp) const
|
||||
{
|
||||
@ -2544,6 +2551,7 @@ template <class DstClock>
|
||||
struct clock_time_conversion<DstClock, std::chrono::system_clock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
typename ctc_detail::return_from_sys<DstClock, Duration>::type
|
||||
operator()(const sys_time<Duration>& st) const
|
||||
{
|
||||
@ -2555,6 +2563,7 @@ template <class SrcClock>
|
||||
struct clock_time_conversion<utc_clock, SrcClock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
typename ctc_detail::return_to_utc<SrcClock, Duration>::type
|
||||
operator()(const std::chrono::time_point<SrcClock, Duration>& tp) const
|
||||
{
|
||||
@ -2566,6 +2575,7 @@ template <class DstClock>
|
||||
struct clock_time_conversion<DstClock, utc_clock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
typename ctc_detail::return_from_utc<DstClock, Duration>::type
|
||||
operator()(const utc_time<Duration>& ut) const
|
||||
{
|
||||
@ -2577,6 +2587,7 @@ template<typename SrcClock>
|
||||
struct clock_time_conversion<local_t, SrcClock>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
typename ctc_detail::return_to_local<SrcClock, Duration>::type
|
||||
operator()(const std::chrono::time_point<SrcClock, Duration>& tp) const
|
||||
{
|
||||
@ -2588,6 +2599,7 @@ template<typename DstClock>
|
||||
struct clock_time_conversion<DstClock, local_t>
|
||||
{
|
||||
template <class Duration>
|
||||
CONSTCD14
|
||||
typename ctc_detail::return_from_local<DstClock, Duration>::type
|
||||
operator()(const local_time<Duration>& lt) const
|
||||
{
|
||||
@ -2603,6 +2615,7 @@ template <class Clock, class Duration>
|
||||
using std::chrono::system_clock;
|
||||
|
||||
template <class DstClock, class SrcClock, class Duration>
|
||||
CONSTCD14
|
||||
auto
|
||||
conv_clock(const time_point<SrcClock, Duration>& t)
|
||||
-> decltype(std::declval<clock_time_conversion<DstClock, SrcClock>>()(t))
|
||||
@ -2612,6 +2625,7 @@ conv_clock(const time_point<SrcClock, Duration>& t)
|
||||
|
||||
//direct trait conversion, 1st candidate
|
||||
template <class DstClock, class SrcClock, class Duration>
|
||||
CONSTCD14
|
||||
auto
|
||||
cc_impl(const time_point<SrcClock, Duration>& t, const time_point<SrcClock, Duration>*)
|
||||
-> decltype(conv_clock<DstClock>(t))
|
||||
@ -2621,6 +2635,7 @@ cc_impl(const time_point<SrcClock, Duration>& t, const time_point<SrcClock, Dura
|
||||
|
||||
//conversion through sys, 2nd candidate
|
||||
template <class DstClock, class SrcClock, class Duration>
|
||||
CONSTCD14
|
||||
auto
|
||||
cc_impl(const time_point<SrcClock, Duration>& t, const void*)
|
||||
-> decltype(conv_clock<DstClock>(conv_clock<system_clock>(t)))
|
||||
@ -2630,6 +2645,7 @@ cc_impl(const time_point<SrcClock, Duration>& t, const void*)
|
||||
|
||||
//conversion through utc, 2nd candidate
|
||||
template <class DstClock, class SrcClock, class Duration>
|
||||
CONSTCD14
|
||||
auto
|
||||
cc_impl(const time_point<SrcClock, Duration>& t, const void*)
|
||||
-> decltype(0, // MSVC_WORKAROUND
|
||||
@ -2640,6 +2656,7 @@ cc_impl(const time_point<SrcClock, Duration>& t, const void*)
|
||||
|
||||
//conversion through sys and utc, 3rd candidate
|
||||
template <class DstClock, class SrcClock, class Duration>
|
||||
CONSTCD14
|
||||
auto
|
||||
cc_impl(const time_point<SrcClock, Duration>& t, ...)
|
||||
-> decltype(conv_clock<DstClock>(conv_clock<utc_clock>(conv_clock<system_clock>(t))))
|
||||
@ -2649,6 +2666,7 @@ cc_impl(const time_point<SrcClock, Duration>& t, ...)
|
||||
|
||||
//conversion through utc and sys, 3rd candidate
|
||||
template <class DstClock, class SrcClock, class Duration>
|
||||
CONSTCD14
|
||||
auto
|
||||
cc_impl(const time_point<SrcClock, Duration>& t, ...)
|
||||
-> decltype(0, // MSVC_WORKAROUND
|
||||
@ -2660,6 +2678,7 @@ cc_impl(const time_point<SrcClock, Duration>& t, ...)
|
||||
} // namespace clock_cast_detail
|
||||
|
||||
template <class DstClock, class SrcClock, class Duration>
|
||||
CONSTCD14
|
||||
auto
|
||||
clock_cast(const std::chrono::time_point<SrcClock, Duration>& tp)
|
||||
-> decltype(clock_cast_detail::cc_impl<DstClock>(tp, &tp))
|
||||
|
75
test/clock_cast_test/constexpr.pass.cpp
Normal file
75
test/clock_cast_test/constexpr.pass.cpp
Normal file
@ -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 <cassert>
|
||||
#include <type_traits>
|
||||
|
||||
struct const_clock {
|
||||
using duration =
|
||||
typename std::common_type<std::chrono::system_clock::duration,
|
||||
date::days>::type;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<const_clock, duration>;
|
||||
|
||||
static constexpr date::sys_days epoch { date::days { 1000 } };
|
||||
|
||||
template <typename Duration>
|
||||
static std::chrono::time_point<std::chrono::system_clock,
|
||||
typename std::common_type<Duration, date::days>::type>
|
||||
CONSTCD11 to_sys(std::chrono::time_point<const_clock, Duration> const& tp)
|
||||
{
|
||||
return epoch + tp.time_since_epoch();
|
||||
}
|
||||
|
||||
template <typename Duration>
|
||||
static std::chrono::time_point<const_clock,
|
||||
typename std::common_type<Duration, date::days>::type>
|
||||
CONSTCD11 from_sys(
|
||||
std::chrono::time_point<std::chrono::system_clock, Duration> const&
|
||||
tp)
|
||||
{
|
||||
using res = std::chrono::time_point<const_clock,
|
||||
typename std::common_type<Duration, date::days>::type>;
|
||||
return res(tp - epoch);
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
using const_days = time_point<const_clock, days>;
|
||||
|
||||
CONSTCD14 sys_days sys { days { 1024 } };
|
||||
static_assert(sys.time_since_epoch().count() == 1024, "");
|
||||
|
||||
CONSTCD14 const_days c {clock_cast<const_clock>(sys)};
|
||||
CONSTCD14 sys_days sys2 {clock_cast<system_clock>(c)};
|
||||
CONSTCD14 sys_days sys3 { clock_cast<system_clock>(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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user