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
This commit is contained in:
Jeffrey 2022-03-07 19:16:26 -06:00
parent 40f02f9d73
commit 441c860738
10 changed files with 35 additions and 281 deletions

View File

@ -37,8 +37,8 @@
#include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid.hpp>
#include <boost/uuid/random_generator.hpp> #include <boost/uuid/random_generator.hpp>
#include "misc_os_dependent.h"
#include "syncobj.h" #include "syncobj.h"
#include "time_helper.h"
namespace epee namespace epee
{ {

View File

@ -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
}
}
}

View File

@ -37,7 +37,7 @@
#include "buffer.h" #include "buffer.h"
#include "misc_language.h" #include "misc_language.h"
#include "syncobj.h" #include "syncobj.h"
#include "misc_os_dependent.h" #include "time_helper.h"
#include "int-util.h" #include "int-util.h"
#include <random> #include <random>

View File

@ -28,7 +28,7 @@
#ifndef _PROFILE_TOOLS_H_ #ifndef _PROFILE_TOOLS_H_
#define _PROFILE_TOOLS_H_ #define _PROFILE_TOOLS_H_
#include "misc_os_dependent.h" #include "time_helper.h"
namespace epee namespace epee
{ {

View File

@ -28,132 +28,60 @@
#pragma once #pragma once
//#include <atltime.h> #include <chrono>
//#include <sqlext.h> #include <cstdio>
#include <boost/date_time/posix_time/posix_time.hpp> #include <ctime>
#include <boost/date_time/local_time/local_time.hpp> #include <string>
#include "pragma_comp_defs.h"
namespace epee namespace epee
{ {
namespace misc_utils namespace misc_utils
{ {
inline bool get_gmt_time(time_t t, struct tm &tm)
#ifdef __ATLTIME_H__
inline
bool get_time_t_from_ole_date(DATE src, time_t& res)
{ {
SYSTEMTIME st = {0}; #ifdef _WIN32
if(TRUE != ::VariantTimeToSystemTime(src, &st)) return gmtime_s(&tm, &t);
return false; #else
ATL::CTime ss(st); return gmtime_r(&t, &tm);
res = ss.GetTime();
return true;
}
#endif #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_) inline std::string get_internet_time_str(const time_t& time_)
{ {
char tmpbuf[200] = {0}; char tmpbuf[200] = {0};
tm* pt = NULL; struct tm pt;
PRAGMA_WARNING_PUSH get_gmt_time(time_, pt);
PRAGMA_WARNING_DISABLE_VS(4996) strftime(tmpbuf, 199, "%a, %d %b %Y %H:%M:%S GMT", &pt);
pt = gmtime(&time_);
PRAGMA_WARNING_POP
strftime( tmpbuf, 199, "%a, %d %b %Y %H:%M:%S GMT", pt );
return tmpbuf; return tmpbuf;
} }
inline std::string get_time_interval_string(const time_t& time_) inline std::string get_time_interval_string(const time_t& time_)
{ {
std::string res;
time_t tail = time_; time_t tail = time_;
PRAGMA_WARNING_PUSH const int days = (int) (tail/(60*60*24));
PRAGMA_WARNING_DISABLE_VS(4244)
int days = tail/(60*60*24);
tail = 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); tail = tail%(60*60);
int minutes = tail/(60); const int minutes = (int) (tail/(60));
tail = tail%(60); tail = tail%(60);
int seconds = tail; const int seconds = (int) 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); char tmpbuf[64] = {0};
return res; snprintf(tmpbuf, sizeof(tmpbuf) - 1, "d%d.h%d.m%d.s%d", days, hours, minutes, seconds);
return tmpbuf;
} }
#ifdef __SQLEXT inline uint64_t get_ns_count()
inline
bool odbc_time_to_oledb_taime(const SQL_TIMESTAMP_STRUCT& odbc_timestamp, DATE& oledb_date)
{ {
typedef std::chrono::duration<uint64_t, std::nano> ns_duration;
SYSTEMTIME st = {0}; const ns_duration ns_since_epoch = std::chrono::steady_clock::now().time_since_epoch();
st.wYear = odbc_timestamp.year; return ns_since_epoch.count();
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;
} }
#endif inline uint64_t get_tick_count()
{
return get_ns_count() / 1000000;
}
} }
} }

View File

@ -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 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 int-util.cpp portable_storage.cpp
misc_language.cpp misc_language.cpp
misc_os_dependent.cpp
file_io_utils.cpp file_io_utils.cpp
net_parse_helpers.cpp net_parse_helpers.cpp
http_base.cpp http_base.cpp

View File

@ -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
}
}
}

View File

@ -40,7 +40,7 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include "string_tools.h" #include "string_tools.h"
#include "misc_os_dependent.h" #include "time_helper.h"
#include "misc_log_ex.h" #include "misc_log_ex.h"
#undef MONERO_DEFAULT_LOG_CATEGORY #undef MONERO_DEFAULT_LOG_CATEGORY

View File

@ -27,7 +27,7 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <vector> #include <vector>
#include "misc_os_dependent.h" #include "time_helper.h"
#include "perf_timer.h" #include "perf_timer.h"
#undef MONERO_DEFAULT_LOG_CATEGORY #undef MONERO_DEFAULT_LOG_CATEGORY

View File

@ -59,7 +59,7 @@
#include "include_base_utils.h" #include "include_base_utils.h"
#include "file_io_utils.h" #include "file_io_utils.h"
#include "wipeable_string.h" #include "wipeable_string.h"
#include "misc_os_dependent.h" #include "time_helper.h"
using namespace epee; using namespace epee;
#include "crypto/crypto.h" #include "crypto/crypto.h"