date/test/clock_cast_test/local_t.pass.cpp

133 lines
4.0 KiB
C++
Raw Normal View History

// The MIT License (MIT)
//
// Copyright (c) 2018 Tomasz Kamiński
//
// 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.
2018-06-10 19:01:30 +02:00
#include <chrono>
#include "date/tz.h"
int
main()
{
using namespace date;
using namespace std::chrono;
2018-06-10 23:38:21 +02:00
// self
{
auto ls = local_days{1970_y/January/1_d};
2018-06-10 23:38:21 +02:00
assert(clock_cast<local_t>(ls) == ls);
}
2018-06-11 15:14:44 -04:00
/// sys epoch
2018-06-10 19:01:30 +02:00
{
auto ls = local_days{1970_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto st = clock_cast<system_clock>(ls);
assert(clock_cast<local_t>(st) == ls);
2018-06-10 23:03:54 +02:00
assert(st.time_since_epoch() == seconds(0));
2018-06-10 19:01:30 +02:00
}
2018-06-11 15:14:44 -04:00
/// sys 2000 case
2018-06-10 19:01:30 +02:00
{
auto ls = local_days{2000_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto st = clock_cast<system_clock>(ls);
assert(clock_cast<local_t>(st) == ls);
2018-06-10 23:03:54 +02:00
assert(st.time_since_epoch() == seconds(946684800));
2018-06-10 19:01:30 +02:00
}
2018-06-11 15:14:44 -04:00
/// utc epoch
2018-06-10 19:01:30 +02:00
{
auto lu = local_days{1970_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto ut = clock_cast<utc_clock>(lu);
assert(clock_cast<local_t>(ut) == lu);
2018-06-10 23:03:54 +02:00
assert(ut.time_since_epoch() == seconds(0));
2018-06-10 19:01:30 +02:00
}
// utc leap second
{
auto lu = local_days{2015_y/July/1_d} - milliseconds(1);
auto ut = clock_cast<utc_clock>(lu) + milliseconds(50); //into leap second
assert(clock_cast<local_t>(ut) == lu);
}
2018-06-11 15:14:44 -04:00
/// utc paper example
2018-06-10 19:01:30 +02:00
{
auto lu = local_days{2000_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto ut = clock_cast<utc_clock>(lu);
assert(clock_cast<local_t>(ut) == lu);
2018-06-10 23:03:54 +02:00
assert(ut.time_since_epoch() == seconds(946684822));
2018-06-10 19:01:30 +02:00
}
/// tai epoch
{
auto lt = local_days{1958_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto tt = clock_cast<tai_clock>(lt);
assert(clock_cast<local_t>(tt) == lt);
2018-06-10 23:03:54 +02:00
assert(tt.time_since_epoch() == seconds(0));
2018-06-10 19:01:30 +02:00
auto lu = local_days{1958_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto ut = clock_cast<utc_clock>(lu);
assert(clock_cast<tai_clock>(ut) == tt);
}
// tai paper example
{
auto lt = local_days{2000_y/January/1_d} + seconds(32);
2018-06-10 19:01:30 +02:00
auto tt = clock_cast<tai_clock>(lt);
assert(clock_cast<local_t>(tt) == lt);
auto lu = local_days{2000_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto ut = clock_cast<utc_clock>(lu);
assert(clock_cast<tai_clock>(ut) == tt);
}
/// gps epoch
{
auto lg = local_days{1980_y/January/Sunday[1]};
2018-06-10 19:01:30 +02:00
auto gt = clock_cast<gps_clock>(lg);
assert(clock_cast<local_t>(gt) == lg);
2018-06-10 23:03:54 +02:00
assert(gt.time_since_epoch() == seconds(0));
2018-06-10 19:01:30 +02:00
auto lu = local_days{1980_y/January/Sunday[1]};
2018-06-10 19:01:30 +02:00
auto ut = clock_cast<utc_clock>(lu);
assert(clock_cast<gps_clock>(ut) == gt);
auto lt = local_days{1980_y/January/Sunday[1]} + seconds(19);
2018-06-10 19:01:30 +02:00
auto tt = clock_cast<tai_clock>(lt);
assert(clock_cast<gps_clock>(tt) == gt);
}
// gps 2000 example
{
auto lg = local_days{2000_y/January/1_d};
2018-06-10 19:01:30 +02:00
auto gt = clock_cast<gps_clock>(lg);
assert(clock_cast<local_t>(gt) == lg);
auto lu = local_days{2000_y/January/1_d} - seconds(13);
2018-06-10 19:01:30 +02:00
auto ut = clock_cast<utc_clock>(lu);
assert(clock_cast<gps_clock>(ut) == gt);
auto lt = local_days{2000_y/January/1_d} + seconds(19);
2018-06-10 19:01:30 +02:00
auto tt = clock_cast<tai_clock>(lt);
assert(clock_cast<gps_clock>(tt) == gt);
}
}