From 9a59b131c4ed1be8afe238fff3780fe203c65a46 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Wed, 2 Mar 2022 00:32:22 -0600 Subject: [PATCH] Merge misc_os_dependent.h into time_helper.h Actions: * Move get_ns_count(), get_tick_count(), and get_gmt_time() from misc_os_dependent.h to time_helper.h. I did this because all those functions are time-related and under the same namespace * Remove misc_os_dependent.h because it is now empty * Change all includes of misc_os_dependent.h to time_helper.h where appropriate * Remove get_time_string*() from time_helper.h b/c its unused * Remove get_time_t_from_ole_date() from time_helper.h b/c its unused * Remove odbc_time_to_oledb_taime() from time_helper.h b/c its unused * Refactor get_ns_count() to use std::chrono instead of 4 different implementation-specific hacks --- contrib/epee/include/math_helper.h | 2 +- contrib/epee/include/misc_os_dependent.h | 102 ----------------- .../net/levin_protocol_handler_async.h | 2 +- contrib/epee/include/profile_tools.h | 2 +- contrib/epee/include/time_helper.h | 107 ++++-------------- contrib/epee/src/mlog.cpp | 2 +- src/common/perf_timer.cpp | 2 +- src/common/util.cpp | 2 +- src/p2p/p2p_protocol_defs.h | 1 + 9 files changed, 28 insertions(+), 194 deletions(-) delete mode 100644 contrib/epee/include/misc_os_dependent.h 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 c8f6f28ae..000000000 --- a/contrib/epee/include/misc_os_dependent.h +++ /dev/null @@ -1,102 +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 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 a6816cafc..24e1a018c 100644 --- a/contrib/epee/include/net/levin_protocol_handler_async.h +++ b/contrib/epee/include/net/levin_protocol_handler_async.h @@ -38,7 +38,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..bfbcd5a23 100644 --- a/contrib/epee/include/time_helper.h +++ b/contrib/epee/include/time_helper.h @@ -28,83 +28,18 @@ #pragma once -//#include -//#include #include #include +#include +#include +#include + #include "pragma_comp_defs.h" namespace epee { namespace misc_utils { - -#ifdef __ATLTIME_H__ - - inline - bool get_time_t_from_ole_date(DATE src, time_t& res) - { - SYSTEMTIME st = {0}; - if(TRUE != ::VariantTimeToSystemTime(src, &st)) - return false; - ATL::CTime ss(st); - res = ss.GetTime(); - return true; - } -#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}; @@ -135,25 +70,25 @@ PRAGMA_WARNING_POP return res; } -#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(); } + inline uint64_t get_tick_count() + { + return get_ns_count() / 1000000; + } + + 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 + } } -} +} \ No newline at end of file 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 898701b2f..11b1aabf3 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 af9843b95..b7aec434e 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" diff --git a/src/p2p/p2p_protocol_defs.h b/src/p2p/p2p_protocol_defs.h index 12763d4ee..22a3a186a 100644 --- a/src/p2p/p2p_protocol_defs.h +++ b/src/p2p/p2p_protocol_defs.h @@ -30,6 +30,7 @@ #pragma once +#include #include #include #include "serialization/keyvalue_serialization.h"