diff --git a/include/dsn/utility/time_utils.h b/include/dsn/utility/time_utils.h new file mode 100644 index 0000000000..3101928b9c --- /dev/null +++ b/include/dsn/utility/time_utils.h @@ -0,0 +1,125 @@ +/* + * 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. + */ +#pragma once + +#include +#include +#include "string_view.h" + +namespace dsn { +namespace utils { + +static struct tm *get_localtime(uint64_t ts_ms) +{ + auto t = (time_t)(ts_ms / 1000); + struct tm tmp; + return localtime_r(&t, &tmp); +} + +// 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); + sprintf(str, + "%04d-%02d-%02d %02d:%02d:%02d.%03u", + ret->tm_year + 1900, + ret->tm_mon + 1, + ret->tm_mday, + ret->tm_hour, + ret->tm_min, + ret->tm_sec, + static_cast(ts_ms % 1000)); +} + +// 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)); +} + +// 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)); +} + +// 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); + hour = ret->tm_hour; + min = ret->tm_min; + sec = ret->tm_sec; +} + +inline uint64_t get_current_physical_time_ns() +{ + auto now = std::chrono::high_resolution_clock::now(); + return std::chrono::duration_cast(now.time_since_epoch()).count(); +} + +// 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); + struct tm tmp; + auto ret = localtime_r(&t, &tmp); + ret->tm_hour = 0; + ret->tm_min = 0; + ret->tm_sec = 0; + return static_cast(mktime(ret)); +} + +// `hh:mm` (range in [00:00, 23:59]) to seconds since 00:00:00 +// eg. `01:00` => `3600` +// Return: -1 when invalid +inline int hh_mm_to_seconds(dsn::string_view hhmm) +{ + int hour = 0, min = 0, sec = -1; + if (::sscanf(hhmm.data(), "%d:%d", &hour, &min) == 2 && (0 <= hour && hour <= 23) && + (0 <= min && min <= 59)) { + sec = 3600 * hour + 60 * min; + } + return sec; +} + +// local time `hh:mm` to unix timestamp. +// eg. `18:10` => `1525947000` when called on May 10, 2018, CST +// Return: -1 when invalid +inline int64_t hh_mm_today_to_unix_sec(string_view hhmm_of_day) +{ + int sec_of_day = hh_mm_to_seconds(hhmm_of_day); + if (sec_of_day == -1) { + return -1; + } + + return get_unix_sec_today_midnight() + sec_of_day; +} + +} // namespace utils +} // namespace dsn diff --git a/include/dsn/utility/utils.h b/include/dsn/utility/utils.h index 28b163b1c7..c9e414c93d 100644 --- a/include/dsn/utility/utils.h +++ b/include/dsn/utility/utils.h @@ -52,33 +52,6 @@ std::shared_ptr make_shared_array(size_t size) return std::shared_ptr(new T[size], std::default_delete()); } -void time_ms_to_string(uint64_t ts_ms, char *str); // yyyy-MM-dd hh:mm:ss.SSS - -void time_ms_to_date(uint64_t ts_ms, char *str, int len); // yyyy-MM-dd - -void time_ms_to_date_time(uint64_t ts_ms, char *str, int len); // yyyy-MM-dd hh:mm:ss - -void time_ms_to_date_time(uint64_t ts_ms, - int32_t &hour, - int32_t &min, - int32_t &sec); // time to hour, min, sec - -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 -int64_t get_unix_sec_today_midnight(); - -// `hh:mm` (range in [00:00, 23:59]) to seconds since 00:00:00 -// eg. `01:00` => `3600` -// Return: -1 when invalid -int hh_mm_to_seconds(dsn::string_view hhmm); - -// local time `hh:mm` to unix timestamp. -// eg. `18:10` => `1525947000` when called on May 10, 2018, CST -// Return: -1 when invalid -int64_t hh_mm_today_to_unix_sec(string_view hhmm_of_day); - // get host name from ip series // if can't get a hostname from ip(maybe no hostname or other errors), return false, and // hostname_result will be invalid value diff --git a/src/core/core/env_provider.cpp b/src/core/core/env_provider.cpp index 237e112303..4442b9a71b 100644 --- a/src/core/core/env_provider.cpp +++ b/src/core/core/env_provider.cpp @@ -37,6 +37,7 @@ #include #include #include +#include namespace dsn { diff --git a/src/core/core/process_utils.cpp b/src/core/core/process_utils.cpp index 383a0bb4cc..d684f888fa 100644 --- a/src/core/core/process_utils.cpp +++ b/src/core/core/process_utils.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace dsn { namespace utils { diff --git a/src/core/core/utils.cpp b/src/core/core/utils.cpp index 1bbf56ec48..7a3da0dacc 100644 --- a/src/core/core/utils.cpp +++ b/src/core/core/utils.cpp @@ -42,7 +42,6 @@ #include #include -#include #include #include #include @@ -65,89 +64,6 @@ namespace dsn { namespace utils { -uint64_t get_current_physical_time_ns() -{ - auto now = std::chrono::high_resolution_clock::now(); - auto nanos = - std::chrono::duration_cast(now.time_since_epoch()).count(); - return nanos; -} - -// len >= 24 -void time_ms_to_string(uint64_t ts_ms, char *str) -{ - auto t = (time_t)(ts_ms / 1000); - struct tm tmp; - auto ret = localtime_r(&t, &tmp); - sprintf(str, - "%04d-%02d-%02d %02d:%02d:%02d.%03u", - ret->tm_year + 1900, - ret->tm_mon + 1, - ret->tm_mday, - ret->tm_hour, - ret->tm_min, - ret->tm_sec, - static_cast(ts_ms % 1000)); -} - -// len >= 11 -void time_ms_to_date(uint64_t ts_ms, char *str, int len) -{ - auto t = (time_t)(ts_ms / 1000); - struct tm tmp; - auto ret = localtime_r(&t, &tmp); - strftime(str, len, "%Y-%m-%d", ret); -} - -// len >= 20 -void time_ms_to_date_time(uint64_t ts_ms, char *str, int len) -{ - auto t = (time_t)(ts_ms / 1000); - struct tm tmp; - auto ret = localtime_r(&t, &tmp); - strftime(str, len, "%Y-%m-%d %H:%M:%S", ret); -} - -void time_ms_to_date_time(uint64_t ts_ms, int32_t &hour, int32_t &min, int32_t &sec) -{ - auto t = (time_t)(ts_ms / 1000); - struct tm tmp; - auto ret = localtime_r(&t, &tmp); - hour = ret->tm_hour; - min = ret->tm_min; - sec = ret->tm_sec; -} - -int64_t get_unix_sec_today_midnight() -{ - time_t t = time(nullptr); - struct tm tmp; - auto ret = localtime_r(&t, &tmp); - ret->tm_hour = 0; - ret->tm_min = 0; - ret->tm_sec = 0; - return static_cast(mktime(ret)); -} - -int hh_mm_to_seconds(dsn::string_view hhmm) -{ - int hour = 0, min = 0, sec = -1; - if (::sscanf(hhmm.data(), "%d:%d", &hour, &min) == 2 && (0 <= hour && hour <= 23) && - (0 <= min && min <= 59)) { - sec = 3600 * hour + 60 * min; - } - return sec; -} - -int64_t hh_mm_today_to_unix_sec(string_view hhmm_of_day) -{ - int sec_of_day = hh_mm_to_seconds(hhmm_of_day); - if (sec_of_day == -1) { - return -1; - } - - return get_unix_sec_today_midnight() + sec_of_day; -} bool hostname_from_ip(uint32_t ip, std::string *hostname_result) { struct sockaddr_in addr_in; diff --git a/src/core/perf_counter/perf_counter_atomic.h b/src/core/perf_counter/perf_counter_atomic.h index 07dfdfdf55..7e384fa9d3 100644 --- a/src/core/perf_counter/perf_counter_atomic.h +++ b/src/core/perf_counter/perf_counter_atomic.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "core/tools/common/shared_io_service.h" namespace dsn { diff --git a/src/core/perf_counter/perf_counters.cpp b/src/core/perf_counter/perf_counters.cpp index d154a24c08..4ebc44ddb9 100644 --- a/src/core/perf_counter/perf_counters.cpp +++ b/src/core/perf_counter/perf_counters.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include "perf_counter_atomic.h" #include "builtin_counters.h" diff --git a/src/core/tests/utils.cpp b/src/core/tests/utils.cpp index 048da5756a..86d613a97a 100644 --- a/src/core/tests/utils.cpp +++ b/src/core/tests/utils.cpp @@ -43,6 +43,7 @@ #include #include #include +#include using namespace ::dsn; using namespace ::dsn::utils; diff --git a/src/core/tools/common/profiler_command.cpp b/src/core/tools/common/profiler_command.cpp index a983ce8f26..899480f559 100644 --- a/src/core/tools/common/profiler_command.cpp +++ b/src/core/tools/common/profiler_command.cpp @@ -42,6 +42,7 @@ #include #include "profiler_header.h" #include +#include namespace dsn { namespace tools { diff --git a/src/core/tools/common/simple_logger.cpp b/src/core/tools/common/simple_logger.cpp index c89e73cb85..792c2d28c6 100644 --- a/src/core/tools/common/simple_logger.cpp +++ b/src/core/tools/common/simple_logger.cpp @@ -36,6 +36,7 @@ #include "simple_logger.h" #include #include +#include namespace dsn { namespace tools { diff --git a/src/dist/http/server_info_http_services.cpp b/src/dist/http/server_info_http_services.cpp index eae4b70e30..1f2013254d 100644 --- a/src/dist/http/server_info_http_services.cpp +++ b/src/dist/http/server_info_http_services.cpp @@ -3,6 +3,7 @@ // can be found in the LICENSE file in the root directory of this source tree. #include +#include #include "server_info_http_services.h" diff --git a/src/dist/replication/ddl_lib/replication_ddl_client.cpp b/src/dist/replication/ddl_lib/replication_ddl_client.cpp index 7db2f33114..f3bcdac646 100644 --- a/src/dist/replication/ddl_lib/replication_ddl_client.cpp +++ b/src/dist/replication/ddl_lib/replication_ddl_client.cpp @@ -41,6 +41,7 @@ #include #include #include +#include namespace dsn { namespace replication { diff --git a/src/dist/replication/lib/replica_backup.cpp b/src/dist/replication/lib/replica_backup.cpp index 8c9eb35a84..c7b1c29015 100644 --- a/src/dist/replication/lib/replica_backup.cpp +++ b/src/dist/replication/lib/replica_backup.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include diff --git a/src/dist/replication/meta_server/meta_backup_service.cpp b/src/dist/replication/meta_server/meta_backup_service.cpp index 5669bc8c77..233d424652 100644 --- a/src/dist/replication/meta_server/meta_backup_service.cpp +++ b/src/dist/replication/meta_server/meta_backup_service.cpp @@ -1,6 +1,5 @@ -#include "meta_backup_service.h" - #include +#include #include #include diff --git a/src/dist/replication/meta_server/meta_http_service.cpp b/src/dist/replication/meta_server/meta_http_service.cpp index 94572c84ad..0579361fef 100644 --- a/src/dist/replication/meta_server/meta_http_service.cpp +++ b/src/dist/replication/meta_server/meta_http_service.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "meta_http_service.h" #include "meta_server_failure_detector.h" diff --git a/src/dist/replication/meta_server/server_load_balancer.cpp b/src/dist/replication/meta_server/server_load_balancer.cpp index 5af0641c4d..3193502eec 100644 --- a/src/dist/replication/meta_server/server_load_balancer.cpp +++ b/src/dist/replication/meta_server/server_load_balancer.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace dsn { namespace replication { diff --git a/src/dist/replication/test/meta_test/unit_test/backup_test.cpp b/src/dist/replication/test/meta_test/unit_test/backup_test.cpp index 5a1197a53b..1f84dd7e21 100644 --- a/src/dist/replication/test/meta_test/unit_test/backup_test.cpp +++ b/src/dist/replication/test/meta_test/unit_test/backup_test.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "dist/replication/meta_server/meta_backup_service.h" #include "dist/replication/meta_server/meta_service.h"