AK: Add AK::Time factory method to construct from individual time fields

This commit is contained in:
Timothy Flynn 2022-01-10 22:39:33 -05:00 committed by Linus Groh
parent 548643bcc9
commit 2b5840296a
Notes: sideshowbarker 2024-07-17 21:10:07 +09:00

View file

@ -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)
{