Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarTime.hpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
commit6352e8e3196f78388b6c771073f9e03eaa612673 (patch)
treee23772f79a7fbc41bc9108951e9e136857484bf4 /source/core/StarTime.hpp
parent6741a057e5639280d85d0f88ba26f000baa58f61 (diff)
everything everywhere
all at once
Diffstat (limited to 'source/core/StarTime.hpp')
-rw-r--r--source/core/StarTime.hpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/source/core/StarTime.hpp b/source/core/StarTime.hpp
new file mode 100644
index 0000000..e4dd274
--- /dev/null
+++ b/source/core/StarTime.hpp
@@ -0,0 +1,108 @@
+#ifndef STAR_TIME_HPP
+#define STAR_TIME_HPP
+
+#include "StarThread.hpp"
+
+namespace Star {
+
+STAR_CLASS(Clock);
+STAR_CLASS(Timer);
+
+namespace Time {
+ double timeSinceEpoch();
+ int64_t millisecondsSinceEpoch();
+
+ double monotonicTime();
+ int64_t monotonicMilliseconds();
+
+ // Pretty print a duration of time (In days, hours, minutes, seconds, and milliseconds)
+ String printDuration(double time);
+
+ // Pretty print a given date and time
+ String printDateAndTime(int64_t epochTicks, String format = "<year>-<month>-<day> <hours>:<minutes>:<seconds>.<millis>");
+ String printCurrentDateAndTime(String format = "<year>-<month>-<day> <hours>:<minutes>:<seconds>.<millis>");
+
+ // Ticks since unix epoch
+ int64_t epochTicks();
+ // Epoch ticks per second, static throughout application lifetime.
+ int64_t epochTickFrequency();
+
+ // Ticks since unspecified time before program start
+ int64_t monotonicTicks();
+ // Monotonic ticks per second, static throughout application lifetime.
+ int64_t monotonicTickFrequency();
+
+ double ticksToSeconds(int64_t ticks, int64_t tickFrequency);
+ int64_t ticksToMilliseconds(int64_t ticks, int64_t tickFrequency);
+ int64_t secondsToTicks(double seconds, int64_t tickFrequency);
+ int64_t millisecondsToTicks(int64_t milliseconds, int64_t tickFrequency);
+}
+
+// Keeps track of elapsed real time since a given moment. Guaranteed
+// monotonically increasing and thread safe.
+class Clock {
+public:
+ explicit Clock(bool start = true);
+
+ Clock(Clock const& clock);
+
+ Clock& operator=(Clock const& clock);
+
+ // Resets clock to 0 time
+ void reset();
+
+ void stop();
+ void start();
+
+ bool running() const;
+
+ double time() const;
+ int64_t milliseconds() const;
+
+ // Override actual elapsed time with the given time.
+ void setTime(double time);
+ void setMilliseconds(int64_t millis);
+
+ // Warp the clock backwards or forwards
+ void adjustTime(double timeAdjustment);
+ void adjustMilliseconds(int64_t millisAdjustment);
+
+private:
+ void updateElapsed() const;
+
+ mutable RecursiveMutex m_mutex;
+ mutable int64_t m_elapsedTicks;
+ mutable Maybe<int64_t> m_lastTicks;
+ bool m_running;
+};
+
+// An instance of Clock that counts down a given amount of time
+class Timer : private Clock {
+public:
+ static Timer withTime(double timeLeft, bool start = true);
+ static Timer withMilliseconds(int64_t millis, bool start = true);
+
+ // Constructs a stopped timer whose time is up.
+ Timer();
+ Timer(Timer const& timer);
+
+ // Start the timer with the given time left.
+ void restart(double timeLeft);
+ void restartWithMilliseconds(int64_t millisecondsLeft);
+
+ // Time remaining on the timer. If negative is true, will return negative
+ // time values after the timer is up, if false it stops at zero.
+ double timeLeft(bool negative = false) const;
+ int64_t millisecondsLeft(bool negative = false) const;
+
+ // Is the time remaining <= 0.0?
+ bool timeUp() const;
+
+ using Clock::stop;
+ using Clock::start;
+ using Clock::running;
+};
+
+}
+
+#endif