Skip to content

Commit 481e48a

Browse files
committed
util: Add GetTimeMillis convenience wrapper for chrono timestamps
Add a convenience wrapper function for obtaining the current system time in milliseconds. This uses the modern std::chrono library internally while providing a shorter, more readable interface for code that needs millisecond precision timestamps. This improves code readability in areas like the TrafficGraphWidget that work with millisecond timestamps while maintaining compatibility with Bitcoin Core's move toward using std::chrono-based time handling.
1 parent 7fd4745 commit 481e48a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/util/time.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ void MockableSteadyClock::ClearMockTime()
7575

7676
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
7777

78+
int64_t GetTimeMillis()
79+
{
80+
return std::chrono::duration_cast<std::chrono::milliseconds>(
81+
std::chrono::system_clock::now().time_since_epoch()
82+
).count();
83+
}
84+
7885
std::string FormatISO8601DateTime(int64_t nTime)
7986
{
8087
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};

src/util/time.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ using MillisecondsDouble = std::chrono::duration<double, std::chrono::millisecon
9696
*/
9797
int64_t GetTime();
9898

99+
/**
100+
* Convenience wrapper for getting current time in milliseconds.
101+
* Internally uses std::chrono::system_clock for consistency with other time functions.
102+
*/
103+
int64_t GetTimeMillis();
104+
99105
/**
100106
* DEPRECATED
101107
* Use SetMockTime with chrono type

0 commit comments

Comments
 (0)