ulib Time utilities, expose time() syscall

This commit is contained in:
2025-10-16 14:15:27 +02:00
parent 702f0ddf87
commit 7445689010
6 changed files with 50 additions and 2 deletions

31
ulib/time/time.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <sysdefs/time.h>
bool time_isleap(uint32_t y) {
return ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0));
}
const uint16_t days_before_month[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
timeunix_t time_tounix(Time *time) {
uint64_t days = 0;
for (uint32_t y = 1970; y < time->year; y++) {
days += time_isleap(y) ? 366 : 365;
}
days += days_before_month[time->month - 1];
if (time->month > 2 && time_isleap(time->year)) {
days += 1;
}
days += time->day - 1;
uint64_t secs = days * 86400;
secs += time->hour * 3600;
secs += time->minute * 60;
secs += time->second;
return secs;
}

8
ulib/time/time.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef ULIB_TIME_TIME_H_
#define ULIB_TIME_TIME_H_
#include <sysdefs/time.h>
timeunix_t time_tounix(Time *time);
#endif // ULIB_TIME_TIME_H_