Progress...

This commit is contained in:
Nathan Fisher 2024-02-08 10:38:37 -05:00
parent ea728b2487
commit c2b926daf0
2 changed files with 64 additions and 0 deletions

View file

@ -76,3 +76,64 @@ void dateTimeGetTimespec(DateTime *self, struct timespec * ts) {
ts->tv_nsec = self->nanoseconds;
ts->tv_sec = (time_t)dateTimeGetTimestamp(self);
}
Weekday dateTimeGetWeekday(DateTime *self) {
int64_t ts = dateTimeGetTimestampNaive(self);
ts /= SECONDS_PER_DAY;
return ts % 7;
}
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)) {
secs += yearGetSeconds(&dt->year);
yearDecrement(&dt->year);
}
month = December;
while (ret == 0) {
if (secs > monthGetSeconds(month, &dt->year)) break;
secs += monthGetSeconds(month, &dt->year);
monthDecrement(&month);
}
ret = 0;
dt->month = month;
dt->day = monthGetDays(month, &dt->year);
//todo
} else if (seconds > 0) {
secs = seconds;
yearNew(&dt->year, 1970);
while (yearGetSeconds(&dt->year) < seconds) {
secs -= yearGetSeconds(&dt->year);
yearIncrement(&dt->year);
}
dt->month = January;
while (monthGetSeconds(dt->month, &dt->year) < secs) {
secs -= (int64_t)monthGetSeconds(dt->month, &dt->year);
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;
} else {
yearNew(&dt->year, 1970);
dt->month = January;
dt->day = 1;
dt->hour = 0;
dt->minute = 0;
dt->second = 0;
}
}
void dateTimeFromTimespec(struct timespec *ts, DateTime *dt) {
}

View file

@ -144,5 +144,8 @@ int32_t dateTimeGetYear(DateTime *self);
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 dateTimeFromTimespec(struct timespec *ts, DateTime *dt);
#endif // !LIBEPOCH_H