diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-01-10 22:39:33 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-11 23:56:35 +0100 |
commit | 2b5840296a86dfd7096596b675218ca92e1660f3 (patch) | |
tree | 8dcda71333705f599ec18de3ca8d7ca2b52de614 /AK | |
parent | 548643bcc9d196c2b10a29e7ced4e9b5044d88b0 (diff) | |
download | serenity-2b5840296a86dfd7096596b675218ca92e1660f3.zip |
AK: Add AK::Time factory method to construct from individual time fields
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Time.h | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -136,6 +136,24 @@ private: } public: + [[nodiscard]] constexpr static Time from_timestamp(u16 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u8 millisecond) + { + constexpr auto milliseconds_per_day = 86'400'000; + constexpr auto milliseconds_per_hour = 3'600'000; + constexpr auto milliseconds_per_minute = 60'000; + constexpr auto milliseconds_per_second = 1'000; + + i64 milliseconds_since_epoch = days_since_epoch(year, month, day); + milliseconds_since_epoch *= milliseconds_per_day; + + milliseconds_since_epoch += hour * milliseconds_per_hour; + milliseconds_since_epoch += minute * milliseconds_per_minute; + milliseconds_since_epoch += second * milliseconds_per_second; + milliseconds_since_epoch += millisecond; + + return from_milliseconds(milliseconds_since_epoch); + } + [[nodiscard]] constexpr static Time from_seconds(i64 seconds) { return Time(seconds, 0); } [[nodiscard]] constexpr static Time from_nanoseconds(i64 nanoseconds) { |