Datetime增加hex()返回兼容oracle的Datetime格式存储

This commit is contained in:
fasiondog 2021-04-25 23:59:14 +08:00
parent 6f91ab960c
commit 437b33327d
3 changed files with 29 additions and 3 deletions

View File

@ -115,7 +115,7 @@ std::string Datetime::repr() const {
minute(), second(), millisecond(), microsecond());
}
unsigned long long Datetime::number() const noexcept {
uint64_t Datetime::number() const noexcept {
try {
HKU_IF_RETURN(m_data.date() == bd::date(bd::pos_infin), Null<unsigned long long>());
return (unsigned long long)year() * 100000000LL + (unsigned long long)month() * 1000000LL +
@ -129,6 +129,20 @@ unsigned long long Datetime::number() const noexcept {
}
}
uint64_t Datetime::hex() const noexcept {
uint64_t ret = uint64_t(second());
ret |= (uint64_t(minute()) << 8);
ret |= (uint64_t(hour()) << 16);
ret |= (uint64_t(day()) << 24);
ret |= (uint64_t(month()) << 32);
uint64_t y = uint64_t(year());
uint64_t high_y = y / 100;
uint64_t low_y = y - high_y * 100;
ret |= (low_y << 40);
ret |= (high_y << 48);
return ret;
}
long Datetime::year() const {
HKU_CHECK_THROW(!isNull(), std::logic_error, "This is Null Datetime!");
return m_data.date().year();

View File

@ -121,10 +121,17 @@ public:
Datetime operator-(TimeDelta d) const;
/**
* YYYYMMDDhhmm格式的数字便
* YYYYMMDDhhmm格式的数字便
* Null<Datetime>() number Null<unsigned long long>
* @note
*/
unsigned long long number() const noexcept;
uint64_t number() const noexcept;
/**
* oracle datetime 7
* @note
*/
uint64_t hex() const noexcept;
/**
*

View File

@ -9,6 +9,7 @@
#include <hikyuu/datetime/Datetime.h>
#include <hikyuu/utilities/Null.h>
#include <hikyuu/Log.h>
using namespace hku;
@ -121,6 +122,10 @@ TEST_CASE("test_Datetime") {
Datetime m(y);
CHECK(m == d);
/** @arg 兼容oracle datetime表示法 */
d = Datetime(2021, 4, 25, 23, 16, 27);
CHECK_EQ(d.hex(), 0x1415041917101bULL);
/** @arg Null<Datetime>()转化为number */
x = Null<unsigned long long>();
d = Null<Datetime>();