xlnt/tests/utils/datetime_test_suite.cpp

125 lines
4.6 KiB
C++
Raw Normal View History

2018-01-22 22:38:48 +08:00
// Copyright (c) 2014-2018 Thomas Fussell
2017-04-14 07:01:30 +08: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, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
2014-06-06 04:19:31 +08:00
#include <iostream>
2017-04-14 07:24:20 +08:00
#include <helpers/test_suite.hpp>
2018-07-05 17:26:29 +08:00
#include <xlnt/utils/date.hpp>
#include <xlnt/utils/datetime.hpp>
#include <xlnt/utils/time.hpp>
2014-06-06 04:19:31 +08:00
2017-04-14 07:01:30 +08:00
class datetime_test_suite : public test_suite
2014-06-06 04:19:31 +08:00
{
public:
2017-04-14 07:24:20 +08:00
datetime_test_suite()
{
register_test(test_from_string);
register_test(test_to_string);
register_test(test_carry);
register_test(test_leap_year_bug);
register_test(test_early_date);
register_test(test_mac_calendar);
register_test(test_operators);
register_test(test_weekday);
2017-04-14 07:24:20 +08:00
}
2016-07-09 22:37:12 +08:00
void test_from_string()
2014-06-06 04:19:31 +08:00
{
2016-07-09 22:37:12 +08:00
xlnt::time t("10:35:45");
xlnt_assert_equals(t.hour, 10);
xlnt_assert_equals(t.minute, 35);
xlnt_assert_equals(t.second, 45);
2016-07-09 22:37:12 +08:00
}
2016-07-17 03:57:50 +08:00
void test_to_string()
{
xlnt::datetime dt(2016, 7, 16, 9, 11, 32, 999999);
xlnt_assert_equals(dt.to_string(), "2016/7/16 9:11:32:999999");
2016-07-17 03:57:50 +08:00
}
2016-07-09 22:37:12 +08:00
void test_carry()
{
// We want a time that rolls over to the next second, minute, and hour
// Start off with a time 1 microsecond before the next hour
xlnt::datetime dt(2016, 7, 9, 10, 59, 59, 999999);
auto number = dt.to_number(xlnt::calendar::windows_1900);
// Add 600 nanoseconds to the raw number which represents time as a fraction of a day
// In other words, 6 tenths of a millionth of a sixtieth of a sixtieth of a twenty-fourth of a day
number += (0.6 / 1000000) / 60 / 60 / 24;
auto rollover = xlnt::datetime::from_number(number, xlnt::calendar::windows_1900);
xlnt_assert_equals(rollover.hour, 11);
xlnt_assert_equals(rollover.minute, 00);
xlnt_assert_equals(rollover.second, 00);
xlnt_assert_equals(rollover.microsecond, 00);
2014-06-06 04:19:31 +08:00
}
2016-07-17 07:40:20 +08:00
void test_leap_year_bug()
{
xlnt::datetime dt(1900, 2, 29, 0, 0, 0, 0);
auto number = dt.to_number(xlnt::calendar::windows_1900);
xlnt_assert_equals(static_cast<int>(number), 60);
2016-07-17 07:40:20 +08:00
auto converted = xlnt::datetime::from_number(number, xlnt::calendar::windows_1900);
xlnt_assert_equals(dt, converted);
2016-07-17 07:40:20 +08:00
}
void test_early_date()
{
xlnt::date d(1900, 1, 29);
auto number = d.to_number(xlnt::calendar::windows_1900);
xlnt_assert_equals(number, 29);
2016-07-17 07:40:20 +08:00
auto converted = xlnt::date::from_number(number, xlnt::calendar::windows_1900);
xlnt_assert_equals(d, converted);
2016-07-17 07:40:20 +08:00
}
void test_mac_calendar()
{
xlnt::date d(2016, 7, 16);
auto number_1900 = d.to_number(xlnt::calendar::windows_1900);
auto number_1904 = d.to_number(xlnt::calendar::mac_1904);
xlnt_assert_equals(number_1900, 42567);
xlnt_assert_equals(number_1904, 41105);
2016-07-17 07:40:20 +08:00
auto converted_1900 = xlnt::date::from_number(number_1900, xlnt::calendar::windows_1900);
auto converted_1904 = xlnt::date::from_number(number_1904, xlnt::calendar::mac_1904);
xlnt_assert_equals(converted_1900, d);
xlnt_assert_equals(converted_1904, d);
2016-07-17 07:40:20 +08:00
}
void test_operators()
{
xlnt::date d1(2016, 7, 16);
xlnt::date d2(2016, 7, 16);
xlnt::date d3(2016, 7, 15);
xlnt_assert_equals(d1, d2);
xlnt_assert_differs(d1, d3);
2016-07-17 07:40:20 +08:00
}
void test_weekday()
{
xlnt_assert_equals(xlnt::date(2000, 1, 1).weekday(), 6);
xlnt_assert_equals(xlnt::date(2016, 7, 15).weekday(), 5);
xlnt_assert_equals(xlnt::date(2018, 10, 29).weekday(), 1);
}
2014-06-06 04:19:31 +08:00
};
static datetime_test_suite x;