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