summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-08-27 19:30:58 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-08-27 19:30:58 +0100
commit32ebcca4a5fb4a165739b305efbb17f7b2508412 (patch)
tree8026be399c57f26294730e61ae0f5657cfa85442
parentDon't declare clang 12 (diff)
downloadmygrate-32ebcca4a5fb4a165739b305efbb17f7b2508412.tar.bz2
mygrate-32ebcca4a5fb4a165739b305efbb17f7b2508412.tar.xz
mygrate-32ebcca4a5fb4a165739b305efbb17f7b2508412.zip
Make date/time types easy to create and compare
-rw-r--r--lib/dbTypes.h7
-rw-r--r--test/test-misc.cpp11
2 files changed, 18 insertions, 0 deletions
diff --git a/lib/dbTypes.h b/lib/dbTypes.h
index e17315b..a9ed029 100644
--- a/lib/dbTypes.h
+++ b/lib/dbTypes.h
@@ -51,6 +51,7 @@ namespace MyGrate {
boost::numeric_cast<uint8_t>(tm.tm_mday))
{
}
+ bool operator<=>(const Date &) const = default;
uint16_t year;
uint8_t month;
uint8_t day;
@@ -63,6 +64,7 @@ namespace MyGrate {
boost::numeric_cast<uint8_t>(tm.tm_sec))
{
}
+ bool operator<=>(const Time &) const = default;
uint8_t hour;
uint8_t minute;
uint8_t second;
@@ -70,7 +72,12 @@ namespace MyGrate {
struct DateTime : public Date, public Time {
inline DateTime() { }
inline DateTime(const Date & d, const Time & t) : Date {d}, Time {t} { }
+ inline DateTime(uint16_t y, uint8_t m, uint8_t d, uint8_t H, uint8_t M, uint8_t S) :
+ DateTime {{y, m, d}, {H, M, S}}
+ {
+ }
explicit inline DateTime(const tm & tm) : Date {tm}, Time {tm} { }
+ bool operator<=>(const DateTime &) const = default;
};
using Blob = std::span<const std::byte>;
diff --git a/test/test-misc.cpp b/test/test-misc.cpp
index 3f45927..515a9c7 100644
--- a/test/test-misc.cpp
+++ b/test/test-misc.cpp
@@ -88,3 +88,14 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(DbValueConvFloatToString, F, Floats)
MyGrate::DbValue v {F {123}};
BOOST_CHECK_EQUAL((std::string)v, "123.000000"s);
}
+
+BOOST_AUTO_TEST_CASE(create_datetime)
+{
+ struct tm tm;
+ time_t t {1629222289};
+ gmtime_r(&t, &tm);
+ BOOST_REQUIRE_EQUAL(tm.tm_gmtoff, 0);
+ BOOST_REQUIRE_EQUAL(tm.tm_isdst, 0);
+ MyGrate::DateTime dt {tm};
+ BOOST_CHECK_EQUAL(dt, (MyGrate::DateTime {2021, 8, 17, 17, 44, 49}));
+}