Skip to content

Commit

Permalink
datetime2timestamp and timestamp2datetime (#4526)
Browse files Browse the repository at this point in the history
  • Loading branch information
caton-hpg authored Aug 16, 2022
1 parent 41b9f8b commit 0f75120
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@ std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typ
{"datetime",
{TypeSignature({}, Value::Type::DATETIME),
TypeSignature({Value::Type::STRING}, Value::Type::DATETIME),
TypeSignature({Value::Type::MAP}, Value::Type::DATETIME)}},
TypeSignature({Value::Type::MAP}, Value::Type::DATETIME),
TypeSignature({Value::Type::INT}, Value::Type::DATETIME)}},
{"timestamp",
{TypeSignature({}, Value::Type::INT),
TypeSignature({Value::Type::STRING}, Value::Type::INT),
TypeSignature({Value::Type::INT}, Value::Type::INT)}},
TypeSignature({Value::Type::INT}, Value::Type::INT),
TypeSignature({Value::Type::DATETIME}, Value::Type::INT)}},
{"tags",
{
TypeSignature({Value::Type::VERTEX}, Value::Type::LIST),
Expand Down Expand Up @@ -1754,6 +1756,8 @@ FunctionManager::FunctionManager() {
return Value::kNullBadData;
}
return time::TimeUtils::dateTimeToUTC(result.value());
} else if (args[0].get().isInt()) {
return time::TimeConversion::unixSecondsToDateTime(args[0].get().getInt());
} else {
return Value::kNullBadData;
}
Expand Down
10 changes: 10 additions & 0 deletions src/common/function/test/FunctionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ TEST_F(FunctionManagerTest, time) {
{"2020-09-15T20:09:15"},
Value(time::TimeUtils::dateTimeToUTC(DateTime(2020, 9, 15, 20, 9, 15, 0))));
}
{
TEST_FUNCTION(
datetime, {0}, Value(time::TimeUtils::dateTimeToUTC(DateTime(1970, 1, 1, 0, 0, 0, 0))));
}
{
TEST_FUNCTION(datetime,
{Map({{"year", 2020},
Expand Down Expand Up @@ -836,6 +840,12 @@ TEST_F(FunctionManagerTest, time) {
TEST_FUNCTION(radians, args_["radians"], M_PI);
TEST_FUNCTION(radians, args_["nullvalue"], Value::kNullBadType);
}
{
auto result = FunctionManager::get("datetime", kLiteralTimeParaNumber);
ASSERT_TRUE(result.ok());
auto datetime = std::move(result).value()(genArgsRef({"2020-10-10T10:00:00"}));
TEST_FUNCTION(timestamp, {datetime}, 1602324000);
}
}

TEST_F(FunctionManagerTest, returnType) {
Expand Down
2 changes: 2 additions & 0 deletions src/common/time/TimeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ StatusOr<Value> TimeUtils::toTimestamp(const Value &val) {
timestamp = time::TimeConversion::dateTimeToUnixSeconds(dateTime);
} else if (val.isInt()) {
timestamp = val.getInt();
} else if (val.isDateTime()) {
timestamp = time::TimeConversion::dateTimeToUnixSeconds(val.getDateTime());
} else {
return Status::Error("Incorrect timestamp type: `%s'", val.toString().c_str());
}
Expand Down

0 comments on commit 0f75120

Please sign in to comment.