mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Add RelativeTime::fromStartup
This commit is contained in:
@@ -60,6 +60,9 @@ public:
|
|||||||
/** Destructor. */
|
/** Destructor. */
|
||||||
~RelativeTime() noexcept;
|
~RelativeTime() noexcept;
|
||||||
|
|
||||||
|
/** Returns the amount of time since the process was started. */
|
||||||
|
static RelativeTime fromStartup ();
|
||||||
|
|
||||||
//==============================================================================
|
//==============================================================================
|
||||||
/** Creates a new RelativeTime object representing a number of milliseconds.
|
/** Creates a new RelativeTime object representing a number of milliseconds.
|
||||||
@see seconds, minutes, hours, days, weeks
|
@see seconds, minutes, hours, days, weeks
|
||||||
@@ -145,39 +148,42 @@ public:
|
|||||||
String getDescription (const String& returnValueForZeroTime = "0") const;
|
String getDescription (const String& returnValueForZeroTime = "0") const;
|
||||||
std::string to_string () const;
|
std::string to_string () const;
|
||||||
|
|
||||||
//==============================================================================
|
RelativeTime operator+ (double seconds) const noexcept
|
||||||
|
{ return RelativeTime (numSeconds + seconds); }
|
||||||
|
|
||||||
|
RelativeTime operator- (double seconds) const noexcept
|
||||||
|
{ return RelativeTime (numSeconds - seconds); }
|
||||||
|
|
||||||
/** Adds another RelativeTime to this one. */
|
/** Adds another RelativeTime to this one. */
|
||||||
RelativeTime operator+= (RelativeTime timeToAdd) noexcept;
|
RelativeTime operator+= (RelativeTime timeToAdd) noexcept;
|
||||||
|
|
||||||
/** Subtracts another RelativeTime from this one. */
|
/** Subtracts another RelativeTime from this one. */
|
||||||
RelativeTime operator-= (RelativeTime timeToSubtract) noexcept;
|
RelativeTime operator-= (RelativeTime timeToSubtract) noexcept;
|
||||||
|
|
||||||
/** Adds a number of seconds to this time. */
|
/** Adds a number of seconds to this time. */
|
||||||
RelativeTime operator+= (double secondsToAdd) noexcept;
|
RelativeTime operator+= (double secondsToAdd) noexcept;
|
||||||
|
|
||||||
/** Subtracts a number of seconds from this time. */
|
/** Subtracts a number of seconds from this time. */
|
||||||
RelativeTime operator-= (double secondsToSubtract) noexcept;
|
RelativeTime operator-= (double secondsToSubtract) noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//==============================================================================
|
|
||||||
double numSeconds;
|
double numSeconds;
|
||||||
};
|
};
|
||||||
|
|
||||||
//==============================================================================
|
//------------------------------------------------------------------------------
|
||||||
/** Compares two RelativeTimes. */
|
|
||||||
bool operator== (RelativeTime t1, RelativeTime t2) noexcept;
|
bool operator== (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
/** Compares two RelativeTimes. */
|
|
||||||
bool operator!= (RelativeTime t1, RelativeTime t2) noexcept;
|
bool operator!= (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
/** Compares two RelativeTimes. */
|
|
||||||
bool operator> (RelativeTime t1, RelativeTime t2) noexcept;
|
bool operator> (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
/** Compares two RelativeTimes. */
|
|
||||||
bool operator< (RelativeTime t1, RelativeTime t2) noexcept;
|
bool operator< (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
/** Compares two RelativeTimes. */
|
|
||||||
bool operator>= (RelativeTime t1, RelativeTime t2) noexcept;
|
bool operator>= (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
/** Compares two RelativeTimes. */
|
|
||||||
bool operator<= (RelativeTime t1, RelativeTime t2) noexcept;
|
bool operator<= (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
|
|
||||||
//==============================================================================
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
/** Adds two RelativeTimes together. */
|
/** Adds two RelativeTimes together. */
|
||||||
RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept;
|
RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
|
|
||||||
/** Subtracts two RelativeTimes. */
|
/** Subtracts two RelativeTimes. */
|
||||||
RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept;
|
RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept;
|
||||||
|
|
||||||
@@ -190,4 +196,3 @@ inline std::ostream& operator<< (std::ostream& os, RelativeTime const& diff)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -256,3 +256,71 @@ std::string RelativeTime::to_string () const
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if BEAST_WINDOWS
|
||||||
|
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
RelativeTime RelativeTime::fromStartup ()
|
||||||
|
{
|
||||||
|
ULONGLONG ticks (GetTickCount64());
|
||||||
|
|
||||||
|
return RelativeTime (ticks / 1000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
// Converts a timespec to a RelativeTme
|
||||||
|
static RelativeTime toRelativeTime (timespec const& ts)
|
||||||
|
{
|
||||||
|
return RelativeTime (ts.tv_sec +
|
||||||
|
ts.tv_nsec / 1000000000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Records and returns the time from process startup
|
||||||
|
static RelativeTime getStartupTime()
|
||||||
|
{
|
||||||
|
struct StartupTime
|
||||||
|
{
|
||||||
|
StartupTime ()
|
||||||
|
{ clock_gettime (CLOCK_MONOTONIC, &ts); }
|
||||||
|
timespec ts;
|
||||||
|
};
|
||||||
|
|
||||||
|
static StartupTime startupTime;
|
||||||
|
|
||||||
|
return toRelativeTime (startupTime.ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Used to call getStartupTime as early as possible
|
||||||
|
struct StartupTimeStaticInitializer
|
||||||
|
{
|
||||||
|
StartupTimeStaticInitializer ()
|
||||||
|
{ getStartupTime(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
static StartupTimeStaticInitializer startupTimeStaticInitializer;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
RelativeTime RelativeTime::fromStartup ()
|
||||||
|
{
|
||||||
|
timespec ts;
|
||||||
|
clock_gettime (CLOCK_MONOTONIC, &ts);
|
||||||
|
|
||||||
|
return detail::toRelativeTime (ts) - detail::getStartupTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user