Add first conversion test

This commit is contained in:
Nathan Fisher 2024-02-09 12:14:57 -05:00
parent 9eeb0660e4
commit 0003f3678e
4 changed files with 40 additions and 19 deletions

View file

@ -32,6 +32,7 @@
#include "epoch.h"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
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;

View file

@ -37,8 +37,8 @@
#include <time.h>
#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,

View file

@ -38,6 +38,7 @@ LDLIBS += $(LIBS)
tests += dt2ts
tests += dt2timespec
tests += dtfromtimespec
tests += parsemonth
total != echo $(tests) | wc -w | awk '{ print $$1 }'

21
test/dtfromtimespec.c Normal file
View file

@ -0,0 +1,21 @@
#include "epoch.h"
#include <assert.h>
#include <time.h>
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;
}