From c2b926daf0345c9e658a80660caa5eb7f39b1a95 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Thu, 8 Feb 2024 10:38:37 -0500 Subject: [PATCH] Progress... --- datetime.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ include/epoch.h | 3 +++ 2 files changed, 64 insertions(+) diff --git a/datetime.c b/datetime.c index a68db30..94a7575 100644 --- a/datetime.c +++ b/datetime.c @@ -76,3 +76,64 @@ void dateTimeGetTimespec(DateTime *self, struct timespec * ts) { ts->tv_nsec = self->nanoseconds; ts->tv_sec = (time_t)dateTimeGetTimestamp(self); } + +Weekday dateTimeGetWeekday(DateTime *self) { + int64_t ts = dateTimeGetTimestampNaive(self); + ts /= SECONDS_PER_DAY; + return ts % 7; +} + +void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *dt) { + int64_t secs; + Month month; + int ret = 0; + + dt->nanoseconds = nanoseconds; + if (seconds < 0) { + secs = seconds; + yearNew(&dt->year, 1969); + while (secs > -yearGetSeconds(&dt->year)) { + secs += yearGetSeconds(&dt->year); + yearDecrement(&dt->year); + } + month = December; + while (ret == 0) { + if (secs > monthGetSeconds(month, &dt->year)) break; + secs += monthGetSeconds(month, &dt->year); + monthDecrement(&month); + } + ret = 0; + dt->month = month; + dt->day = monthGetDays(month, &dt->year); + //todo + } else if (seconds > 0) { + secs = seconds; + yearNew(&dt->year, 1970); + while (yearGetSeconds(&dt->year) < seconds) { + secs -= yearGetSeconds(&dt->year); + yearIncrement(&dt->year); + } + dt->month = January; + while (monthGetSeconds(dt->month, &dt->year) < secs) { + secs -= (int64_t)monthGetSeconds(dt->month, &dt->year); + monthIncrement(&dt->month); + } + dt->day = secs / SECONDS_PER_DAY; + secs %= SECONDS_PER_DAY; + dt->hour = secs / SECONDS_PER_HOUR; + secs %= SECONDS_PER_HOUR; + dt->minute = secs / 60; + secs %= 60; + dt->second = secs; + } else { + yearNew(&dt->year, 1970); + dt->month = January; + dt->day = 1; + dt->hour = 0; + dt->minute = 0; + dt->second = 0; + } +} + +void dateTimeFromTimespec(struct timespec *ts, DateTime *dt) { +} diff --git a/include/epoch.h b/include/epoch.h index 614deaa..581c191 100644 --- a/include/epoch.h +++ b/include/epoch.h @@ -144,5 +144,8 @@ int32_t dateTimeGetYear(DateTime *self); int64_t dateTimeGetTimestampNaive(DateTime *self); int64_t dateTimeGetTimestamp(DateTime *self); void dateTimeGetTimespec(DateTime *self, struct timespec *ts); +Weekday dateTimeGetWeekday(DateTime *self); +void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *dt); +void dateTimeFromTimespec(struct timespec *ts, DateTime *dt); #endif // !LIBEPOCH_H