diff options
author | Jeffrey <jeffryan@tamu.edu> | 2022-03-07 19:16:26 -0600 |
---|---|---|
committer | Jeffrey <jeffryan@tamu.edu> | 2022-04-18 09:55:21 -0500 |
commit | 441c86073869c5df55e91c39fd4b331212512022 (patch) | |
tree | 18129814dc6b2bd30567c758623f3d60b6e5093d /contrib/epee | |
parent | Add Include statements (diff) | |
download | monero-441c86073869c5df55e91c39fd4b331212512022.tar.xz |
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
Diffstat (limited to 'contrib/epee')
-rw-r--r-- | contrib/epee/include/math_helper.h | 2 | ||||
-rw-r--r-- | contrib/epee/include/misc_os_dependent.h | 129 | ||||
-rw-r--r-- | contrib/epee/include/net/levin_protocol_handler_async.h | 2 | ||||
-rw-r--r-- | contrib/epee/include/profile_tools.h | 2 | ||||
-rw-r--r-- | contrib/epee/include/time_helper.h | 130 | ||||
-rw-r--r-- | contrib/epee/src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | contrib/epee/src/misc_os_dependent.cpp | 44 | ||||
-rw-r--r-- | contrib/epee/src/mlog.cpp | 2 |
8 files changed, 33 insertions, 279 deletions
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 <boost/uuid/uuid.hpp> #include <boost/uuid/random_generator.hpp> -#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 <winsock2.h> -#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 <windows.h> -#endif - -#ifdef __MACH__ -#include <mach/clock.h> -#include <mach/mach.h> -#endif - -#include <iostream> -#include <ctime> - -#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 <random> 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 <atltime.h> -//#include <sqlext.h> -#include <boost/date_time/posix_time/posix_time.hpp> -#include <boost/date_time/local_time/local_time.hpp> -#include "pragma_comp_defs.h" +#include <chrono> +#include <cstdio> +#include <ctime> +#include <string> 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<std::string>(days) + ".h" + boost::lexical_cast<std::string>(hours) + ".m" + boost::lexical_cast<std::string>(minutes) + ".s" + boost::lexical_cast<std::string>(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<uint64_t, std::nano> 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 <boost/lexical_cast.hpp> - -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<std::string>(GetCurrentThreadId()); -#elif defined(__GNUC__) - return boost::lexical_cast<std::string>(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 <boost/filesystem.hpp> #include <boost/algorithm/string.hpp> #include "string_tools.h" -#include "misc_os_dependent.h" +#include "time_helper.h" #include "misc_log_ex.h" #undef MONERO_DEFAULT_LOG_CATEGORY |