summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/mysql_types.cpp26
-rw-r--r--lib/mysql_types.h2
-rw-r--r--test/test-misc.cpp22
3 files changed, 48 insertions, 2 deletions
diff --git a/lib/mysql_types.cpp b/lib/mysql_types.cpp
index 8b2208e..40614ef 100644
--- a/lib/mysql_types.cpp
+++ b/lib/mysql_types.cpp
@@ -3,10 +3,21 @@
#include "rawDataReader.h"
#include <algorithm>
#include <array>
+#include <byteswap.h>
#include <stdexcept>
#include <string>
namespace MyGrate::MySQL {
+#define NTOH(B) \
+ inline auto ntoh(uint##B##_t x) \
+ { \
+ if constexpr (__BYTE_ORDER == __LITTLE_ENDIAN) \
+ x = bswap_##B(x); \
+ return x; \
+ }
+ NTOH(32);
+ NTOH(64);
+
typename Type<MYSQL_TYPE_NULL, false>::C
Type<MYSQL_TYPE_NULL, false>::read(RawDataReader &, RawDataReader &)
{
@@ -153,10 +164,21 @@ namespace MyGrate::MySQL {
return t;
}
+ Time
+ time2From24bit(uint32_t tint)
+ {
+ Time t {};
+ t.second = bitslice<6>(tint, 8);
+ t.minute = bitslice<6>(tint, 14);
+ t.hour = bitslice<5>(tint, 20);
+ return t;
+ }
+
typename Type<MYSQL_TYPE_TIME2>::C
- Type<MYSQL_TYPE_TIME2>::read(RawDataReader &, RawDataReader &)
+ Type<MYSQL_TYPE_TIME2>::read(RawDataReader & md, RawDataReader & data)
{
- throw std::logic_error("Not implemented");
+ md.discard(1);
+ return time2From24bit(ntoh(data.readValue<uint32_t>(3)));
}
typename Type<MYSQL_TYPE_DATE>::C
diff --git a/lib/mysql_types.h b/lib/mysql_types.h
index d32f6a8..d49cd2f 100644
--- a/lib/mysql_types.h
+++ b/lib/mysql_types.h
@@ -84,6 +84,8 @@ namespace MyGrate {
#undef DEFINE_USTYPE
#undef DEFINE_TYPE
+ Time time2From24bit(uint32_t tint);
+
struct ReplicationPosition {
#ifndef __cpp_aggregate_paren_init
ReplicationPosition(std::string f, uint64_t p) : filename {std::move(f)}, position {p} { }
diff --git a/test/test-misc.cpp b/test/test-misc.cpp
index c72f3a6..6452050 100644
--- a/test/test-misc.cpp
+++ b/test/test-misc.cpp
@@ -1,11 +1,14 @@
#define BOOST_TEST_MODULE BitSet
+#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include <cstddef>
#include <cstdint>
#include <dbTypes.h>
#include <helpers.h>
+#include <mysql_types.h>
#include <stdexcept>
+#include <streamSupport.h>
#include <string>
#include <string_view>
#include <tuple>
@@ -112,3 +115,22 @@ BOOST_AUTO_TEST_CASE(mod100_extract)
BOOST_CHECK_EQUAL((int)MyGrate::mod100_extract(i), 0);
BOOST_CHECK_EQUAL((int)MyGrate::mod100_extract(i), 0);
}
+
+using ConvertTimeData = std::tuple<uint32_t, MyGrate::Time>;
+BOOST_DATA_TEST_CASE(convert_time,
+ boost::unit_test::data::make<ConvertTimeData>({
+ {0x8128cc00, {18, 35, 12}},
+ {0x81537500, {21, 13, 53}},
+ {0x817d2200, {23, 52, 34}},
+ {0x8027cf00, {2, 31, 15}},
+ {0x80527800, {5, 9, 56}},
+ {0x807c2500, {7, 48, 37}},
+ {0x80a6d200, {10, 27, 18}},
+ {0x80d17b00, {13, 5, 59}},
+ {0x80fb2800, {15, 44, 40}},
+ {0x8125d500, {18, 23, 21}},
+ }),
+ tint, time)
+{
+ BOOST_CHECK_EQUAL(MyGrate::MySQL::time2From24bit(tint), time);
+}