rcpp_framework/tests/unittests/DateUnittest.cc

43 lines
1.5 KiB
C++
Raw Normal View History

#include "core/math/date.h"
2021-06-17 14:43:29 +02:00
#include <gtest/gtest.h>
#include <string>
#include <iostream>
2022-02-10 12:07:47 +01:00
2021-06-17 14:43:29 +02:00
TEST(Date, constructorTest)
{
EXPECT_STREQ("1985-01-01 00:00:00",
2022-02-10 12:07:47 +01:00
Date(1985, 1, 1)
2021-06-17 14:43:29 +02:00
.toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S")
.c_str());
EXPECT_STREQ("2004-02-29 00:00:00.000000",
2022-02-10 12:07:47 +01:00
Date(2004, 2, 29)
2021-06-17 14:43:29 +02:00
.toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true)
.c_str());
EXPECT_STRNE("2001-02-29 00:00:00.000000",
2022-02-10 12:07:47 +01:00
Date(2001, 2, 29)
2021-06-17 14:43:29 +02:00
.toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true)
.c_str());
EXPECT_STREQ("2018-01-01 00:00:00.000000",
2022-02-10 12:07:47 +01:00
Date(2018, 1, 1, 12, 12, 12, 2321)
2021-06-17 14:43:29 +02:00
.roundDay()
.toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true)
.c_str());
}
TEST(Date, DatabaseStringTest)
{
2022-02-10 12:07:47 +01:00
auto now = Date::now();
EXPECT_EQ(now, Date::fromDbStringLocal(now.toDbStringLocal()));
2021-06-17 14:43:29 +02:00
std::string dbString = "2018-01-01 00:00:00.123";
2022-02-10 12:07:47 +01:00
auto dbDate = Date::fromDbStringLocal(dbString);
2021-06-17 14:43:29 +02:00
auto ms = (dbDate.microSecondsSinceEpoch() % 1000000) / 1000;
EXPECT_EQ(ms, 123);
dbString = "2018-01-01 00:00:00";
2022-02-10 12:07:47 +01:00
dbDate = Date::fromDbStringLocal(dbString);
2021-06-17 14:43:29 +02:00
ms = (dbDate.microSecondsSinceEpoch() % 1000000) / 1000;
EXPECT_EQ(ms, 0);
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}