Skip to content

Commit d5179ec

Browse files
committed
refactor: move away from gmtime
The next commit moves inherited code away from `gmtime` and removes it altogether.
1 parent 3a71354 commit d5179ec

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

src/util/time.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,12 @@ std::string FormatISO8601Date(int64_t nTime) {
135135
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
136136
}
137137

138-
std::string FormatISO8601Time(int64_t nTime) {
139-
struct tm ts;
140-
time_t time_val = nTime;
141-
#ifdef HAVE_GMTIME_R
142-
gmtime_r(&time_val, &ts);
143-
#else
144-
gmtime_s(&ts, &time_val);
145-
#endif
146-
return strprintf("%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec);
138+
std::string FormatISO8601Time(int64_t nTime)
139+
{
140+
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
141+
const auto days{std::chrono::floor<std::chrono::days>(secs)};
142+
const std::chrono::hh_mm_ss hms{secs - days};
143+
return strprintf("%02i:%02i:%02iZ", hms.hours().count(), hms.minutes().count(), hms.seconds().count());
147144
}
148145

149146
int64_t ParseISO8601DateTime(const std::string& str)

src/wallet/wallet.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,15 +3262,13 @@ bool CWallet::AutoBackupWallet(const fs::path& wallet_path, bilingual_str& error
32623262
}
32633263

32643264
// Create backup of the ...
3265-
struct tm ts;
3266-
time_t time_val = GetTime();
3267-
#ifdef HAVE_GMTIME_R
3268-
gmtime_r(&time_val, &ts);
3269-
#else
3270-
gmtime_s(&ts, &time_val);
3271-
#endif
3272-
std::string dateTimeStr = strprintf(".%04i-%02i-%02i-%02i-%02i",
3273-
ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min);
3265+
std::string dateTimeStr = [&]() {
3266+
const std::chrono::sys_seconds secs{GetTime<std::chrono::seconds>()};
3267+
const auto days{std::chrono::floor<std::chrono::days>(secs)};
3268+
const std::chrono::year_month_day ymd{days};
3269+
const std::chrono::hh_mm_ss hms{secs - days};
3270+
return strprintf(".%04i-%02u-%02u-%02i-%02i", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()}, hms.hours().count(), hms.minutes().count());
3271+
}();
32743272

32753273
if (wallet_path.empty()) {
32763274
// ... opened wallet

0 commit comments

Comments
 (0)