From 441c86073869c5df55e91c39fd4b331212512022 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 7 Mar 2022 19:16:26 -0600 Subject: Merge functionality of misc_os_dependent into time_helper.h Actions: 1. Remove unused functions from misc_os_dependent.h 2. Move three remaining functions, get_gmt_time, get_ns_count, and get_tick_count into time_helper.h 3. Remove unused functions from time_helper.h 4. Refactor get_ns_count and get_internet_time_str and get_time_interval_string 5. Remove/add includes as needed Relevant commits on the old PR: a9fbe52b02ffab451e90c977459fea4642731cd1 9a59b131c4ed1be8afe238fff3780fe203c65a46 7fa9e2817df9b9ef3f0290f7f86357939829e588 --- contrib/epee/include/math_helper.h | 2 +- contrib/epee/include/misc_os_dependent.h | 129 -------------------- .../include/net/levin_protocol_handler_async.h | 2 +- contrib/epee/include/profile_tools.h | 2 +- contrib/epee/include/time_helper.h | 130 +++++---------------- contrib/epee/src/CMakeLists.txt | 1 - contrib/epee/src/misc_os_dependent.cpp | 44 ------- contrib/epee/src/mlog.cpp | 2 +- src/common/perf_timer.cpp | 2 +- src/common/util.cpp | 2 +- 10 files changed, 35 insertions(+), 281 deletions(-) delete mode 100644 contrib/epee/include/misc_os_dependent.h delete mode 100644 contrib/epee/src/misc_os_dependent.cpp diff --git a/contrib/epee/include/math_helper.h b/contrib/epee/include/math_helper.h index 29acffaea..6a759b515 100644 --- a/contrib/epee/include/math_helper.h +++ b/contrib/epee/include/math_helper.h @@ -37,8 +37,8 @@ #include #include -#include "misc_os_dependent.h" #include "syncobj.h" +#include "time_helper.h" namespace epee { diff --git a/contrib/epee/include/misc_os_dependent.h b/contrib/epee/include/misc_os_dependent.h deleted file mode 100644 index 522cdf263..000000000 --- a/contrib/epee/include/misc_os_dependent.h +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// * Neither the name of the Andrey N. Sabelnikov nor the -// names of its contributors may be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY -// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -#ifdef _WIN32 -#include -#endif - -#ifdef WIN32 - #ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif - - //#ifdef _WIN32_WINNT - // #undef _WIN32_WINNT - // #define _WIN32_WINNT 0x0600 - //#endif - - -#include -#endif - -#ifdef __MACH__ -#include -#include -#endif - -#include -#include - -#pragma once -namespace epee -{ -namespace misc_utils -{ - - inline uint64_t get_ns_count() - { -#if defined(_MSC_VER) - return ::GetTickCount64() * 1000000; -#elif defined(WIN32) - static LARGE_INTEGER pcfreq = {0}; - LARGE_INTEGER ticks; - if (!pcfreq.QuadPart) - QueryPerformanceFrequency(&pcfreq); - QueryPerformanceCounter(&ticks); - ticks.QuadPart *= 1000000000; /* we want nsec */ - return ticks.QuadPart / pcfreq.QuadPart; -#elif defined(__MACH__) - clock_serv_t cclock; - mach_timespec_t mts; - - host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - - return ((uint64_t)mts.tv_sec * 1000000000) + (mts.tv_nsec); -#else - struct timespec ts; - if(clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { - return 0; - } - return ((uint64_t)ts.tv_sec * 1000000000) + (ts.tv_nsec); -#endif - } - - inline uint64_t get_tick_count() - { - return get_ns_count() / 1000000; - } - - - inline int call_sys_cmd(const std::string& cmd) - { - std::cout << "# " << cmd << std::endl; - - FILE * fp ; - //char tstCommand[] ="ls *"; - char path[1000] = {0}; -#if !defined(__GNUC__) - fp = _popen(cmd.c_str(), "r"); -#else - fp = popen(cmd.c_str(), "r"); -#endif - while ( fgets( path, 1000, fp ) != NULL ) - std::cout << path; - -#if !defined(__GNUC__) - _pclose(fp); -#else - pclose(fp); -#endif - return 0; - - } - - std::string get_thread_string_id(); - - inline bool get_gmt_time(time_t t, struct tm &tm) - { -#ifdef _WIN32 - return gmtime_s(&tm, &t); -#else - return gmtime_r(&t, &tm); -#endif - } -} -} diff --git a/contrib/epee/include/net/levin_protocol_handler_async.h b/contrib/epee/include/net/levin_protocol_handler_async.h index 7cc42b5c4..bd6ffe930 100644 --- a/contrib/epee/include/net/levin_protocol_handler_async.h +++ b/contrib/epee/include/net/levin_protocol_handler_async.h @@ -37,7 +37,7 @@ #include "buffer.h" #include "misc_language.h" #include "syncobj.h" -#include "misc_os_dependent.h" +#include "time_helper.h" #include "int-util.h" #include diff --git a/contrib/epee/include/profile_tools.h b/contrib/epee/include/profile_tools.h index a0b5f77f4..76f794a36 100644 --- a/contrib/epee/include/profile_tools.h +++ b/contrib/epee/include/profile_tools.h @@ -28,7 +28,7 @@ #ifndef _PROFILE_TOOLS_H_ #define _PROFILE_TOOLS_H_ -#include "misc_os_dependent.h" +#include "time_helper.h" namespace epee { diff --git a/contrib/epee/include/time_helper.h b/contrib/epee/include/time_helper.h index 244b35800..632996bc5 100644 --- a/contrib/epee/include/time_helper.h +++ b/contrib/epee/include/time_helper.h @@ -28,132 +28,60 @@ #pragma once -//#include -//#include -#include -#include -#include "pragma_comp_defs.h" +#include +#include +#include +#include namespace epee { namespace misc_utils { - -#ifdef __ATLTIME_H__ - - inline - bool get_time_t_from_ole_date(DATE src, time_t& res) + inline bool get_gmt_time(time_t t, struct tm &tm) { - SYSTEMTIME st = {0}; - if(TRUE != ::VariantTimeToSystemTime(src, &st)) - return false; - ATL::CTime ss(st); - res = ss.GetTime(); - return true; - } +#ifdef _WIN32 + return gmtime_s(&tm, &t); +#else + return gmtime_r(&t, &tm); #endif - inline - std::string get_time_str(const time_t& time_) - { - - - char tmpbuf[200] = {0}; - tm* pt = NULL; -PRAGMA_WARNING_PUSH -PRAGMA_WARNING_DISABLE_VS(4996) - pt = localtime(&time_); -PRAGMA_WARNING_POP - - if(pt) - strftime( tmpbuf, 199, "%d.%m.%Y %H:%M:%S", pt ); - else - { - std::stringstream strs; - strs << "[wrong_time: " << std::hex << time_ << "]"; - return strs.str(); - } - return tmpbuf; } - inline - std::string get_time_str_v2(const time_t& time_) - { - - char tmpbuf[200] = {0}; - tm* pt = NULL; -PRAGMA_WARNING_PUSH -PRAGMA_WARNING_DISABLE_VS(4996) - pt = localtime(&time_); -PRAGMA_WARNING_POP - - if(pt) - strftime( tmpbuf, 199, "%Y_%m_%d %H_%M_%S", pt ); - else - { - std::stringstream strs; - strs << "[wrong_time: " << std::hex << time_ << "]"; - return strs.str(); - } - return tmpbuf; - } - - inline - std::string get_time_str_v3(const boost::posix_time::ptime& time_) - { - return boost::posix_time::to_simple_string(time_); - } - - - inline std::string get_internet_time_str(const time_t& time_) { char tmpbuf[200] = {0}; - tm* pt = NULL; -PRAGMA_WARNING_PUSH -PRAGMA_WARNING_DISABLE_VS(4996) - pt = gmtime(&time_); -PRAGMA_WARNING_POP - strftime( tmpbuf, 199, "%a, %d %b %Y %H:%M:%S GMT", pt ); + struct tm pt; + get_gmt_time(time_, pt); + strftime(tmpbuf, 199, "%a, %d %b %Y %H:%M:%S GMT", &pt); return tmpbuf; } inline std::string get_time_interval_string(const time_t& time_) { - std::string res; time_t tail = time_; -PRAGMA_WARNING_PUSH -PRAGMA_WARNING_DISABLE_VS(4244) - int days = tail/(60*60*24); + const int days = (int) (tail/(60*60*24)); tail = tail%(60*60*24); - int hours = tail/(60*60); + const int hours = (int) (tail/(60*60)); tail = tail%(60*60); - int minutes = tail/(60); + const int minutes = (int) (tail/(60)); tail = tail%(60); - int seconds = tail; -PRAGMA_WARNING_POP - res = std::string() + "d" + boost::lexical_cast(days) + ".h" + boost::lexical_cast(hours) + ".m" + boost::lexical_cast(minutes) + ".s" + boost::lexical_cast(seconds); - return res; + const int seconds = (int) tail; + + char tmpbuf[64] = {0}; + snprintf(tmpbuf, sizeof(tmpbuf) - 1, "d%d.h%d.m%d.s%d", days, hours, minutes, seconds); + + return tmpbuf; } -#ifdef __SQLEXT - inline - bool odbc_time_to_oledb_taime(const SQL_TIMESTAMP_STRUCT& odbc_timestamp, DATE& oledb_date) + inline uint64_t get_ns_count() { - - SYSTEMTIME st = {0}; - st.wYear = odbc_timestamp.year; - st.wDay = odbc_timestamp.day; - st.wHour = odbc_timestamp.hour ; - st.wMilliseconds = (WORD)odbc_timestamp.fraction ; - st.wMinute = odbc_timestamp.minute ; - st.wMonth = odbc_timestamp.month ; - st.wSecond = odbc_timestamp.second ; - - if(TRUE != ::SystemTimeToVariantTime(&st, &oledb_date)) - return false; - return true; + typedef std::chrono::duration ns_duration; + const ns_duration ns_since_epoch = std::chrono::steady_clock::now().time_since_epoch(); + return ns_since_epoch.count(); } -#endif + inline uint64_t get_tick_count() + { + return get_ns_count() / 1000000; + } } } diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt index 13d8154f3..808b9f09e 100644 --- a/contrib/epee/src/CMakeLists.txt +++ b/contrib/epee/src/CMakeLists.txt @@ -35,7 +35,6 @@ monero_add_library(epee byte_slice.cpp byte_stream.cpp hex.cpp abstract_http_cli wipeable_string.cpp levin_base.cpp memwipe.c connection_basic.cpp network_throttle.cpp network_throttle-detail.cpp mlocker.cpp buffer.cpp net_ssl.cpp int-util.cpp portable_storage.cpp misc_language.cpp - misc_os_dependent.cpp file_io_utils.cpp net_parse_helpers.cpp http_base.cpp diff --git a/contrib/epee/src/misc_os_dependent.cpp b/contrib/epee/src/misc_os_dependent.cpp deleted file mode 100644 index cd4967131..000000000 --- a/contrib/epee/src/misc_os_dependent.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// * Neither the name of the Andrey N. Sabelnikov nor the -// names of its contributors may be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY -// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// - -#include "misc_os_dependent.h" -#include - -namespace epee -{ -namespace misc_utils -{ - // TODO: (vtnerd) This function is weird since boost::this_thread::get_id() exists but returns a different value. - std::string get_thread_string_id() - { -#if defined(_WIN32) - return boost::lexical_cast(GetCurrentThreadId()); -#elif defined(__GNUC__) - return boost::lexical_cast(pthread_self()); -#endif - } -} -} diff --git a/contrib/epee/src/mlog.cpp b/contrib/epee/src/mlog.cpp index bcde215be..092d41777 100644 --- a/contrib/epee/src/mlog.cpp +++ b/contrib/epee/src/mlog.cpp @@ -40,7 +40,7 @@ #include #include #include "string_tools.h" -#include "misc_os_dependent.h" +#include "time_helper.h" #include "misc_log_ex.h" #undef MONERO_DEFAULT_LOG_CATEGORY diff --git a/src/common/perf_timer.cpp b/src/common/perf_timer.cpp index 1152bf25a..30164a557 100644 --- a/src/common/perf_timer.cpp +++ b/src/common/perf_timer.cpp @@ -27,7 +27,7 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include "misc_os_dependent.h" +#include "time_helper.h" #include "perf_timer.h" #undef MONERO_DEFAULT_LOG_CATEGORY diff --git a/src/common/util.cpp b/src/common/util.cpp index d607d8f7f..89dcf4fef 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -59,7 +59,7 @@ #include "include_base_utils.h" #include "file_io_utils.h" #include "wipeable_string.h" -#include "misc_os_dependent.h" +#include "time_helper.h" using namespace epee; #include "crypto/crypto.h" -- cgit v1.2.3