From b1cf9e5dfbc8967bd7cb2a22ec1e5e521f4e0e6e Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Wed, 31 Jul 2024 13:36:53 -0400 Subject: add clock --- euler/source/std/ctime.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 euler/source/std/ctime.cpp (limited to 'euler/source/std') diff --git a/euler/source/std/ctime.cpp b/euler/source/std/ctime.cpp new file mode 100644 index 0000000..d83817a --- /dev/null +++ b/euler/source/std/ctime.cpp @@ -0,0 +1,58 @@ +#include +#include + +time_t time(time_t *arg) { + time_t t = euler::syscall::get_time(); + if (arg) *arg = t; + return t; +} + +static tm static_tm; + +static int days_per_month[] = { + 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +}; + +tm *gmtime(const time_t *time) { + + time_t t = *time / 1024; + + static_tm.tm_isdst = 0; + + static_tm.tm_sec = t % 60; t /= 60; + static_tm.tm_min = t % 60; t /= 60; + static_tm.tm_hour = t % 24; t /= 24; + static_tm.tm_wday = (t + 5) % 7 + 1; + static_tm.tm_year = (t / 1461) * 4 + 100; + + int days_into_quadyear = t % 1461; + + static_tm.tm_yday = 0; + static_tm.tm_mon = 0; + static_tm.tm_mday = 1; + + for (int i = 0; i < 48; ++i) { + if (days_into_quadyear >= days_per_month[i]) { + days_into_quadyear -= days_per_month[i]; + if (static_tm.tm_mon == 11) { + static_tm.tm_mon = 0; + static_tm.tm_yday = 0; + } + else { + ++static_tm.tm_mon; + static_tm.tm_yday += days_per_month[i]; + } + } + else { + static_tm.tm_yday += days_into_quadyear; + static_tm.tm_mday += days_into_quadyear; + break; + } + } + + return &static_tm; + +} -- cgit v1.2.3