From bea3eab2d84c6e442d564990c434ae572a0f04a4 Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 11:19:37 +0800 Subject: [PATCH 1/8] bug fix --- include/dsn/utility/time_utils.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/include/dsn/utility/time_utils.h b/include/dsn/utility/time_utils.h index 3101928b9c..c87ca69e53 100644 --- a/include/dsn/utility/time_utils.h +++ b/include/dsn/utility/time_utils.h @@ -32,17 +32,17 @@ namespace dsn { namespace utils { -static struct tm *get_localtime(uint64_t ts_ms) +static struct tm *get_localtime(uint64_t ts_ms, struct tm *tm_buf) { auto t = (time_t)(ts_ms / 1000); - struct tm tmp; - return localtime_r(&t, &tmp); + return localtime_r(&t, tm_buf); } // get time string, which format is yyyy-MM-dd hh:mm:ss.SSS inline void time_ms_to_string(uint64_t ts_ms, char *str) { - auto ret = get_localtime(ts_ms); + struct tm tmp; + auto ret = get_localtime(ts_ms, &tmp); sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d.%03u", ret->tm_year + 1900, @@ -57,19 +57,22 @@ inline void time_ms_to_string(uint64_t ts_ms, char *str) // get date string with format of 'yyyy-MM-dd' from given timestamp inline void time_ms_to_date(uint64_t ts_ms, char *str, int len) { - strftime(str, len, "%Y-%m-%d", get_localtime(ts_ms)); + struct tm tmp; + strftime(str, len, "%Y-%m-%d", get_localtime(ts_ms, &tmp)); } // get date string with format of 'yyyy-MM-dd hh:mm:ss' from given timestamp inline void time_ms_to_date_time(uint64_t ts_ms, char *str, int len) { - strftime(str, len, "%Y-%m-%d %H:%M:%S", get_localtime(ts_ms)); + struct tm tmp; + strftime(str, len, "%Y-%m-%d %H:%M:%S", get_localtime(ts_ms, &tmp)); } // parse hour/min/sec from the given timestamp inline void time_ms_to_date_time(uint64_t ts_ms, int32_t &hour, int32_t &min, int32_t &sec) { - auto ret = get_localtime(ts_ms); + struct tm tmp; + auto ret = get_localtime(ts_ms, &tmp); hour = ret->tm_hour; min = ret->tm_min; sec = ret->tm_sec; @@ -83,7 +86,6 @@ inline uint64_t get_current_physical_time_ns() // get unix timestamp of today's zero o'clock. // eg. `1525881600` returned when called on May 10, 2018, CST - inline int64_t get_unix_sec_today_midnight() { time_t t = time(nullptr); From ae432c05c143d658759c8d0f4c963c54d40e2a41 Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 12:34:16 +0800 Subject: [PATCH 2/8] bugfix --- src/core/tests/time_utils_test.cpp | 120 +++++++++++++++++++++++++++++ src/core/tests/utils.cpp | 49 ------------ 2 files changed, 120 insertions(+), 49 deletions(-) create mode 100644 src/core/tests/time_utils_test.cpp diff --git a/src/core/tests/time_utils_test.cpp b/src/core/tests/time_utils_test.cpp new file mode 100644 index 0000000000..56a5ad571c --- /dev/null +++ b/src/core/tests/time_utils_test.cpp @@ -0,0 +1,120 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Microsoft Corporation + * + * -=- Robust Distributed System Nucleus (rDSN) -=- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +using namespace ::dsn; +using namespace ::dsn::utils; + +TEST(core, hh_mm_to_seconds) +{ + ASSERT_EQ(hh_mm_to_seconds("00:00"), 0); + ASSERT_EQ(hh_mm_to_seconds("23:59"), 86340); + ASSERT_EQ(hh_mm_to_seconds("1:1"), 3660); + ASSERT_EQ(hh_mm_to_seconds("01:1"), 3660); + ASSERT_EQ(hh_mm_to_seconds("1:01"), 3660); + ASSERT_EQ(hh_mm_to_seconds("01:01"), 3660); + + ASSERT_EQ(hh_mm_to_seconds("23"), -1); + ASSERT_EQ(hh_mm_to_seconds("23:"), -1); + ASSERT_EQ(hh_mm_to_seconds(":59"), -1); + ASSERT_EQ(hh_mm_to_seconds("-1:00"), -1); + ASSERT_EQ(hh_mm_to_seconds("24:00"), -1); + ASSERT_EQ(hh_mm_to_seconds("01:-1"), -1); + ASSERT_EQ(hh_mm_to_seconds("01:60"), -1); + ASSERT_EQ(hh_mm_to_seconds("a:00"), -1); + ASSERT_EQ(hh_mm_to_seconds("01:b"), -1); + ASSERT_EQ(hh_mm_to_seconds("01b"), -1); +} + +TEST(core, get_unix_sec_today_midnight) +{ + ASSERT_LT(0, get_unix_sec_today_midnight()); + ASSERT_LE(get_unix_sec_today_midnight(), time(nullptr)); + ASSERT_GE(time(nullptr) - get_unix_sec_today_midnight(), 0); + ASSERT_LT(time(nullptr) - get_unix_sec_today_midnight(), 86400); +} + +TEST(core, hh_mm_today_to_unix_sec) +{ + ASSERT_EQ(get_unix_sec_today_midnight() + hh_mm_to_seconds("0:0"), + hh_mm_today_to_unix_sec("0:0")); + ASSERT_EQ(get_unix_sec_today_midnight() + hh_mm_to_seconds("23:59"), + hh_mm_today_to_unix_sec("23:59")); + + ASSERT_EQ(hh_mm_today_to_unix_sec("23"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("23:"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec(":59"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("-1:00"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("24:00"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("01:-1"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("01:60"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("a:00"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("01:b"), -1); + ASSERT_EQ(hh_mm_today_to_unix_sec("01b"), -1); +} + +TEST(core, time_ms_to_string) +{ + uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 + char buf[24]; + time_ms_to_string(ts_ms, buf); + ASSERT_EQ("2020-02-13 12:30:30.235", buf); +} + +TEST(core, time_ms_to_date) +{ + uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 + char buf[11]; + time_ms_to_date(ts_ms, buf, sifzeof(buf)); + ASSERT_EQ("2020-02-13", buf); +} + +TEST(core, time_ms_to_date_time) +{ + uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 + int32_t hour = 0, min = 0, sec = 0; + time_ms_to_date_time(ts_ms, hour, min, sec); + ASSERT_EQ(12, hour); + ASSERT_EQ(30, min); + ASSERT_EQ(30, sec); +} + +TEST(core, time_ms_to_date_time_str) +{ + uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 + char buf[20]; + time_ms_to_date_time(ts_ms, buf, sifzeof(buf)); + ASSERT_EQ("2020-02-13 12:30:30", buf); +} + +TEST(core, get_current_physical_time_ns) +{ + int64_t ts_ns = get_current_physical_time_ns(); + ASSERT_LT(0, ts_ns); + ASSERT_GE(get_unix_sec_today_midnight() - ts_ns, 0); + ASSERT_LT(get_unix_sec_today_midnight() - ts_ns, 1e9); // < 1s +} diff --git a/src/core/tests/utils.cpp b/src/core/tests/utils.cpp index 86d613a97a..2e10d20c0b 100644 --- a/src/core/tests/utils.cpp +++ b/src/core/tests/utils.cpp @@ -43,7 +43,6 @@ #include #include #include -#include using namespace ::dsn; using namespace ::dsn::utils; @@ -264,51 +263,3 @@ TEST(core, ref_ptr) z = foo_ptr(); EXPECT_TRUE(count == 0); } - -TEST(core, hh_mm_to_seconds) -{ - ASSERT_EQ(hh_mm_to_seconds("00:00"), 0); - ASSERT_EQ(hh_mm_to_seconds("23:59"), 86340); - ASSERT_EQ(hh_mm_to_seconds("1:1"), 3660); - ASSERT_EQ(hh_mm_to_seconds("01:1"), 3660); - ASSERT_EQ(hh_mm_to_seconds("1:01"), 3660); - ASSERT_EQ(hh_mm_to_seconds("01:01"), 3660); - - ASSERT_EQ(hh_mm_to_seconds("23"), -1); - ASSERT_EQ(hh_mm_to_seconds("23:"), -1); - ASSERT_EQ(hh_mm_to_seconds(":59"), -1); - ASSERT_EQ(hh_mm_to_seconds("-1:00"), -1); - ASSERT_EQ(hh_mm_to_seconds("24:00"), -1); - ASSERT_EQ(hh_mm_to_seconds("01:-1"), -1); - ASSERT_EQ(hh_mm_to_seconds("01:60"), -1); - ASSERT_EQ(hh_mm_to_seconds("a:00"), -1); - ASSERT_EQ(hh_mm_to_seconds("01:b"), -1); - ASSERT_EQ(hh_mm_to_seconds("01b"), -1); -} - -TEST(core, get_unix_sec_today_midnight) -{ - ASSERT_LT(0, get_unix_sec_today_midnight()); - ASSERT_LE(get_unix_sec_today_midnight(), time(nullptr)); - ASSERT_GE(time(nullptr) - get_unix_sec_today_midnight(), 0); - ASSERT_LT(time(nullptr) - get_unix_sec_today_midnight(), 86400); -} - -TEST(core, hh_mm_today_to_unix_sec) -{ - ASSERT_EQ(get_unix_sec_today_midnight() + hh_mm_to_seconds("0:0"), - hh_mm_today_to_unix_sec("0:0")); - ASSERT_EQ(get_unix_sec_today_midnight() + hh_mm_to_seconds("23:59"), - hh_mm_today_to_unix_sec("23:59")); - - ASSERT_EQ(hh_mm_today_to_unix_sec("23"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("23:"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec(":59"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("-1:00"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("24:00"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("01:-1"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("01:60"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("a:00"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("01:b"), -1); - ASSERT_EQ(hh_mm_today_to_unix_sec("01b"), -1); -} From 798087fac00cc194d10bf4c5d00cd76db224a277 Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 13:21:02 +0800 Subject: [PATCH 3/8] fix --- src/core/tests/time_utils_test.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/tests/time_utils_test.cpp b/src/core/tests/time_utils_test.cpp index 56a5ad571c..0f3ac9dac3 100644 --- a/src/core/tests/time_utils_test.cpp +++ b/src/core/tests/time_utils_test.cpp @@ -25,6 +25,7 @@ */ #include +#include using namespace ::dsn; using namespace ::dsn::utils; @@ -89,7 +90,7 @@ TEST(core, time_ms_to_date) { uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 char buf[11]; - time_ms_to_date(ts_ms, buf, sifzeof(buf)); + time_ms_to_date(ts_ms, buf, sizeof(buf)); ASSERT_EQ("2020-02-13", buf); } @@ -107,7 +108,7 @@ TEST(core, time_ms_to_date_time_str) { uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 char buf[20]; - time_ms_to_date_time(ts_ms, buf, sifzeof(buf)); + time_ms_to_date_time(ts_ms, buf, sizeof(buf)); ASSERT_EQ("2020-02-13 12:30:30", buf); } From a994bc9c281dfaecf2cd9ca7a50a6fc58142e363 Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 13:27:28 +0800 Subject: [PATCH 4/8] unit test --- src/core/tests/time_utils_test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/tests/time_utils_test.cpp b/src/core/tests/time_utils_test.cpp index 0f3ac9dac3..7477775403 100644 --- a/src/core/tests/time_utils_test.cpp +++ b/src/core/tests/time_utils_test.cpp @@ -83,7 +83,7 @@ TEST(core, time_ms_to_string) uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 char buf[24]; time_ms_to_string(ts_ms, buf); - ASSERT_EQ("2020-02-13 12:30:30.235", buf); + ASSERT_EQ("2020-02-13 12:30:30.235", std::string(buf)); } TEST(core, time_ms_to_date) @@ -91,7 +91,7 @@ TEST(core, time_ms_to_date) uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 char buf[11]; time_ms_to_date(ts_ms, buf, sizeof(buf)); - ASSERT_EQ("2020-02-13", buf); + ASSERT_EQ("2020-02-13", std::string(buf)); } TEST(core, time_ms_to_date_time) @@ -109,13 +109,13 @@ TEST(core, time_ms_to_date_time_str) uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 char buf[20]; time_ms_to_date_time(ts_ms, buf, sizeof(buf)); - ASSERT_EQ("2020-02-13 12:30:30", buf); + ASSERT_EQ("2020-02-13 12:30:30", std::string(buf)); } TEST(core, get_current_physical_time_ns) { int64_t ts_ns = get_current_physical_time_ns(); ASSERT_LT(0, ts_ns); - ASSERT_GE(get_unix_sec_today_midnight() - ts_ns, 0); - ASSERT_LT(get_unix_sec_today_midnight() - ts_ns, 1e9); // < 1s + ASSERT_GE(get_current_physical_time_ns() - ts_ns, 0); + ASSERT_LT(get_current_physical_time_ns() - ts_ns, 1e9); // < 1s } From 5c569e1bb616c58428a1573a88c9ce6d5636f2a5 Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 13:58:32 +0800 Subject: [PATCH 5/8] unit test --- src/core/tests/time_utils_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/tests/time_utils_test.cpp b/src/core/tests/time_utils_test.cpp index 7477775403..98137dd122 100644 --- a/src/core/tests/time_utils_test.cpp +++ b/src/core/tests/time_utils_test.cpp @@ -117,5 +117,5 @@ TEST(core, get_current_physical_time_ns) int64_t ts_ns = get_current_physical_time_ns(); ASSERT_LT(0, ts_ns); ASSERT_GE(get_current_physical_time_ns() - ts_ns, 0); - ASSERT_LT(get_current_physical_time_ns() - ts_ns, 1e9); // < 1s + ASSERT_LT(get_current_physical_time_ns() - ts_ns, 1e7); // < 10 ms } From 7f11c8d13ca3aa0a4de7aa4c7e27e85c4f0f67b3 Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 14:12:42 +0800 Subject: [PATCH 6/8] fix --- src/core/tests/time_utils_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/tests/time_utils_test.cpp b/src/core/tests/time_utils_test.cpp index 98137dd122..d5376ec1c7 100644 --- a/src/core/tests/time_utils_test.cpp +++ b/src/core/tests/time_utils_test.cpp @@ -27,7 +27,6 @@ #include #include -using namespace ::dsn; using namespace ::dsn::utils; TEST(core, hh_mm_to_seconds) From 4450e34e9a90a3fcf92a9af1535f35f7c68548e7 Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 15:31:23 +0800 Subject: [PATCH 7/8] delete --- src/core/tests/time_utils_test.cpp | 34 ------------------------------ 1 file changed, 34 deletions(-) diff --git a/src/core/tests/time_utils_test.cpp b/src/core/tests/time_utils_test.cpp index d5376ec1c7..715c7ae05a 100644 --- a/src/core/tests/time_utils_test.cpp +++ b/src/core/tests/time_utils_test.cpp @@ -77,40 +77,6 @@ TEST(core, hh_mm_today_to_unix_sec) ASSERT_EQ(hh_mm_today_to_unix_sec("01b"), -1); } -TEST(core, time_ms_to_string) -{ - uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 - char buf[24]; - time_ms_to_string(ts_ms, buf); - ASSERT_EQ("2020-02-13 12:30:30.235", std::string(buf)); -} - -TEST(core, time_ms_to_date) -{ - uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 - char buf[11]; - time_ms_to_date(ts_ms, buf, sizeof(buf)); - ASSERT_EQ("2020-02-13", std::string(buf)); -} - -TEST(core, time_ms_to_date_time) -{ - uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 - int32_t hour = 0, min = 0, sec = 0; - time_ms_to_date_time(ts_ms, hour, min, sec); - ASSERT_EQ(12, hour); - ASSERT_EQ(30, min); - ASSERT_EQ(30, sec); -} - -TEST(core, time_ms_to_date_time_str) -{ - uint64_t ts_ms = 1581568230235; // 2020-02-13 12:30:30.235 - char buf[20]; - time_ms_to_date_time(ts_ms, buf, sizeof(buf)); - ASSERT_EQ("2020-02-13 12:30:30", std::string(buf)); -} - TEST(core, get_current_physical_time_ns) { int64_t ts_ns = get_current_physical_time_ns(); From f1a65a4998cb6c2eaaa72fc9bc3a8f00093ca94b Mon Sep 17 00:00:00 2001 From: levy5307 Date: Thu, 13 Feb 2020 16:50:11 +0800 Subject: [PATCH 8/8] fix --- src/core/tests/time_utils_test.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/tests/time_utils_test.cpp b/src/core/tests/time_utils_test.cpp index 715c7ae05a..8a9e56805b 100644 --- a/src/core/tests/time_utils_test.cpp +++ b/src/core/tests/time_utils_test.cpp @@ -27,7 +27,8 @@ #include #include -using namespace ::dsn::utils; +namespace dsn { +namespace utils { TEST(core, hh_mm_to_seconds) { @@ -84,3 +85,6 @@ TEST(core, get_current_physical_time_ns) ASSERT_GE(get_current_physical_time_ns() - ts_ns, 0); ASSERT_LT(get_current_physical_time_ns() - ts_ns, 1e7); // < 10 ms } + +} // namespace utils +} // namespace dsn