From 0f75120ef127b7128f83674f46173fd1e2ea1cad Mon Sep 17 00:00:00 2001 From: canon <87342612+caton-hpg@users.noreply.github.com> Date: Tue, 16 Aug 2022 17:27:51 +0800 Subject: [PATCH] datetime2timestamp and timestamp2datetime (#4526) --- src/common/function/FunctionManager.cpp | 8 ++++++-- src/common/function/test/FunctionManagerTest.cpp | 10 ++++++++++ src/common/time/TimeUtils.cpp | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/common/function/FunctionManager.cpp b/src/common/function/FunctionManager.cpp index 08bd27b110a..f73053cdc1f 100644 --- a/src/common/function/FunctionManager.cpp +++ b/src/common/function/FunctionManager.cpp @@ -220,11 +220,13 @@ std::unordered_map> 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), @@ -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; } diff --git a/src/common/function/test/FunctionManagerTest.cpp b/src/common/function/test/FunctionManagerTest.cpp index 24d7f3157b1..20e460c4661 100644 --- a/src/common/function/test/FunctionManagerTest.cpp +++ b/src/common/function/test/FunctionManagerTest.cpp @@ -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}, @@ -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) { diff --git a/src/common/time/TimeUtils.cpp b/src/common/time/TimeUtils.cpp index 35baf3a1c91..4b1c1630700 100644 --- a/src/common/time/TimeUtils.cpp +++ b/src/common/time/TimeUtils.cpp @@ -161,6 +161,8 @@ StatusOr 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()); }