diff --git a/datetime.c b/datetime.c index aa23d53..b0644ba 100644 --- a/datetime.c +++ b/datetime.c @@ -32,6 +32,7 @@ #include "epoch.h" #include +#include #include int32_t dateTimeGetYear(DateTime *self) { @@ -84,21 +85,20 @@ Weekday dateTimeGetWeekday(DateTime *self) { } void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *dt) { - int64_t secs; int ret = 0; dt->nanoseconds = nanoseconds; + dt->zone.tag = UTC; if (seconds < 0) { - secs = seconds; yearNew(&dt->year, 1969); - while (secs < yearGetSeconds(&dt->year)) { - secs += yearGetSeconds(&dt->year); + while (seconds < yearGetSeconds(&dt->year)) { + seconds += yearGetSeconds(&dt->year); yearDecrement(&dt->year); } dt->month = December; while (ret == 0) { - if (-secs < monthGetSeconds(dt->month, &dt->year)) break; - secs += monthGetSeconds(dt->month, &dt->year); + if (-seconds < monthGetSeconds(dt->month, &dt->year)) break; + seconds += monthGetSeconds(dt->month, &dt->year); ret = monthDecrement(&dt->month); } ret = 0; @@ -119,24 +119,23 @@ void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime * } dt->second = seconds + 60; } else if (seconds > 0) { - secs = seconds; yearNew(&dt->year, 1970); while (yearGetSeconds(&dt->year) < seconds) { - secs -= yearGetSeconds(&dt->year); + seconds -= yearGetSeconds(&dt->year); yearIncrement(&dt->year); } dt->month = January; - while (ret == 0 && monthGetSeconds(dt->month, &dt->year) < secs) { - secs -= (int64_t)monthGetSeconds(dt->month, &dt->year); + while (ret == 0 && monthGetSeconds(dt->month, &dt->year) < seconds) { + seconds -= (int64_t)monthGetSeconds(dt->month, &dt->year); ret = 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; + dt->day = seconds / SECONDS_PER_DAY + 1; + seconds %= SECONDS_PER_DAY; + dt->hour = seconds / SECONDS_PER_HOUR; + seconds %= SECONDS_PER_HOUR; + dt->minute = seconds / 60; + seconds %= 60; + dt->second = seconds; } else { yearNew(&dt->year, 1970); dt->month = January; diff --git a/include/epoch.h b/include/epoch.h index 581c191..f73d61d 100644 --- a/include/epoch.h +++ b/include/epoch.h @@ -37,8 +37,8 @@ #include #define SECONDS_PER_MINUTE 60 -#define SECONDS_PER_HOUR 60 * 60 -#define SECONDS_PER_DAY 60 * 60 * 24 +#define SECONDS_PER_HOUR 3600 +#define SECONDS_PER_DAY 86400 typedef enum { normalYear, diff --git a/test/Makefile b/test/Makefile index 1d99955..0caba5f 100644 --- a/test/Makefile +++ b/test/Makefile @@ -38,6 +38,7 @@ LDLIBS += $(LIBS) tests += dt2ts tests += dt2timespec +tests += dtfromtimespec tests += parsemonth total != echo $(tests) | wc -w | awk '{ print $$1 }' diff --git a/test/dtfromtimespec.c b/test/dtfromtimespec.c new file mode 100644 index 0000000..49fdade --- /dev/null +++ b/test/dtfromtimespec.c @@ -0,0 +1,21 @@ +#include "epoch.h" +#include +#include + +int main() { + struct timespec ts; + DateTime dt; + + ts.tv_nsec = 42; + ts.tv_sec = 1706571482; + dateTimeFromTimespec(&ts, &dt); + assert(dt.year.year == 2024); + assert(dt.month == January); + assert(dt.day == 29); + assert(dt.hour == 23); + assert(dt.minute == 38); + assert(dt.second == 02); + assert(dt.nanoseconds == 42); + assert(dt.zone.tag == UTC); + return 0; +}