libc: Add mktime()

This commit is contained in:
apio 2022-10-30 20:46:25 +01:00
parent 45f40a31d6
commit aabff7a1d3
2 changed files with 14 additions and 0 deletions

View File

@ -71,6 +71,9 @@ extern "C"
/* Fills buf with a string representation of time. Thread-safe. */ /* Fills buf with a string representation of time. Thread-safe. */
char* ctime_r(const time_t* time, char buf[26]); char* ctime_r(const time_t* time, char buf[26]);
/* Returns the UNIX timestamp representation of the broken-down time in the time structure. */
time_t mktime(struct tm* time);
/* Fills str with a formatted string representation of time according to the format string. */ /* Fills str with a formatted string representation of time according to the format string. */
size_t strftime(char* str, size_t max, const char* format, const struct tm* time); size_t strftime(char* str, size_t max, const char* format, const struct tm* time);

View File

@ -30,6 +30,12 @@ static int day_of_week(int year, int mon, int day)
return (year + year / 4 - year / 100 + year / 400 + t[mon - 1] + day) % 7; return (year + year / 4 - year / 100 + year / 400 + t[mon - 1] + day) % 7;
} }
static time_t broken_down_to_unix(time_t year, time_t yday, time_t hour, time_t min, time_t sec)
{
return sec + min * 60 + hour * 3600 + yday * 86400 + (year - 70) * 31536000 + ((year - 69) / 4) * 86400 -
((year - 1) / 100) * 86400 + ((year + 299) / 400) * 86400;
}
static void time_to_struct_tm(time_t time, struct tm* result) static void time_to_struct_tm(time_t time, struct tm* result)
{ {
result->tm_isdst = 0; // No DST/timezone support for now. result->tm_isdst = 0; // No DST/timezone support for now.
@ -164,4 +170,9 @@ extern "C"
{ {
return asctime(localtime(time)); return asctime(localtime(time));
} }
time_t mktime(struct tm* time)
{
return broken_down_to_unix(time->tm_year, time->tm_yday, time->tm_hour, time->tm_min, time->tm_sec);
}
} }