Replace epee C date crap with C++20 date backport

This commit is contained in:
Jason Rhinelander 2021-08-12 19:48:02 -03:00 committed by Thomas Winget
parent 2a46e4d736
commit 1f16daf109
12 changed files with 41 additions and 65 deletions

3
.gitmodules vendored
View File

@ -37,3 +37,6 @@
[submodule "external/nlohmann-json"]
path = external/nlohmann-json
url = https://github.com/nlohmann/json.git
[submodule "external/date"]
path = external/date
url = https://github.com/HowardHinnant/date.git

View File

@ -68,4 +68,5 @@ target_link_libraries(epee
PRIVATE
filesystem
Boost::thread
date::date
extra)

View File

@ -28,6 +28,8 @@
#ifndef _MLOG_H_
#define _MLOG_H_
#include <date/date.h>
#include <chrono>
#ifdef _WIN32
#include <windows.h>
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
@ -39,7 +41,6 @@
#include <atomic>
#include <boost/algorithm/string.hpp>
#include "epee/string_tools.h"
#include "epee/misc_os_dependent.h"
#include "epee/misc_log_ex.h"
#ifndef USE_GHC_FILESYSTEM
@ -59,23 +60,6 @@ namespace fs = ghc::filesystem;
using namespace epee;
static std::string generate_log_filename(const char *base)
{
std::string filename(base);
static unsigned int fallback_counter = 0;
char tmp[200];
struct tm tm;
time_t now = time(NULL);
if (!epee::misc_utils::get_gmt_time(now, tm))
snprintf(tmp, sizeof(tmp), "part-%u", ++fallback_counter);
else
strftime(tmp, sizeof(tmp), "%Y-%m-%d-%H-%M-%S", &tm);
tmp[sizeof(tmp) - 1] = 0;
filename += "-";
filename += tmp;
return filename;
}
std::string mlog_get_default_log_path(const char *default_filename)
{
std::string process_name = epee::string_tools::get_current_module_name();
@ -171,7 +155,7 @@ void mlog_configure(const std::string &filename_base, bool console, const std::s
el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
el::Helpers::installPreRollOutCallback([filename_base, max_log_files](const char *name, size_t){
std::string rname = generate_log_filename(filename_base.c_str());
std::string rname = filename_base + "-" + date::format("%Y-%m-%d-%H-%M-%S", std::chrono::system_clock::now());
int ret = rename(name, rname.c_str());
if (ret < 0)
{

View File

@ -84,6 +84,7 @@ add_subdirectory(db_drivers)
add_subdirectory(easylogging++ easyloggingpp)
add_subdirectory(randomx EXCLUDE_FROM_ALL)
add_subdirectory(fmt)
add_subdirectory(date EXCLUDE_FROM_ALL)
set(JSON_MultipleHeaders ON CACHE BOOL "") # Allows multi-header nlohmann use
add_subdirectory(nlohmann-json)

1
external/date vendored Submodule

@ -0,0 +1 @@
Subproject commit 6e921e1b1d21e84a5c82416ba7ecd98e33a436d0

View File

@ -87,7 +87,7 @@ target_link_libraries(blockchain_depth PRIVATE blockchain_tools_common_libs)
oxen_add_executable(blockchain_stats "oxen-blockchain-stats"
blockchain_stats.cpp
)
target_link_libraries(blockchain_stats PRIVATE blockchain_tools_common_libs)
target_link_libraries(blockchain_stats PRIVATE blockchain_tools_common_libs date::date)
# TODO(oxen): Blockchain pruning not supported in Oxen yet
# oxen_add_executable(blockchain_prune_known_spent_data "oxen-blockchain-prune-known-spent-data"

View File

@ -27,6 +27,7 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/algorithm/string.hpp>
#include <chrono>
#include "common/command_line.h"
#include "common/varint.h"
#include "common/signal_handler.h"
@ -35,8 +36,8 @@
#include "blockchain_objects.h"
#include "blockchain_db/blockchain_db.h"
#include "version.h"
#include "epee/misc_os_dependent.h"
#include "cryptonote_core/uptime_proof.h"
#include <date/date.h>
#undef OXEN_DEFAULT_LOG_CATEGORY
#define OXEN_DEFAULT_LOG_CATEGORY "bcutil"
@ -193,7 +194,7 @@ plot 'stats.csv' index "DATA" using (timecolumn(1,"%Y-%m-%d")):4 with lines, ''
}
std::cout << "\n";
struct tm prevtm = {0}, currtm;
std::optional<std::chrono::system_clock::time_point> prev_ts;
uint64_t prevsz = 0, currsz = 0;
uint64_t prevtxs = 0, currtxs = 0;
uint64_t currblks = 0;
@ -214,20 +215,20 @@ plot 'stats.csv' index "DATA" using (timecolumn(1,"%Y-%m-%d")):4 with lines, ''
LOG_PRINT_L0("Bad block from db");
return 1;
}
time_t tt = blk.timestamp;
char timebuf[64];
epee::misc_utils::get_gmt_time(tt, currtm);
if (!prevtm.tm_year)
prevtm = currtm;
auto ts = std::chrono::system_clock::from_time_t(blk.timestamp);
using namespace date;
year_month_day curr_date{floor<days>(ts)};
if (!prev_ts)
prev_ts = ts;
year_month_day prev_date{floor<days>(*prev_ts)};
// catch change of day
if (currtm.tm_mday > prevtm.tm_mday || (currtm.tm_mday == 1 && prevtm.tm_mday > 27))
if (curr_date.day() > prev_date.day() || (curr_date.day() == day{1} && prev_date.day() > day{27}))
{
// check for timestamp fudging around month ends
if (prevtm.tm_mday == 1 && currtm.tm_mday > 27)
if (curr_date.day() == day{1} && prev_date.day() > day{27})
goto skip;
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d", &prevtm);
prevtm = currtm;
std::cout << timebuf << "\t" << currblks << "\t" << h << "\t" << currtxs << "\t" << prevtxs + currtxs << "\t" << currsz << "\t" << prevsz + currsz;
prev_ts = ts;
std::cout << format("%Y-%m-%d", prev_date) << "\t" << currblks << "\t" << h << "\t" << currtxs << "\t" << prevtxs + currtxs << "\t" << currsz << "\t" << prevsz + currsz;
prevsz += currsz;
currsz = 0;
currblks = 0;
@ -277,7 +278,7 @@ skip:
currsz += bd.size();
currtxs++;
if (do_hours)
txhr[currtm.tm_hour]++;
txhr[hh_mm_ss{ts - floor<days>(ts)}.hours().count()]++;
if (do_inputs) {
io = tx.vin.size();
if (io < minins)

View File

@ -67,6 +67,7 @@ target_link_libraries(common
oxenmq::oxenmq
filesystem
fmt::fmt
date::date
PRIVATE
libunbound
OpenSSL::SSL

View File

@ -29,19 +29,21 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <chrono>
#include <string>
#include <iomanip>
#include <thread>
#include <openssl/ssl.h>
#include <date/date.h>
#include "unbound.h"
#include "epee/string_tools.h"
#include "epee/wipeable_string.h"
#include "crypto/crypto.h"
#include "util.h"
#include "epee/misc_os_dependent.h"
#include "epee/readline_buffer.h"
#include "string_util.h"
@ -220,16 +222,11 @@ namespace tools
}
#endif
std::string get_human_readable_timestamp(uint64_t ts)
std::string get_human_readable_timestamp(std::time_t t)
{
char buffer[64];
if (ts < 1234567890)
if (t < 1234567890)
return "<unknown>";
time_t tt = ts;
struct tm tm;
epee::misc_utils::get_gmt_time(tt, tm);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S UTC", &tm);
return std::string(buffer);
return date::format("%Y-%m-%d %H:%M:%S UTC", std::chrono::system_clock::from_time_t(t));
}
std::string get_human_readable_timespan(std::chrono::seconds seconds)

View File

@ -65,7 +65,7 @@ namespace tools
std::string input_line_win();
#endif
std::string get_human_readable_timestamp(uint64_t ts);
std::string get_human_readable_timestamp(std::time_t ts);
std::string get_human_readable_timespan(std::chrono::seconds seconds);
std::string get_human_readable_bytes(uint64_t bytes);

View File

@ -51,7 +51,6 @@ extern "C" {
#include "common/random.h"
#include "common/lock.h"
#include "common/hex.h"
#include "epee/misc_os_dependent.h"
#include "blockchain.h"
#include "service_node_quorum_cop.h"
@ -62,6 +61,8 @@ extern "C" {
#include "service_node_swarm.h"
#include "version.h"
#include <date/date.h>
#undef OXEN_DEFAULT_LOG_CATEGORY
#define OXEN_DEFAULT_LOG_CATEGORY "service_nodes"
@ -3759,14 +3760,8 @@ namespace service_nodes
if (make_friendly)
{
stream << "\n\n";
time_t tt = exp_timestamp;
struct tm tm;
epee::misc_utils::get_gmt_time(tt, tm);
char buffer[128];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M:%S %p UTC", &tm);
stream << tr("This registration expires at ") << buffer << tr(".\n");
auto exp = std::chrono::system_clock::from_time_t(exp_timestamp);
stream << tr("This registration expires at ") << date::format("%Y-%m-%d %I:%M:%S %p UTC", exp) << tr(".\n");
stream << tr("This should be in about 2 weeks, if it isn't, check this computer's clock.\n");
stream << tr("Please submit your registration into the blockchain before this time or it will be invalid.");
}

View File

@ -176,17 +176,6 @@ namespace {
return get_human_time_ago(std::chrono::seconds{now - t}, abbreviate);
}
char const *get_date_time(time_t t)
{
static char buf[128];
buf[0] = 0;
struct tm tm;
epee::misc_utils::get_gmt_time(t, tm);
strftime(buf, sizeof(buf), "%Y-%m-%d %I:%M:%S %p UTC", &tm);
return buf;
}
std::string get_time_hms(time_t t)
{
unsigned int hours, minutes, seconds;
@ -1616,9 +1605,12 @@ static void append_printable_service_node_list_entry(cryptonote::network_type ne
else
{
uint64_t delta_height = (blockchain_height >= expiry_height) ? 0 : expiry_height - blockchain_height;
uint64_t expiry_epoch_time = now + (delta_height * tools::to_seconds(TARGET_BLOCK_TIME));
auto expiry_epoch_time = now + (delta_height * tools::to_seconds(TARGET_BLOCK_TIME));
stream << expiry_height << " (in " << delta_height << ") blocks\n";
stream << indent2 << "Expiry Date (estimated): " << get_date_time(expiry_epoch_time) << " (" << get_human_time_ago(expiry_epoch_time, now) << ")\n";
stream << indent2 << "Expiry Date (estimated): " <<
date::format("%Y-%m-%d %I:%M:%S %p UTC", std::chrono::system_clock::from_time_t(expiry_epoch_time)) <<
" (" << get_human_time_ago(expiry_epoch_time, now) << ")\n";
}
}