2017-11-19 18:47:18 +01:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
2018-06-23 16:39:00 +02:00
|
|
|
// Copyright (c) 2017, 2018 Tomasz Kamiński
|
2017-11-19 18:47:18 +01:00
|
|
|
//
|
|
|
|
// 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>
|
|
|
|
|
|
|
|
//used to count number of conversion
|
|
|
|
int conversions = 0;
|
|
|
|
|
|
|
|
//to/from impl
|
|
|
|
struct mil_clock
|
|
|
|
{
|
2018-05-06 19:13:36 +02:00
|
|
|
using duration = typename std::common_type<std::chrono::system_clock::duration, date::days>::type;
|
2017-11-19 18:47:18 +01:00
|
|
|
using rep = duration::rep;
|
|
|
|
using period = duration::period;
|
|
|
|
using time_point = std::chrono::time_point<mil_clock, duration>;
|
|
|
|
|
2018-05-06 19:13:36 +02:00
|
|
|
static constexpr date::sys_days epoch{date::days{1000}};
|
2017-11-19 18:47:18 +01:00
|
|
|
|
|
|
|
template<typename Duration>
|
|
|
|
static
|
2018-05-06 19:13:36 +02:00
|
|
|
std::chrono::time_point<std::chrono::system_clock, typename std::common_type<Duration, date::days>::type>
|
2017-11-19 18:47:18 +01:00
|
|
|
to_sys(std::chrono::time_point<mil_clock, Duration> const& tp)
|
|
|
|
{
|
|
|
|
++conversions;
|
|
|
|
return epoch + tp.time_since_epoch();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Duration>
|
|
|
|
static
|
2018-05-06 19:13:36 +02:00
|
|
|
std::chrono::time_point<mil_clock, typename std::common_type<Duration, date::days>::type>
|
2017-11-19 18:47:18 +01:00
|
|
|
from_sys(std::chrono::time_point<std::chrono::system_clock, Duration> const& tp)
|
|
|
|
{
|
|
|
|
++conversions;
|
2018-05-06 19:13:36 +02:00
|
|
|
using res = std::chrono::time_point<mil_clock, typename std::common_type<Duration, date::days>::type>;
|
2017-11-19 18:47:18 +01:00
|
|
|
return res(tp - epoch);
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:38:21 +02:00
|
|
|
template<typename Duration>
|
|
|
|
static
|
|
|
|
std::chrono::time_point<date::local_t, typename std::common_type<Duration, date::days>::type>
|
|
|
|
to_local(std::chrono::time_point<mil_clock, Duration> const& tp)
|
|
|
|
{
|
|
|
|
return date::clock_cast<date::local_t>(to_sys(tp));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Duration>
|
|
|
|
static
|
|
|
|
std::chrono::time_point<mil_clock, typename std::common_type<Duration, date::days>::type>
|
|
|
|
from_local(std::chrono::time_point<date::local_t, Duration> const& tp)
|
|
|
|
{
|
|
|
|
return from_sys(date::clock_cast<std::chrono::system_clock>(tp));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-19 18:47:18 +01:00
|
|
|
static time_point now()
|
|
|
|
{
|
|
|
|
return from_sys(std::chrono::system_clock::now());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
date::sys_days const mil_clock::epoch;
|
|
|
|
|
|
|
|
// traits example
|
2017-11-25 22:14:51 +01:00
|
|
|
struct s2s_clock
|
2017-11-19 18:47:18 +01:00
|
|
|
{
|
|
|
|
using duration = std::chrono::system_clock::duration;
|
|
|
|
using rep = duration::rep;
|
|
|
|
using period = duration::period;
|
|
|
|
using time_point = std::chrono::time_point<s2s_clock, duration>;
|
|
|
|
|
|
|
|
template<typename Duration>
|
|
|
|
static
|
2017-11-25 22:14:51 +01:00
|
|
|
std::chrono::time_point<std::chrono::system_clock, Duration>
|
2017-11-19 18:47:18 +01:00
|
|
|
to_sys(std::chrono::time_point<s2s_clock, Duration> const& tp)
|
|
|
|
{
|
|
|
|
++conversions;
|
|
|
|
return std::chrono::time_point<std::chrono::system_clock, Duration>(tp.time_since_epoch());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Duration>
|
|
|
|
static
|
2017-11-25 22:14:51 +01:00
|
|
|
std::chrono::time_point<s2s_clock, Duration>
|
2017-11-19 18:47:18 +01:00
|
|
|
from_sys(std::chrono::time_point<std::chrono::system_clock, Duration> const& tp)
|
|
|
|
{
|
|
|
|
++conversions;
|
|
|
|
return std::chrono::time_point<s2s_clock, Duration>(tp.time_since_epoch());
|
|
|
|
}
|
|
|
|
|
|
|
|
static time_point now()
|
|
|
|
{
|
|
|
|
return from_sys(std::chrono::system_clock::now());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace date
|
|
|
|
{
|
|
|
|
template<>
|
2017-11-25 22:14:51 +01:00
|
|
|
struct clock_time_conversion<mil_clock, s2s_clock>
|
2017-11-19 18:47:18 +01:00
|
|
|
{
|
|
|
|
template<typename Duration>
|
2018-05-06 19:13:36 +02:00
|
|
|
std::chrono::time_point<mil_clock, typename std::common_type<Duration, date::days>::type>
|
2017-11-19 18:47:18 +01:00
|
|
|
operator()(std::chrono::time_point<s2s_clock, Duration> const& tp)
|
|
|
|
{
|
|
|
|
++conversions;
|
2018-05-06 19:13:36 +02:00
|
|
|
using res = std::chrono::time_point<mil_clock, typename std::common_type<Duration, date::days>::type>;
|
2017-11-19 18:47:18 +01:00
|
|
|
return res(tp.time_since_epoch() - mil_clock::epoch.time_since_epoch());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
using namespace date;
|
|
|
|
using sys_clock = std::chrono::system_clock;
|
|
|
|
|
|
|
|
// self
|
|
|
|
{
|
|
|
|
sys_days st(1997_y/dec/12);
|
|
|
|
auto mt = mil_clock::from_sys(st);
|
2017-11-25 22:14:51 +01:00
|
|
|
|
2017-11-19 18:47:18 +01:00
|
|
|
assert(clock_cast<mil_clock>(mt) == mt);
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:38:21 +02:00
|
|
|
// mil <-> local
|
|
|
|
{
|
|
|
|
local_days lt(1997_y/dec/12);
|
|
|
|
auto mt = mil_clock::from_local(lt);
|
|
|
|
|
|
|
|
assert(clock_cast<mil_clock>(lt) == mt);
|
|
|
|
assert(clock_cast<local_t>(mt) == lt);
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:47:18 +01:00
|
|
|
// mil <-> sys
|
|
|
|
{
|
|
|
|
sys_days st(1997_y/dec/12);
|
|
|
|
auto mt = mil_clock::from_sys(st);
|
|
|
|
|
|
|
|
assert(clock_cast<mil_clock>(st) == mt);
|
2017-11-25 22:14:51 +01:00
|
|
|
assert(clock_cast<sys_clock>(mt) == st);
|
2017-11-19 18:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mil <-> utc
|
|
|
|
{
|
|
|
|
sys_days st(1997_y/dec/12);
|
|
|
|
auto mt = mil_clock::from_sys(st);
|
|
|
|
auto ut = utc_clock::from_sys(st);
|
|
|
|
|
|
|
|
assert(clock_cast<mil_clock>(ut) == mt);
|
2017-11-25 22:14:51 +01:00
|
|
|
assert(clock_cast<utc_clock>(mt) == ut);
|
2017-11-19 18:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mil <-> tai
|
|
|
|
{
|
|
|
|
sys_days st(1997_y/dec/12);
|
|
|
|
auto mt = mil_clock::from_sys(st);
|
|
|
|
auto ut = utc_clock::from_sys(st);
|
|
|
|
auto tt = tai_clock::from_utc(ut);
|
|
|
|
|
|
|
|
assert(clock_cast<tai_clock>(mt) == tt);
|
|
|
|
assert(clock_cast<mil_clock>(tt) == mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// mil <-> gps
|
|
|
|
{
|
|
|
|
sys_days st(1997_y/dec/12);
|
|
|
|
auto mt = mil_clock::from_sys(st);
|
|
|
|
auto ut = utc_clock::from_sys(st);
|
|
|
|
auto gt = gps_clock::from_utc(ut);
|
|
|
|
|
|
|
|
assert(clock_cast<gps_clock>(mt) == gt);
|
|
|
|
assert(clock_cast<mil_clock>(gt) == mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// s2s -> mil
|
|
|
|
{
|
|
|
|
sys_days st(1997_y/dec/12);
|
|
|
|
auto mt = mil_clock::from_sys(st);
|
|
|
|
auto s2t = s2s_clock::from_sys(st);
|
|
|
|
|
|
|
|
//direct trait conversion
|
|
|
|
conversions = 0;
|
|
|
|
assert(clock_cast<mil_clock>(s2t) == mt);
|
|
|
|
assert(conversions == 1);
|
|
|
|
|
2017-11-25 22:14:51 +01:00
|
|
|
//uses sys_clock
|
2017-11-19 18:47:18 +01:00
|
|
|
conversions = 0;
|
|
|
|
assert(clock_cast<s2s_clock>(mt) == s2t);
|
|
|
|
assert(conversions == 2);
|
|
|
|
}
|
|
|
|
}
|