Fixed ConvertFromUnix()

This commit is contained in:
Alex 2023-02-25 04:52:09 +02:00
parent 744895afd6
commit 4d279f0d12
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 37 additions and 22 deletions

View File

@ -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<int>(Days) + 1;
result.Hour = static_cast<int>(Hours % 24);
result.Minute = static_cast<int>(Minutes % 60);
result.Second = static_cast<int>(Seconds % 60);
result.Counter = static_cast<uint64_t>(Timestamp);
return result;
}
}

View File

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