Kernel/BAN: Make Time a struct in BAN and add formatter to it

This commit is contained in:
Bananymous 2022-12-13 12:37:26 +02:00
parent 3cda527987
commit 52c793bb5c
4 changed files with 38 additions and 18 deletions

30
BAN/include/BAN/Time.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <BAN/Formatter.h>
#include <stdint.h>
namespace BAN
{
struct Time
{
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
int year;
};
}
namespace BAN::Formatter
{
template<void(*PUTC_LIKE)(char)> void print_argument_impl(const Time& time, const ValueFormat&)
{
print<PUTC_LIKE>("{2}:{2}:{2} {2}.{2}.{4}", time.hour, time.minute, time.second, time.day, time.month, time.year);
}
}

View File

@ -1,20 +1,10 @@
#pragma once #pragma once
#include <stdint.h> #include <BAN/Time.h>
namespace RTC namespace RTC
{ {
struct Time BAN::Time GetCurrentTime();
{
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
int year;
};
Time GetCurrentTime();
} }

View File

@ -33,7 +33,7 @@ namespace RTC
return IO::inb(CMOS_DATA); return IO::inb(CMOS_DATA);
} }
static void get_time(Time& out) static void get_time(BAN::Time& out)
{ {
out.second = get_rtc_register(CMOS_REGISTER_SECOND); out.second = get_rtc_register(CMOS_REGISTER_SECOND);
out.minute = get_rtc_register(CMOS_REGISTER_MINUTE); out.minute = get_rtc_register(CMOS_REGISTER_MINUTE);
@ -43,10 +43,10 @@ namespace RTC
out.year = get_rtc_register(CMOS_REGISTER_YEAR); out.year = get_rtc_register(CMOS_REGISTER_YEAR);
} }
Time GetCurrentTime() BAN::Time GetCurrentTime()
{ {
Time last_time = {}; BAN::Time last_time = {};
Time time = {}; BAN::Time time = {};
while (get_update_in_progress()) while (get_update_in_progress())
continue; continue;
@ -54,7 +54,7 @@ namespace RTC
get_time(time); get_time(time);
last_time.second = time.second + 1; last_time.second = time.second + 1;
while (memcmp(&last_time, &time, sizeof(Time))) while (memcmp(&last_time, &time, sizeof(BAN::Time)))
{ {
last_time = time; last_time = time;
get_time(time); get_time(time);

View File

@ -103,7 +103,7 @@ void kernel_main(multiboot_info_t* mbi, uint32_t magic)
return; return;
auto time = RTC::GetCurrentTime(); auto time = RTC::GetCurrentTime();
kprintln("Today is {2}:{2}:{2} {2}.{2}.{4}", time.hour, time.minute, time.second, time.day, time.month, time.year); kprintln("Today is {}", time);
kprintln("Hello from the kernel!"); kprintln("Hello from the kernel!");