From 0ab305de404ce55ca835ea40eaddfcf8cd652e21 Mon Sep 17 00:00:00 2001 From: shylock <33566796+Shylock-Hg@users.noreply.github.com> Date: Fri, 31 Dec 2021 10:42:35 +0800 Subject: [PATCH] Fix duration to string. (#3608) Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com> --- src/common/datatypes/Duration.h | 6 ++---- src/common/datatypes/test/ValueTest.cpp | 21 +++++++++++++++++++++ src/storage/test/GetNeighborsTest.cpp | 4 ++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/common/datatypes/Duration.h b/src/common/datatypes/Duration.h index 730ab1b324d..b5d9247b3f0 100644 --- a/src/common/datatypes/Duration.h +++ b/src/common/datatypes/Duration.h @@ -125,10 +125,8 @@ struct Duration { } std::string toString() const { - std::stringstream ss; - ss << "P" << months << "M" << days() << "D" - << "T" << seconds << "S"; - return ss.str(); + return folly::sformat( + "P{}MT{}.{:0>6}000S", months, seconds + microseconds / 1000000, microseconds % 1000000); } folly::dynamic toJson() const { diff --git a/src/common/datatypes/test/ValueTest.cpp b/src/common/datatypes/test/ValueTest.cpp index 00f89ed8d99..5cfe18c76b1 100644 --- a/src/common/datatypes/test/ValueTest.cpp +++ b/src/common/datatypes/test/ValueTest.cpp @@ -1630,6 +1630,27 @@ TEST(Value, Ctor) { // Value v2(&tmp); } +TEST(Value, ToString) { + { + Duration d; + d.addYears(1); + d.addMonths(2); + d.addDays(500); + d.addSeconds(10); + d.addMicroseconds(20); + EXPECT_EQ(d.toString(), "P14MT43200010.000020000S"); + } + { + Duration d; + d.addYears(1); + d.addMonths(2); + d.addDays(500); + d.addSeconds(10); + d.addMicroseconds(20000000); + EXPECT_EQ(d.toString(), "P14MT43200030.000000000S"); + } +} + } // namespace nebula int main(int argc, char** argv) { diff --git a/src/storage/test/GetNeighborsTest.cpp b/src/storage/test/GetNeighborsTest.cpp index cd9c5eb6b9c..c3b560732aa 100644 --- a/src/storage/test/GetNeighborsTest.cpp +++ b/src/storage/test/GetNeighborsTest.cpp @@ -1119,8 +1119,8 @@ TEST(GetNeighborsTest, TtlTest) { LOG(INFO) << "colName: " << s; } ASSERT_EQ("Tim Duncan", (*resp.vertices_ref()).rows[0].values[0].getStr()); - ASSERT_TRUE((*resp.vertices_ref()).rows[0].values[1].empty()); // stat - ASSERT_TRUE((*resp.vertices_ref()).rows[0].values[2].empty()); // expr + ASSERT_TRUE((*resp.vertices_ref()).rows[0].values[1].empty()); // stat + ASSERT_TRUE((*resp.vertices_ref()).rows[0].values[2].empty()); // expr } FLAGS_mock_ttl_col = false; }