Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the time parser. #2770

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/common/datatypes/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

namespace nebula {

static inline std::string decimal(const std::string& number) {
auto find = std::find(number.begin(), number.end(), '.');
return std::string(find, number.end());
}

const int64_t kDaysSoFar[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
const int64_t kLeapDaysSoFar[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};

Expand Down Expand Up @@ -99,22 +104,26 @@ std::string Date::toString() const {
}

std::string Time::toString() const {
auto microsecStr = folly::stringPrintf("%.9f", static_cast<uint32_t>(microsec) / 1000000.0);
auto decimalPart = decimal(microsecStr);
// It's in current timezone already
return folly::stringPrintf("%02d:%02d:%02d.%06d", hour, minute, sec, microsec);
return folly::stringPrintf("%02d:%02d:%02d%s", hour, minute, sec, decimalPart.c_str());
}

std::string DateTime::toString() const {
auto microsecStr = folly::stringPrintf("%.9f", static_cast<uint32_t>(microsec) / 1000000.0);
auto decimalPart = decimal(microsecStr);
// It's in current timezone already
return folly::stringPrintf(
"%hd-%02hhu-%02hhu"
"T%02hhu:%02hhu:%02hhu.%u",
"T%02hhu:%02hhu:%02hhu%s",
static_cast<int16_t>(year),
static_cast<uint8_t>(month),
static_cast<uint8_t>(day),
static_cast<uint8_t>(hour),
static_cast<uint8_t>(minute),
static_cast<uint8_t>(sec),
static_cast<uint32_t>(microsec));
decimalPart.c_str());
}

} // namespace nebula
Expand Down
15 changes: 9 additions & 6 deletions src/common/function/test/FunctionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ class FunctionManagerTest : public ::testing::Test {
}
auto res = result.value()(argsRef);
if (res.type() != expect.type()) {
return ::testing::AssertionFailure() << "function return type check failed: " << expr;
return ::testing::AssertionFailure() << "function return type check failed, expect "
<< expect.type() << ", got " << res.type();
}
if (res != expect) {
return ::testing::AssertionFailure() << "function return value check failed: " << expr;
return ::testing::AssertionFailure()
<< "function return value check failed, expect " << expect << ", got " << res;
}
return ::testing::AssertionSuccess();
}
Expand Down Expand Up @@ -309,7 +311,7 @@ TEST_F(FunctionManagerTest, functionCall) {
TEST_FUNCTION(toString, args_["toString_bool"], "true");
TEST_FUNCTION(toString, args_["string"], "AbcDeFG");
TEST_FUNCTION(toString, args_["date"], "1984-10-11");
TEST_FUNCTION(toString, args_["datetime"], "1984-10-11T12:31:14.341");
TEST_FUNCTION(toString, args_["datetime"], "1984-10-11T12:31:14.000341000");
TEST_FUNCTION(toString, args_["nullvalue"], Value::kNullValue);
}
{
Expand All @@ -318,8 +320,9 @@ TEST_F(FunctionManagerTest, functionCall) {
DateTime dateTime(2021, 10, 31, 8, 5, 34, 29);
TEST_FUNCTION(concat, std::vector<Value>({"hello", 1, "world"}), "hello1world");
TEST_FUNCTION(concat, std::vector<Value>({true, 2, date}), "true22021-10-31");
TEST_FUNCTION(concat, std::vector<Value>({true, dateTime}), "true2021-10-31T08:05:34.29");
TEST_FUNCTION(concat, std::vector<Value>({2.3, time}), "2.309:39:21.000012");
TEST_FUNCTION(
concat, std::vector<Value>({true, dateTime}), "true2021-10-31T08:05:34.000029000");
TEST_FUNCTION(concat, std::vector<Value>({2.3, time}), "2.309:39:21.000012000");
TEST_FUNCTION(concat, args_["two"], "24");
TEST_FUNCTION(concat_ws, std::vector<Value>({",", 1}), "1");
TEST_FUNCTION(concat_ws, std::vector<Value>({"@", 1, "world"}), "1@world");
Expand All @@ -328,7 +331,7 @@ TEST_F(FunctionManagerTest, functionCall) {
"1ABtrueABworld");
TEST_FUNCTION(concat_ws,
std::vector<Value>({".", 1, true, Value::kNullValue, "world", time}),
"1.true.world.09:39:21.000012");
"1.true.world.09:39:21.000012000");
}
{
TEST_FUNCTION(toBoolean, args_["int"], Value::kNullBadType);
Expand Down
1 change: 1 addition & 0 deletions src/common/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ nebula_add_library(
TimeUtils.cpp
TimezoneInfo.cpp
TimeConversion.cpp
TimeParser.cpp
)

nebula_add_subdirectory(test)
Loading