diff --git a/Core/Time.cpp b/Core/Time.cpp index 5be77b5..dd505c0 100644 --- a/Core/Time.cpp +++ b/Core/Time.cpp @@ -43,31 +43,46 @@ namespace Time Clock ConvertFromUnix(int Timestamp) { Clock result; - if (Timestamp == 0) - return result; - int SecondsSinceYearStart = Timestamp % (60 * 60 * 24 * 365); + uint64_t Seconds = Timestamp; + uint64_t Minutes = Seconds / 60; + uint64_t Hours = Minutes / 60; + uint64_t Days = Hours / 24; - 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; + result.Year = 1970; + while (Days >= 365) + { + if (result.Year % 4 == 0 && (result.Year % 100 != 0 || result.Year % 400 == 0)) + { + if (Days >= 366) + { + Days -= 366; + result.Year++; + } + else + break; + } + else + { + Days -= 365; + result.Year++; + } + } -#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 + int DaysInMonth[] = {31, result.Year % 4 == 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + for (result.Month = 0; result.Month < 12; result.Month++) + { + if (Days < DaysInMonth[result.Month]) + break; + Days -= DaysInMonth[result.Month]; + } + result.Month++; + result.Day = static_cast(Days) + 1; + result.Hour = static_cast(Hours % 24); + result.Minute = static_cast(Minutes % 60); + result.Second = static_cast(Seconds % 60); + result.Counter = static_cast(Timestamp); return result; } } diff --git a/Network/NetworkController.cpp b/Network/NetworkController.cpp index 2a87d34..fce0329 100644 --- a/Network/NetworkController.cpp +++ b/Network/NetworkController.cpp @@ -121,7 +121,7 @@ namespace NetworkInterfaceManager udp->Bind(NTP_Socket, ntp); int UnixTimestamp = ntp->ReadTime(); Time::Clock time = Time::ConvertFromUnix(UnixTimestamp); - DbgWriteScreen("NTP: %d - %d.%d.%d %d:%d:%d", UnixTimestamp, + DbgWriteScreen("NTP: %d - %d.%d.%d %d:%d:%d", time.Counter, time.Day, time.Month, time.Year, time.Hour, time.Minute, time.Second); TaskManager->Sleep(200);