summaryrefslogtreecommitdiff
path: root/lib/libc/time/gmtime_r.c
diff options
context:
space:
mode:
authorKacper <kacper@mail.openlinux.dev>2025-12-07 20:10:31 +0100
committerKacper <kacper@mail.openlinux.dev>2025-12-07 20:10:31 +0100
commitfc00c656c96528112d05cf0edf8631bd5eaea446 (patch)
treea6e0e6c588191a8bd1c64afc3b7a258e3e66c236 /lib/libc/time/gmtime_r.c
Add build system scaffolding and libc headers
Diffstat (limited to 'lib/libc/time/gmtime_r.c')
-rw-r--r--lib/libc/time/gmtime_r.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/libc/time/gmtime_r.c b/lib/libc/time/gmtime_r.c
new file mode 100644
index 00000000..8d441549
--- /dev/null
+++ b/lib/libc/time/gmtime_r.c
@@ -0,0 +1,42 @@
+#include <time.h>
+
+struct tm *gmtime_r(const time_t *timer, struct tm *result)
+{
+ time_t t = *timer;
+ int days, rem;
+ int year, month;
+
+ rem = t % 86400;
+ days = t / 86400;
+ if (rem < 0) {
+ rem += 86400;
+ days--;
+ }
+
+ result->tm_hour = rem / 3600;
+ rem %= 3600;
+ result->tm_min = rem / 60;
+ result->tm_sec = rem % 60;
+ result->tm_isdst = 0;
+
+ result->tm_wday = (4 + days) % 7;
+ if (result->tm_wday < 0)
+ result->tm_wday += 7;
+
+ long z = days + 719468;
+ long era = (z >= 0 ? z : z - 146096) / 146097;
+ long day_of_era = z - era * 146097;
+ long yoe = (day_of_era - day_of_era / 1460 + day_of_era / 36524 -
+ day_of_era / 146096) /
+ 365;
+ year = (int)(yoe + era * 400);
+ long doy = day_of_era - (365 * yoe + yoe / 4 - yoe / 100);
+ result->tm_yday = (int)doy;
+
+ int mp = (5 * doy + 2) / 153;
+ result->tm_mday = (int)(doy - (153 * mp + 2) / 5 + 1);
+ result->tm_mon = (mp + 2) % 12;
+ result->tm_year = year - 1900 + (mp / 10);
+
+ return result;
+}