82 lines
2.8 KiB
C++
82 lines
2.8 KiB
C++
#include <BoostLog.h>
|
|
#include <DateTime.h>
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
BOOST_AUTO_TEST_SUITE(DateTimeTest, *boost::unit_test::enabled())
|
|
|
|
BOOST_AUTO_TEST_CASE(BasicUse) {
|
|
std::time_t c_current = std::time(nullptr);
|
|
auto c_local = localtime(&c_current);
|
|
|
|
auto now = DateTime::currentDateTime();
|
|
BOOST_CHECK_EQUAL(now.year(), c_local->tm_year + 1900);
|
|
BOOST_CHECK_EQUAL(now.month(), c_local->tm_mon + 1);
|
|
BOOST_CHECK_EQUAL(now.day(), c_local->tm_mday);
|
|
BOOST_CHECK_EQUAL(now.hour(), c_local->tm_hour);
|
|
BOOST_CHECK_EQUAL(now.minute(), c_local->tm_min);
|
|
BOOST_CHECK_EQUAL(now.second(), c_local->tm_sec);
|
|
|
|
// std::cout << "now: " << DateTime::toString(std::chrono::system_clock::now()) << std::endl;
|
|
// std::cout << "now: " << DateTime::currentDateTime().toString() << std::endl;
|
|
|
|
auto day = DateTime::makeDateTime(2018, 8, 15, 13, 14, 59);
|
|
DateTime r(day);
|
|
BOOST_CHECK_EQUAL(r.week(), DateTime::Wednesday);
|
|
BOOST_CHECK_EQUAL(DateTime::toString(day), "2018-08-15 13:14:59");
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(IsLeapYearTest) {
|
|
BOOST_TEST(DateTime::isLeapYear(2008));
|
|
BOOST_TEST(!DateTime::isLeapYear(2100));
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(TomorrowTest) {
|
|
DateTime day(2018, 8, 15, 13, 14, 59);
|
|
auto t = day.tomorrow();
|
|
BOOST_CHECK_EQUAL(t.toString(), "2018-08-16 13:14:59");
|
|
|
|
day = DateTime(2018, 12, 31, 13, 14, 59);
|
|
t = day.tomorrow();
|
|
BOOST_CHECK_EQUAL(t.toString(), "2019-01-01 13:14:59");
|
|
|
|
day = DateTime(2020, 2, 28, 13, 14, 59);
|
|
t = day.tomorrow();
|
|
BOOST_CHECK_EQUAL(t.toString(), "2020-02-29 13:14:59");
|
|
|
|
day = DateTime(2021, 2, 28, 13, 14, 59);
|
|
t = day.tomorrow();
|
|
BOOST_CHECK_EQUAL(t.toString(), "2021-03-01 13:14:59");
|
|
|
|
day = DateTime(2021, 10, 25, 22, 31, 59);
|
|
t = day.tomorrow();
|
|
BOOST_CHECK_EQUAL(t.toString(), "2021-10-26 22:31:59");
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(CurrentMSecsSinceEpoch) {
|
|
auto time1 = DateTime::currentMSecsSinceEpoch();
|
|
auto time2 = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
|
|
BOOST_TEST(std::abs(time1 - time2) < 5);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(ParseTimeString) {
|
|
auto [hour, minute, second] = DateTime::parseTime("12:14:15");
|
|
BOOST_CHECK_EQUAL(hour, 12);
|
|
BOOST_CHECK_EQUAL(minute, 14);
|
|
BOOST_CHECK_EQUAL(second, 15);
|
|
|
|
std::tie(hour, minute, second) = DateTime::parseTime("15:12");
|
|
BOOST_CHECK_EQUAL(hour, 15);
|
|
BOOST_CHECK_EQUAL(minute, 12);
|
|
BOOST_CHECK_EQUAL(second, 0);
|
|
|
|
std::tie(hour, minute, second) = DateTime::parseTime("00:09:07");
|
|
BOOST_CHECK_EQUAL(hour, 0);
|
|
BOOST_CHECK_EQUAL(minute, 9);
|
|
BOOST_CHECK_EQUAL(second, 7);
|
|
|
|
std::tie(hour, minute, second) = DateTime::parseTime("15:12");
|
|
BOOST_CHECK_EQUAL(hour, 15);
|
|
BOOST_CHECK_EQUAL(minute, 12);
|
|
BOOST_CHECK_EQUAL(second, 0);
|
|
}
|
|
BOOST_AUTO_TEST_SUITE_END() |