plugify 1.2.6
Loading...
Searching...
No Matches
date_time.hpp
1#pragma once
2
3#include <chrono>
4#include <iomanip>
5#include <cmath>
6
7namespace plugify {
8 using namespace std::chrono_literals;
9
10 class DateTime {
11 public:
12 DateTime() = default;
13
20 template<typename Rep, typename Period>
21 constexpr DateTime(const std::chrono::duration<Rep, Period>& duration) noexcept : _value{std::chrono::duration_cast<std::chrono::microseconds>(duration).count()} {}
22
29 template<typename T = float>
30 constexpr static DateTime Seconds(const T& seconds) noexcept { return {std::chrono::duration<T>(seconds)}; }
31
38 template<typename T = double>
39 constexpr static DateTime Milliseconds(const T& milliseconds) noexcept { return {std::chrono::duration<T, std::micro>(milliseconds)}; }
40
47 template<typename T = uint64_t>
48 constexpr static DateTime Microseconds(const T& microseconds) noexcept { return {std::chrono::duration<T, std::micro>(microseconds)}; }
49
55 template<typename T = float>
56 constexpr auto AsSeconds() const noexcept { return static_cast<T>(_value.count()) / static_cast<T>(1000000); }
57
63 template<typename T = double>
64 constexpr auto AsMilliseconds() const noexcept { return static_cast<T>(_value.count()) / static_cast<T>(1000); }
65
71 template<typename T = uint64_t>
72 constexpr auto AsMicroseconds() const noexcept { return static_cast<T>(_value.count()); }
73
78 static DateTime Now() noexcept {
79 return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - localEpoch);
80 }
81
87 static std::string Get(std::string_view format = "%Y-%m-%d %H:%M:%S") {
88 auto now = std::chrono::system_clock::now();
89 auto t = std::chrono::system_clock::to_time_t(now);
90 std::tm time{};
91#if _WIN32
92 localtime_s(&time, &t); // Windows-specific
93#else
94 localtime_r(&t, &time); // POSIX-compliant
95#endif
96 std::string buffer(80, '\0');
97 size_t res = std::strftime(buffer.data(), buffer.size(), format.data(), &time);
98 if (!res)
99 return "strftime error";
100 buffer.resize(res);
101 return buffer;
102 }
103
110 template<typename Rep, typename Period>
111 constexpr explicit operator std::chrono::duration<Rep, Period>() const noexcept {
112 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(_value);
113 }
114
115 //constexpr auto operator<=>(const DateTime& rhs) const { return _value <=> rhs._value; }
121 constexpr bool operator==(const DateTime& rhs) const noexcept { return _value == rhs._value; }
127 constexpr bool operator!=(const DateTime& rhs) const noexcept { return _value != rhs._value; }
133 constexpr bool operator<(const DateTime& rhs) const noexcept { return _value < rhs._value; }
139 constexpr bool operator<=(const DateTime& rhs) const noexcept { return _value <= rhs._value; }
145 constexpr bool operator>(const DateTime& rhs) const noexcept { return _value > rhs._value; }
151 constexpr bool operator>=(const DateTime& rhs) const noexcept { return _value >= rhs._value; }
156 constexpr DateTime operator-() const noexcept { return { -_value}; }
157
164 constexpr friend DateTime operator+(const DateTime& lhs, const DateTime& rhs) noexcept { return lhs._value + rhs._value; }
171 constexpr friend DateTime operator-(const DateTime& lhs, const DateTime& rhs) noexcept { return lhs._value - rhs._value; }
178 constexpr friend DateTime operator*(const DateTime& lhs, float rhs) noexcept { return lhs._value * rhs; }
185 constexpr friend DateTime operator*(const DateTime& lhs, int64_t rhs) noexcept { return lhs._value * rhs; }
192 constexpr friend DateTime operator*(float lhs, const DateTime& rhs) noexcept { return rhs * lhs; }
199 constexpr friend DateTime operator*(int64_t lhs, const DateTime& rhs) noexcept { return rhs * lhs; }
206 constexpr friend DateTime operator/(const DateTime& lhs, float rhs) noexcept { return lhs._value / rhs; }
213 constexpr friend DateTime operator/(const DateTime& lhs, int64_t rhs) noexcept { return lhs._value / rhs; }
220 constexpr friend double operator/(const DateTime& lhs, const DateTime& rhs) noexcept { return static_cast<double>(lhs._value.count()) / static_cast<double>(rhs._value.count()); }
221
229 template<typename Period = std::ratio<1, 1>>
230 constexpr friend double operator%(const DateTime& lhs, const DateTime& rhs) {
231 return std::modf(std::chrono::duration_cast<std::chrono::duration<double, Period>>(lhs._value), std::chrono::duration_cast<std::chrono::duration<double, Period>>(rhs._value));
232 }
233
239 constexpr DateTime& operator+=(const DateTime& rhs) noexcept { return *this = *this + rhs; }
245 constexpr DateTime& operator-=(const DateTime& rhs) noexcept { return *this = *this - rhs; }
251 constexpr DateTime& operator*=(float rhs) noexcept { return *this = *this * rhs; }
257 constexpr DateTime& operator*=(int64_t rhs) noexcept{ return *this = *this * rhs; }
263 constexpr DateTime& operator/=(float rhs) noexcept { return *this = *this / rhs; }
269 constexpr DateTime& operator/=(int64_t rhs) noexcept { return *this = *this / rhs; }
270
271 private:
272 std::chrono::microseconds _value{};
273
274 static inline const std::chrono::high_resolution_clock::time_point localEpoch = std::chrono::high_resolution_clock::now();
275 };
276}
constexpr friend DateTime operator*(const DateTime &lhs, int64_t rhs) noexcept
Multiplies a DateTime object by an integer value.
constexpr friend DateTime operator/(const DateTime &lhs, int64_t rhs) noexcept
Divides a DateTime object by an integer value.
constexpr friend DateTime operator/(const DateTime &lhs, float rhs) noexcept
Divides a DateTime object by a floating-point value.
constexpr bool operator>=(const DateTime &rhs) const noexcept
Compares if one DateTime object is greater than or equal to another.
constexpr auto AsMicroseconds() const noexcept
Converts the time duration to microseconds.
Definition date_time.hpp:72
static constexpr DateTime Microseconds(const T &microseconds) noexcept
Creates a DateTime object representing microseconds.
Definition date_time.hpp:48
static constexpr DateTime Milliseconds(const T &milliseconds) noexcept
Creates a DateTime object representing milliseconds.
Definition date_time.hpp:39
constexpr bool operator<=(const DateTime &rhs) const noexcept
Compares if one DateTime object is less than or equal to another.
constexpr auto AsSeconds() const noexcept
Converts the time duration to seconds.
Definition date_time.hpp:56
constexpr DateTime operator-() const noexcept
Negates the DateTime value.
constexpr DateTime & operator*=(float rhs) noexcept
Multiplies this DateTime object by a floating-point value.
constexpr DateTime(const std::chrono::duration< Rep, Period > &duration) noexcept
Constructs a DateTime object from a duration.
Definition date_time.hpp:21
constexpr friend DateTime operator+(const DateTime &lhs, const DateTime &rhs) noexcept
Adds two DateTime objects.
constexpr DateTime & operator+=(const DateTime &rhs) noexcept
Adds another DateTime object to this one.
constexpr friend double operator%(const DateTime &lhs, const DateTime &rhs)
Computes the modulo (remainder) of one DateTime object divided by another.
static std::string Get(std::string_view format="%Y-%m-%d %H:%M:%S")
Gets the current system time formatted as a string.
Definition date_time.hpp:87
constexpr DateTime & operator/=(float rhs) noexcept
Divides this DateTime object by a floating-point value.
constexpr bool operator<(const DateTime &rhs) const noexcept
Compares if one DateTime object is less than another.
constexpr DateTime & operator*=(int64_t rhs) noexcept
Multiplies this DateTime object by an integer value.
constexpr DateTime & operator/=(int64_t rhs) noexcept
Divides this DateTime object by an integer value.
static DateTime Now() noexcept
Gets the current time since a local epoch.
Definition date_time.hpp:78
static constexpr DateTime Seconds(const T &seconds) noexcept
Creates a DateTime object representing seconds.
Definition date_time.hpp:30
constexpr friend DateTime operator*(int64_t lhs, const DateTime &rhs) noexcept
Multiplies an integer value by a DateTime object.
constexpr bool operator!=(const DateTime &rhs) const noexcept
Compares if two DateTime objects are not equal.
constexpr friend double operator/(const DateTime &lhs, const DateTime &rhs) noexcept
Divides one DateTime object by another.
constexpr friend DateTime operator*(const DateTime &lhs, float rhs) noexcept
Multiplies a DateTime object by a floating-point value.
constexpr bool operator==(const DateTime &rhs) const noexcept
Compares if two DateTime objects are equal.
constexpr auto AsMilliseconds() const noexcept
Converts the time duration to milliseconds.
Definition date_time.hpp:64
constexpr DateTime & operator-=(const DateTime &rhs) noexcept
Subtracts another DateTime object from this one.
constexpr friend DateTime operator-(const DateTime &lhs, const DateTime &rhs) noexcept
Subtracts one DateTime object from another.
constexpr bool operator>(const DateTime &rhs) const noexcept
Compares if one DateTime object is greater than another.
constexpr friend DateTime operator*(float lhs, const DateTime &rhs) noexcept
Multiplies a floating-point value by a DateTime object.