Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
fix(utils): fix bug in time_utils (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
levy5307 authored Feb 13, 2020
1 parent 2efa13d commit 0db7b36
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 57 deletions.
18 changes: 10 additions & 8 deletions include/dsn/utility/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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);
Expand Down
90 changes: 90 additions & 0 deletions src/core/tests/time_utils_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 <dsn/utility/time_utils.h>
#include <gtest/gtest.h>

namespace dsn {
namespace 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, 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, 1e7); // < 10 ms
}

} // namespace utils
} // namespace dsn
49 changes: 0 additions & 49 deletions src/core/tests/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <dsn/c/api_layer1.h>
#include <gtest/gtest.h>
#include <dsn/utility/rand.h>
#include <dsn/utility/time_utils.h>

using namespace ::dsn;
using namespace ::dsn::utils;
Expand Down Expand Up @@ -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);
}

0 comments on commit 0db7b36

Please sign in to comment.