Kernel: Shell now prints unix time with 'date' command

This commit is contained in:
Bananymous 2023-03-23 11:13:51 +02:00
parent 96579b88cf
commit 3efbe22a1b
1 changed files with 35 additions and 0 deletions

View File

@ -217,6 +217,40 @@ argument_done:
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)
{
@ -230,6 +264,7 @@ argument_done:
return BAN::Error::from_c_string("'date' does not support command line arguments");
auto time = RTC::get_current_time();
TTY_PRINTLN("{}", time);
TTY_PRINTLN("{}", unix_time(time));
}
else if (arguments.front() == "echo")
{