Skip to content

Commit 63a6393

Browse files
authored
Merge pull request #2319 from barton2526/localtime
util: Don't use gmtime() or localtime()
2 parents 36e5f5f + 9cdabec commit 63a6393

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

src/gridcoin/backup.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "util.h"
1010
#include "util/time.h"
1111

12+
#include <boost/date_time/posix_time/posix_time.hpp>
13+
1214
#include <string>
1315

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

2224
std::string GRC::GetBackupFilename(const std::string& basename, const std::string& suffix)
2325
{
24-
time_t biTime;
25-
struct tm * blTime;
26-
time (&biTime);
27-
blTime = localtime(&biTime);
28-
char boTime[200];
29-
strftime(boTime, sizeof(boTime), "%Y-%m-%dT%H-%M-%S", blTime);
3026
std::string sBackupFilename;
3127
fs::path rpath;
32-
sBackupFilename = basename + "-" + std::string(boTime);
28+
sBackupFilename = basename + "-" + std::string(FormatISO8601DateTime(GetTime()));
3329
if (!suffix.empty())
3430
sBackupFilename = sBackupFilename + "-" + suffix;
3531
rpath = GetBackupPath() / sBackupFilename;

src/logging.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "util/time.h"
99
#include "util/system.h"
1010

11+
#include <boost/date_time/posix_time/posix_time.hpp>
1112
#include <boost/iostreams/stream.hpp>
1213
#include <boost/iostreams/copy.hpp>
1314
#include <boost/iostreams/filtering_stream.hpp>
@@ -502,3 +503,13 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)
502503
return false; // archive condition was not satisfied. Do nothing and return false.
503504
}
504505
}
506+
507+
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
508+
{
509+
// std::locale takes ownership of the pointer
510+
std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
511+
std::stringstream ss;
512+
ss.imbue(loc);
513+
ss << boost::posix_time::from_time_t(nTime);
514+
return ss.str();
515+
}

src/logging.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,7 @@ extern bool fLogIPs;
3131
// Unavoidable because this is in util.h.
3232
extern int64_t GetAdjustedTime();
3333

34-
inline std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
35-
{
36-
time_t n = nTime;
37-
struct tm* ptmTime = gmtime(&n);
38-
char pszTime[200];
39-
strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
40-
return pszTime;
41-
}
34+
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);
4235

4336
static const std::string strTimestampFormat = "%Y-%m-%d %H:%M:%S UTC";
4437
inline std::string DateTimeStrFormat(int64_t nTime)

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,14 +1407,7 @@ UniValue scanforunspent(const UniValue& params, bool fHelp)
14071407
// We will place this in wallet backups as a safer location then in main data directory
14081408
fs::path exportpath;
14091409

1410-
time_t biTime;
1411-
struct tm * blTime;
1412-
time (&biTime);
1413-
blTime = localtime(&biTime);
1414-
char boTime[200];
1415-
strftime(boTime, sizeof(boTime), "%Y-%m-%dT%H-%M-%S", blTime);
1416-
1417-
std::string exportfile = params[0].get_str() + "-" + std::string(boTime) + "." + params[4].get_str();
1410+
std::string exportfile = params[0].get_str() + "-" + std::string(FormatISO8601DateTime(GetTime())) + "." + params[4].get_str();
14181411

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

0 commit comments

Comments
 (0)