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 all 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
35 changes: 20 additions & 15 deletions src/common/datatypes/test/ValueToJsonTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ TEST(ValueToJson, list) {
Time(13, 30, 15, 0), // time
DateTime(2021, 12, 21, 13, 30, 15, 0)})); // datetime
dynamic expectedListJsonObj = dynamic::array(
2, 2.33, true, "str", "2021-12-21", "13:30:15.000000Z", "2021-12-21T13:30:15.0Z");
2, 2.33, true, "str", "2021-12-21", "13:30:15.000000000Z", "2021-12-21T13:30:15.000000000Z");
ASSERT_EQ(expectedListJsonObj, list1.toJson());

dynamic expectedListMetaObj = dynamic::array(nullptr,
Expand All @@ -159,7 +159,7 @@ TEST(ValueToJson, Set) {
Time(13, 30, 15, 0), // time
DateTime(2021, 12, 21, 13, 30, 15, 0)})); // datetime
dynamic expectedSetJsonObj = dynamic::array(
2, 2.33, true, "str", "2021-12-21", "13:30:15.000000Z", "2021-12-21T13:30:15.0Z");
2, 2.33, true, "str", "2021-12-21", "13:30:15.000000000Z", "2021-12-21T13:30:15.000000000Z");
// The underlying data strcuture is unordered_set, so sort before the comparison
auto actualJson = set.toJson();
std::sort(actualJson.begin(), actualJson.end());
Expand All @@ -180,7 +180,7 @@ TEST(ValueToJson, map) {
{"key7", DateTime(2021, 12, 21, 13, 30, 15, 0)}})); // datetime
dynamic expectedMapJsonObj =
dynamic::object("key1", 2)("key2", 2.33)("key3", true)("key4", "str")("key5", "2021-12-21")(
"key6", "13:30:15.000000Z")("key7", "2021-12-21T13:30:15.0Z");
"key6", "13:30:15.000000000Z")("key7", "2021-12-21T13:30:15.000000000Z");
ASSERT_EQ(expectedMapJsonObj, map.toJson());
// Skip meta json comparison since nested dynamic objects cannot be sorted. i.g. dynamic::object
// inside dynamic::array
Expand All @@ -195,18 +195,23 @@ TEST(ValueToJson, dataset) {
Date(2021, 12, 21), // date
Time(13, 30, 15, 0), // time
DateTime(2021, 12, 21, 13, 30, 15, 0)}));
dynamic expectedDatasetJsonObj = dynamic::array(dynamic::object(
"row",
dynamic::array(
2, 2.33, true, "str", "2021-12-21", "13:30:15.000000Z", "2021-12-21T13:30:15.0Z"))(
"meta",
dynamic::array(nullptr,
nullptr,
nullptr,
nullptr,
dynamic::object("type", "date"),
dynamic::object("type", "time"),
dynamic::object("type", "datetime"))));
dynamic expectedDatasetJsonObj =
dynamic::array(dynamic::object("row",
dynamic::array(2,
2.33,
true,
"str",
"2021-12-21",
"13:30:15.000000000Z",
"2021-12-21T13:30:15.000000000Z"))(
"meta",
dynamic::array(nullptr,
nullptr,
nullptr,
nullptr,
dynamic::object("type", "date"),
dynamic::object("type", "time"),
dynamic::object("type", "datetime"))));
ASSERT_EQ(expectedDatasetJsonObj, dataset.toJson());
}

Expand Down
9 changes: 5 additions & 4 deletions src/common/function/test/FunctionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,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 @@ -320,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 @@ -330,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