Merge branch 'odin' of git.hitchhiker-linux.org:jeang3nie/libepoch into odin

This commit is contained in:
Nathan Fisher 2024-04-17 03:10:52 -04:00
commit 5d3d4658f5
3 changed files with 55 additions and 43 deletions

View file

@ -0,0 +1,12 @@
PREFIX ?= /usr/local
bindir = $(DESTDIR)$(PREFIX)/bin
includedir = $(DESTDIR)$(PREFIX)/include
libdir = $(DESTDIR)$(PREFIX)/lib
sharedir = $(DESTDIR)$(PREFIX)/share
mandir = $(sharedir)/man
docdir = $(sharedir)/doc/libepoch
# We need an `echo` program that doesn't screw with terminal escape sequences.
# This only matters if /bin/sh is a symlink to dash, as the echo builtin in dash
# will screw with them and pass them as printed characters.
ECHO = /bin/echo

View file

@ -32,7 +32,6 @@
#include "epoch.h" #include "epoch.h"
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <time.h> #include <time.h>
int32_t dateTimeGetYear(DateTime *self) { int32_t dateTimeGetYear(DateTime *self) {
@ -84,68 +83,69 @@ Weekday dateTimeGetWeekday(DateTime *self) {
return ts % 7; return ts % 7;
} }
void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *dt) { void dateTimeFromTimestampParts(DateTime *self, int64_t seconds, int64_t nanoseconds) {
int ret = 0; int ret = 0;
dt->nanoseconds = nanoseconds; self->nanoseconds = nanoseconds;
dt->zone.tag = UTC; self->zone.tag = UTC;
if (seconds < 0) { if (seconds < 0) {
yearNew(&dt->year, 1969); yearNew(&self->year, 1969);
while (seconds < yearGetSeconds(&dt->year)) { while (seconds < yearGetSeconds(&self->year)) {
seconds += yearGetSeconds(&dt->year); seconds += yearGetSeconds(&self->year);
yearDecrement(&dt->year); yearDecrement(&self->year);
} }
dt->month = December; self->month = December;
while (ret == 0) { while (ret == 0) {
if (-seconds < monthGetSeconds(dt->month, &dt->year)) break; if (-seconds < monthGetSeconds(self->month, &self->year)) break;
seconds += monthGetSeconds(dt->month, &dt->year); seconds += monthGetSeconds(self->month, &self->year);
ret = monthDecrement(&dt->month); ret = monthDecrement(&self->month);
} }
ret = 0; ret = 0;
dt->day = monthGetDays(dt->month, &dt->year); self->day = monthGetDays(self->month, &self->year);
while (dt->day > 0 && seconds < -SECONDS_PER_DAY) { while (self->day > 0 && seconds < -SECONDS_PER_DAY) {
seconds += SECONDS_PER_DAY; seconds += SECONDS_PER_DAY;
dt->day -=1; self->day -=1;
} }
dt->hour = 23; self->hour = 23;
while (dt->hour > 0 && seconds < -SECONDS_PER_HOUR) { while (self->hour > 0 && seconds < -SECONDS_PER_HOUR) {
seconds += SECONDS_PER_HOUR; seconds += SECONDS_PER_HOUR;
dt->hour -= 1; self->hour -= 1;
} }
dt->minute = 59; self->minute = 59;
while (dt->minute > 0 && seconds < -60) { while (self->minute > 0 && seconds < -60) {
seconds += 60; seconds += 60;
dt->minute -= 1; self->minute -= 1;
} }
dt->second = seconds + 60; self->second = seconds + 60;
} else if (seconds > 0) { } else if (seconds > 0) {
yearNew(&dt->year, 1970); yearNew(&self->year, 1970);
while (yearGetSeconds(&dt->year) < seconds) { while (yearGetSeconds(&self->year) < seconds) {
seconds -= yearGetSeconds(&dt->year); seconds -= yearGetSeconds(&self->year);
yearIncrement(&dt->year); yearIncrement(&self->year);
} }
dt->month = January; self->month = January;
while (ret == 0 && monthGetSeconds(dt->month, &dt->year) < seconds) { while (ret == 0 && monthGetSeconds(self->month, &self->year) < seconds) {
seconds -= (int64_t)monthGetSeconds(dt->month, &dt->year); seconds -= (int64_t)monthGetSeconds(self->month, &self->year);
ret = monthIncrement(&dt->month); ret = monthIncrement(&self->month);
} }
dt->day = seconds / SECONDS_PER_DAY + 1; self->day = seconds / SECONDS_PER_DAY + 1;
seconds %= SECONDS_PER_DAY; seconds %= SECONDS_PER_DAY;
dt->hour = seconds / SECONDS_PER_HOUR; self->hour = seconds / SECONDS_PER_HOUR;
seconds %= SECONDS_PER_HOUR; seconds %= SECONDS_PER_HOUR;
dt->minute = seconds / 60; self->minute = seconds / 60;
seconds %= 60; seconds %= 60;
dt->second = seconds; self->second = seconds;
} else { } else {
yearNew(&dt->year, 1970); yearNew(&self->year, 1970);
dt->month = January; self->month = January;
dt->day = 1; self->day = 1;
dt->hour = 0; self->hour = 0;
dt->minute = 0; self->minute = 0;
dt->second = 0; self->second = 0;
} }
} }
void dateTimeFromTimespec(DateTime *self, struct timespec *ts) { void dateTimeFromTimespec(DateTime *self, struct timespec *ts) {
dateTimeFromTimestampParts(ts->tv_sec, ts->tv_nsec, self); dateTimeFromTimestampParts(self, ts->tv_sec, ts->tv_nsec);
} }

View file

@ -146,8 +146,8 @@ int64_t dateTimeGetTimestampNaive (DateTime *self);
int64_t dateTimeGetTimestamp (DateTime *self); int64_t dateTimeGetTimestamp (DateTime *self);
void dateTimeGetTimespec (DateTime *self, struct timespec *ts); void dateTimeGetTimespec (DateTime *self, struct timespec *ts);
Weekday dateTimeGetWeekday (DateTime *self); Weekday dateTimeGetWeekday (DateTime *self);
void dateTimeFromTimestampParts (int64_t seconds, int64_t nanoseconds, void dateTimeFromTimestampParts (DateTime *self, int64_t seconds,
DateTime *dt); int64_t nanoseconds);
void dateTimeFromTimespec (DateTime *self, struct timespec *ts); void dateTimeFromTimespec (DateTime *self, struct timespec *ts);
#endif // !LIBEPOCH_H #endif // !LIBEPOCH_H