Kernel: Move get_unix_time to RTC namespace

This commit is contained in:
Bananymous 2023-03-23 18:14:51 +02:00
parent 5831c72aad
commit 15037bfc7a
3 changed files with 38 additions and 36 deletions

View File

@ -6,5 +6,6 @@ namespace RTC
{ {
BAN::Time get_current_time(); BAN::Time get_current_time();
uint64_t get_unix_time();
} }

View File

@ -87,4 +87,41 @@ namespace RTC
return time; return time;
} }
static bool is_leap_year(uint64_t year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
static uint64_t leap_days_since_epoch(const BAN::Time& time)
{
uint64_t leap_years = 0;
for (int year = 1970; year < time.year; year++)
if (is_leap_year(year))
leap_years++;
if (is_leap_year(time.year))
if (time.month >= 3 || (time.month == 2 && time.day == 29))
leap_years++;
return leap_years;
}
uint64_t get_unix_time()
{
auto time = get_current_time();
uint64_t month_days[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
uint64_t years = time.year - 1970;
uint64_t days = years * 365 + month_days[time.month - 1] + time.day + leap_days_since_epoch(time) - 1;
uint64_t hours = days * 24 + time.hour;
uint64_t minutes = hours * 60 + time.minute;
uint64_t seconds = minutes * 60 + time.second;
return seconds;
}
} }

View File

@ -217,41 +217,6 @@ argument_done:
0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
}; };
static bool is_leap_year(uint64_t year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
static uint64_t leap_days_since_epoch(const BAN::Time& time)
{
uint64_t leap_years = 0;
for (int year = 1970; year < time.year; year++)
if (is_leap_year(year))
leap_years++;
if (is_leap_year(time.year))
if (time.month >= 3 || (time.month == 2 && time.day == 29))
leap_years++;
return leap_years;
}
static uint64_t unix_time(const BAN::Time& time)
{
uint64_t month_days[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
uint64_t years = time.year - 1970;
uint64_t days = years * 365 + month_days[time.month - 1] + time.day + leap_days_since_epoch(time) - 1;
uint64_t hours = days * 24 + time.hour;
uint64_t minutes = hours * 60 + time.minute;
uint64_t seconds = minutes * 60 + time.second;
return seconds;
}
BAN::ErrorOr<void> Shell::process_command(const BAN::Vector<BAN::String>& arguments) BAN::ErrorOr<void> Shell::process_command(const BAN::Vector<BAN::String>& arguments)
{ {
if (arguments.empty()) if (arguments.empty())
@ -264,7 +229,6 @@ argument_done:
return BAN::Error::from_c_string("'date' does not support command line arguments"); return BAN::Error::from_c_string("'date' does not support command line arguments");
auto time = RTC::get_current_time(); auto time = RTC::get_current_time();
TTY_PRINTLN("{}", time); TTY_PRINTLN("{}", time);
TTY_PRINTLN("{}", unix_time(time));
} }
else if (arguments.front() == "echo") else if (arguments.front() == "echo")
{ {