Finish dateTimeFromTimestampParts and dateTimeFromTimespec

functions; TODO - write tests
This commit is contained in:
Nathan Fisher 2024-02-09 11:04:05 -05:00
parent c2b926daf0
commit 9eeb0660e4

View file

@ -85,27 +85,39 @@ Weekday dateTimeGetWeekday(DateTime *self) {
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)) {
while (secs < yearGetSeconds(&dt->year)) {
secs += yearGetSeconds(&dt->year);
yearDecrement(&dt->year);
}
month = December;
dt->month = December;
while (ret == 0) {
if (secs > monthGetSeconds(month, &dt->year)) break;
secs += monthGetSeconds(month, &dt->year);
monthDecrement(&month);
if (-secs < monthGetSeconds(dt->month, &dt->year)) break;
secs += monthGetSeconds(dt->month, &dt->year);
ret = monthDecrement(&dt->month);
}
ret = 0;
dt->month = month;
dt->day = monthGetDays(month, &dt->year);
//todo
dt->day = monthGetDays(dt->month, &dt->year);
while (dt->day > 0 && seconds < -SECONDS_PER_DAY) {
seconds += SECONDS_PER_DAY;
dt->day -=1;
}
dt->hour = 23;
while (dt->hour > 0 && seconds < -SECONDS_PER_HOUR) {
seconds += SECONDS_PER_HOUR;
dt->hour -= 1;
}
dt->minute = 59;
while (dt->minute > 0 && seconds < -60) {
seconds += 60;
dt->minute -= 1;
}
dt->second = seconds + 60;
} else if (seconds > 0) {
secs = seconds;
yearNew(&dt->year, 1970);
@ -114,9 +126,9 @@ void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *
yearIncrement(&dt->year);
}
dt->month = January;
while (monthGetSeconds(dt->month, &dt->year) < secs) {
while (ret == 0 && monthGetSeconds(dt->month, &dt->year) < secs) {
secs -= (int64_t)monthGetSeconds(dt->month, &dt->year);
monthIncrement(&dt->month);
ret = monthIncrement(&dt->month);
}
dt->day = secs / SECONDS_PER_DAY;
secs %= SECONDS_PER_DAY;
@ -136,4 +148,5 @@ void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *
}
void dateTimeFromTimespec(struct timespec *ts, DateTime *dt) {
dateTimeFromTimestampParts(ts->tv_sec, ts->tv_nsec, dt);
}