From ad16d361dc4c9730031048fbab323f3c0304f52f Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 9 Jan 2023 03:30:13 +0200 Subject: [PATCH] Implemented "ConvertFromUnix" (not sure if it's right but I guess it works?) --- Core/Time.cpp | 33 +++++++++++++++++++++++++++++++++ include/time.hpp | 3 ++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Core/Time.cpp b/Core/Time.cpp index 96c9ac3..142c711 100644 --- a/Core/Time.cpp +++ b/Core/Time.cpp @@ -1,4 +1,5 @@ #include +#include #include namespace Time @@ -38,4 +39,36 @@ namespace Time #endif return tm; } + + Clock ConvertFromUnix(int Timestamp) + { + + Clock result; + if (Timestamp == 0) + return result; + + int SecondsSinceYearStart = Timestamp % (60 * 60 * 24 * 365); + + result.Year = Timestamp / (60 * 60 * 24 * 365); + result.Month = SecondsSinceYearStart / (60 * 60 * 24 * 30); + result.Day = SecondsSinceYearStart / (60 * 60 * 24); + result.Hour = SecondsSinceYearStart / (60 * 60); + result.Minute = SecondsSinceYearStart / 60; + result.Second = SecondsSinceYearStart; + +#ifdef DEBUG + int DaysInYear; + if (result.Year % 4 != 0) + DaysInYear = 365; + else if (result.Year % 100 != 0) + DaysInYear = 366; + else if (result.Year % 400 == 0) + DaysInYear = 366; + else + DaysInYear = 365; + debug("Days in year: %d", DaysInYear); +#endif + + return result; + } } diff --git a/include/time.hpp b/include/time.hpp index 8356746..18007d9 100644 --- a/include/time.hpp +++ b/include/time.hpp @@ -7,11 +7,12 @@ namespace Time { struct Clock { - uint64_t Year, Month, Day, Hour, Minute, Second; + int Year, Month, Day, Hour, Minute, Second; uint64_t Counter; }; Clock ReadClock(); + Clock ConvertFromUnix(int Timestamp); class time {