From 7ca6cd61bec30d6b12bb2934706dfabcbf9aa2d4 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 23 Mar 2023 18:14:51 +0200 Subject: [PATCH] Kernel: Move get_unix_time to RTC namespace --- kernel/include/kernel/RTC.h | 1 + kernel/kernel/RTC.cpp | 37 +++++++++++++++++++++++++++++++++++++ kernel/kernel/Shell.cpp | 36 ------------------------------------ 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/kernel/include/kernel/RTC.h b/kernel/include/kernel/RTC.h index af5f6b11..ffedbd37 100644 --- a/kernel/include/kernel/RTC.h +++ b/kernel/include/kernel/RTC.h @@ -6,5 +6,6 @@ namespace RTC { BAN::Time get_current_time(); + uint64_t get_unix_time(); } \ No newline at end of file diff --git a/kernel/kernel/RTC.cpp b/kernel/kernel/RTC.cpp index 5da86c98..ddabafb5 100644 --- a/kernel/kernel/RTC.cpp +++ b/kernel/kernel/RTC.cpp @@ -87,4 +87,41 @@ namespace RTC 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; + } + } \ No newline at end of file diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index 71d59dfb..13ce3703 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -217,41 +217,6 @@ 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) { if (arguments.empty()) @@ -264,7 +229,6 @@ 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") {