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

refactor(util): make time_utils decoupled from utils.cpp #388

Merged
merged 11 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions include/dsn/utility/time_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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 <chrono>
#include <cstdio>
#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<uint32_t>(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)
{
auto localtime = get_localtime(ts_ms);
neverchanje marked this conversation as resolved.
Show resolved Hide resolved
strftime(str, len, "%Y-%m-%d", localtime);
}

// 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)
{
auto localtime = get_localtime(ts_ms);
strftime(str, len, "%Y-%m-%d %H:%M:%S", localtime);
}

// 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 localtime = get_localtime(ts_ms);
hour = localtime->tm_hour;
min = localtime->tm_min;
sec = localtime->tm_sec;
}

inline uint64_t get_current_physical_time_ns()
{
auto now = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(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()
neverchanje marked this conversation as resolved.
Show resolved Hide resolved
{
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<int64_t>(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
27 changes: 0 additions & 27 deletions include/dsn/utility/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,6 @@ std::shared_ptr<T> make_shared_array(size_t size)
return std::shared_ptr<T>(new T[size], std::default_delete<T[]>());
}

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
Expand Down
1 change: 1 addition & 0 deletions src/core/core/env_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <dsn/utility/utils.h>
#include <dsn/c/api_utilities.h>
#include <chrono>
#include <dsn/utility/time_utils.h>

namespace dsn {

Expand Down
1 change: 1 addition & 0 deletions src/core/core/process_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <dsn/utility/utils.h>
#include <dsn/utility/preloadable.h>
#include <dsn/utility/process_utils.h>
#include <dsn/utility/time_utils.h>

namespace dsn {
namespace utils {
Expand Down
84 changes: 0 additions & 84 deletions src/core/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <sys/socket.h>

#include <array>
#include <chrono>
#include <fstream>
#include <iostream>
#include <memory>
Expand All @@ -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<std::chrono::nanoseconds>(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<uint32_t>(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<int64_t>(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;
Expand Down
1 change: 1 addition & 0 deletions src/core/perf_counter/perf_counter_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <dsn/utility/config_api.h>
#include <dsn/c/api_utilities.h>
#include <dsn/perf_counter/perf_counter.h>
#include <dsn/utility/time_utils.h>
#include "core/tools/common/shared_io_service.h"

namespace dsn {
Expand Down
1 change: 1 addition & 0 deletions src/core/perf_counter/perf_counters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <dsn/tool-api/command_manager.h>
#include <dsn/tool-api/task.h>
#include <dsn/utility/string_view.h>
#include <dsn/utility/time_utils.h>

#include "perf_counter_atomic.h"
#include "builtin_counters.h"
Expand Down
1 change: 1 addition & 0 deletions src/core/tests/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#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
1 change: 1 addition & 0 deletions src/core/tools/common/profiler_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <dsn/toollet/profiler.h>
#include "profiler_header.h"
#include <dsn/cpp/json_helper.h>
#include <dsn/utility/time_utils.h>

namespace dsn {
namespace tools {
Expand Down
1 change: 1 addition & 0 deletions src/core/tools/common/simple_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "simple_logger.h"
#include <sstream>
#include <dsn/utility/filesystem.h>
#include <dsn/utility/time_utils.h>

namespace dsn {
namespace tools {
Expand Down
1 change: 1 addition & 0 deletions src/dist/http/server_info_http_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// can be found in the LICENSE file in the root directory of this source tree.

#include <dsn/utility/output_utils.h>
#include <dsn/utility/time_utils.h>

#include "server_info_http_services.h"

Expand Down
1 change: 1 addition & 0 deletions src/dist/replication/ddl_lib/replication_ddl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <dsn/utility/error_code.h>
#include <dsn/utility/output_utils.h>
#include <fmt/format.h>
#include <dsn/utility/time_utils.h>

namespace dsn {
namespace replication {
Expand Down
1 change: 1 addition & 0 deletions src/dist/replication/lib/replica_backup.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <boost/lexical_cast.hpp>

#include <dsn/utility/filesystem.h>
#include <dsn/utility/time_utils.h>
#include <dsn/dist/fmt_logging.h>
#include <dsn/dist/replication/replication_app_base.h>

Expand Down
3 changes: 1 addition & 2 deletions src/dist/replication/meta_server/meta_backup_service.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "meta_backup_service.h"

#include <dsn/utility/filesystem.h>
#include <dsn/utility/time_utils.h>
#include <dsn/utility/output_utils.h>
#include <dsn/tool-api/http_server.h>

Expand Down
1 change: 1 addition & 0 deletions src/dist/replication/meta_server/meta_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <dsn/dist/replication/replication_types.h>
#include <dsn/utility/config_api.h>
#include <dsn/utility/output_utils.h>
#include <dsn/utility/time_utils.h>

#include "meta_http_service.h"
#include "meta_server_failure_detector.h"
Expand Down
1 change: 1 addition & 0 deletions src/dist/replication/meta_server/server_load_balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <dsn/utility/string_conv.h>
#include <dsn/tool-api/command_manager.h>
#include <boost/lexical_cast.hpp>
#include <dsn/utility/time_utils.h>

namespace dsn {
namespace replication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <dsn/service_api_cpp.h>
#include <dsn/utility/time_utils.h>

#include "dist/replication/meta_server/meta_backup_service.h"
#include "dist/replication/meta_server/meta_service.h"
Expand Down