Skip to content

Commit

Permalink
Merge pull request #2319 from barton2526/localtime
Browse files Browse the repository at this point in the history
util: Don't use gmtime() or localtime()
  • Loading branch information
jamescowens committed Sep 12, 2021
2 parents 36e5f5f + 9cdabec commit 63a6393
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
10 changes: 3 additions & 7 deletions src/gridcoin/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "util.h"
#include "util/time.h"

#include <boost/date_time/posix_time/posix_time.hpp>

#include <string>

using namespace GRC;
Expand All @@ -21,15 +23,9 @@ fs::path GRC::GetBackupPath()

std::string GRC::GetBackupFilename(const std::string& basename, const std::string& suffix)
{
time_t biTime;
struct tm * blTime;
time (&biTime);
blTime = localtime(&biTime);
char boTime[200];
strftime(boTime, sizeof(boTime), "%Y-%m-%dT%H-%M-%S", blTime);
std::string sBackupFilename;
fs::path rpath;
sBackupFilename = basename + "-" + std::string(boTime);
sBackupFilename = basename + "-" + std::string(FormatISO8601DateTime(GetTime()));
if (!suffix.empty())
sBackupFilename = sBackupFilename + "-" + suffix;
rpath = GetBackupPath() / sBackupFilename;
Expand Down
11 changes: 11 additions & 0 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "util/time.h"
#include "util/system.h"

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filtering_stream.hpp>
Expand Down Expand Up @@ -502,3 +503,13 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)
return false; // archive condition was not satisfied. Do nothing and return false.
}
}

std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
{
// std::locale takes ownership of the pointer
std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
std::stringstream ss;
ss.imbue(loc);
ss << boost::posix_time::from_time_t(nTime);
return ss.str();
}
9 changes: 1 addition & 8 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ extern bool fLogIPs;
// Unavoidable because this is in util.h.
extern int64_t GetAdjustedTime();

inline std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
{
time_t n = nTime;
struct tm* ptmTime = gmtime(&n);
char pszTime[200];
strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
return pszTime;
}
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);

static const std::string strTimestampFormat = "%Y-%m-%d %H:%M:%S UTC";
inline std::string DateTimeStrFormat(int64_t nTime)
Expand Down
9 changes: 1 addition & 8 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1407,14 +1407,7 @@ UniValue scanforunspent(const UniValue& params, bool fHelp)
// We will place this in wallet backups as a safer location then in main data directory
fs::path exportpath;

time_t biTime;
struct tm * blTime;
time (&biTime);
blTime = localtime(&biTime);
char boTime[200];
strftime(boTime, sizeof(boTime), "%Y-%m-%dT%H-%M-%S", blTime);

std::string exportfile = params[0].get_str() + "-" + std::string(boTime) + "." + params[4].get_str();
std::string exportfile = params[0].get_str() + "-" + std::string(FormatISO8601DateTime(GetTime())) + "." + params[4].get_str();

std::string backupdir = gArgs.GetArg("-backupdir", "");

Expand Down

0 comments on commit 63a6393

Please sign in to comment.