| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #pragma once
- #include <chrono>
- #include <string>
- #include <ctime>
- namespace smartbotic::common {
- class TimeUtils {
- public:
- using Clock = std::chrono::system_clock;
- using TimePoint = Clock::time_point;
- using Duration = Clock::duration;
- // Get current timestamp in milliseconds
- static int64_t nowMs();
- // Get current timestamp in seconds
- static int64_t nowSec();
- // Get current timestamp as TimePoint
- static TimePoint now();
- // Convert milliseconds to TimePoint
- static TimePoint fromMs(int64_t ms);
- // Convert TimePoint to milliseconds
- static int64_t toMs(TimePoint tp);
- // Format timestamp as ISO 8601 string
- static std::string toIso8601(int64_t ms);
- static std::string toIso8601(TimePoint tp);
- // Parse ISO 8601 string to milliseconds
- static int64_t parseIso8601(const std::string& str);
- // Check if timestamp has expired
- static bool isExpired(int64_t expiryMs);
- static bool isExpired(TimePoint expiry);
- // Add duration to timestamp
- static int64_t addMs(int64_t timestampMs, int64_t durationMs);
- static int64_t addSec(int64_t timestampMs, int64_t durationSec);
- };
- // RAII timer for measuring durations
- class ScopedTimer {
- public:
- explicit ScopedTimer(const std::string& name = "");
- ~ScopedTimer();
- int64_t elapsedMs() const;
- void reset();
- private:
- std::string name_;
- TimeUtils::TimePoint start_;
- };
- } // namespace smartbotic::common
|