From aabff7a1d387da57090b1f27878256a85853af42 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 30 Oct 2022 20:46:25 +0100 Subject: [PATCH] libc: Add mktime() --- libs/libc/include/time.h | 3 +++ libs/libc/src/time.cpp | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/libs/libc/include/time.h b/libs/libc/include/time.h index 02db35f0..3921b6ff 100644 --- a/libs/libc/include/time.h +++ b/libs/libc/include/time.h @@ -71,6 +71,9 @@ extern "C" /* Fills buf with a string representation of time. Thread-safe. */ 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. */ size_t strftime(char* str, size_t max, const char* format, const struct tm* time); diff --git a/libs/libc/src/time.cpp b/libs/libc/src/time.cpp index 8face2c3..4643eec5 100644 --- a/libs/libc/src/time.cpp +++ b/libs/libc/src/time.cpp @@ -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; } +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) { result->tm_isdst = 0; // No DST/timezone support for now. @@ -164,4 +170,9 @@ extern "C" { 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); + } } \ No newline at end of file