From 3efbe22a1becd8add86ca943b94b708abae90eb6 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 23 Mar 2023 11:13:51 +0200 Subject: [PATCH] Kernel: Shell now prints unix time with 'date' command --- kernel/kernel/Shell.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index a95bcee3..71d59dfb 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -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 Shell::process_command(const BAN::Vector& 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") {