summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-10 22:39:33 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-11 23:56:35 +0100
commit2b5840296a86dfd7096596b675218ca92e1660f3 (patch)
tree8dcda71333705f599ec18de3ca8d7ca2b52de614 /AK
parent548643bcc9d196c2b10a29e7ced4e9b5044d88b0 (diff)
downloadserenity-2b5840296a86dfd7096596b675218ca92e1660f3.zip
AK: Add AK::Time factory method to construct from individual time fields
Diffstat (limited to 'AK')
-rw-r--r--AK/Time.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/AK/Time.h b/AK/Time.h
index df4f5b3045..f1a3f28b1d 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -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)
{