151 lines
5.4 KiB
C
151 lines
5.4 KiB
C
/* _,.---._ .-._ .--.-. ,--.--------.
|
|
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
|
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
|
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
|
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
* _ __ ,---. .-._ .=-.-. _,.----.
|
|
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
|
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
|
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
|
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
|
* `--`---' `--` `--`./ `--``--`-`
|
|
*
|
|
* @(#)Copyright (c) 2024, Nathan D. Fisher.
|
|
*
|
|
* This is free software. It comes with NO WARRANTY.
|
|
* Permission to use, modify and distribute this source code
|
|
* is granted subject to the following conditions.
|
|
* 1/ that the above copyright notice and this notice
|
|
* are preserved in all copies and that due credit be given
|
|
* to the author.
|
|
* 2/ that any changes to this code are clearly commented
|
|
* as such so that the author does not get blamed for bugs
|
|
* other than his own.
|
|
*/
|
|
|
|
#include "epoch.h"
|
|
#include <stdint.h>
|
|
#include <time.h>
|
|
|
|
int32_t dateTimeGetYear(DateTime *self) {
|
|
return self->year.year;
|
|
}
|
|
|
|
int64_t dateTimeGetTimestampNaive(DateTime *self) {
|
|
int64_t seconds = 0;
|
|
Year year = (Year){ .tag = normalYear, .year = 1970 };
|
|
Month month = January;
|
|
int32_t oldyear = yearGetInner(&self->year);
|
|
|
|
if (oldyear < 1970) {
|
|
while (yearGetInner(&year) > oldyear) {
|
|
yearDecrement(&year);
|
|
seconds -= yearGetSeconds(&year);
|
|
}
|
|
} else if (oldyear > 1970) {
|
|
while (yearGetInner(&year) < oldyear) {
|
|
seconds += yearGetSeconds(&year);
|
|
yearIncrement(&year);
|
|
}
|
|
}
|
|
while (month < self->month) {
|
|
seconds += monthGetSeconds(month, &self->year);
|
|
monthIncrement(&month);
|
|
}
|
|
seconds += ((int64_t)self->day - 1) * SECONDS_PER_DAY;
|
|
seconds += (int64_t)self->hour * SECONDS_PER_HOUR;
|
|
seconds += (int64_t)self->minute * 60;
|
|
seconds += (int64_t)self->second;
|
|
return seconds;
|
|
}
|
|
|
|
int64_t dateTimeGetTimestamp(DateTime *self) {
|
|
int64_t seconds = dateTimeGetTimestampNaive(self);
|
|
seconds += timezoneAsSeconds(&self->zone);
|
|
return seconds;
|
|
}
|
|
|
|
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(DateTime *self, int64_t seconds, int64_t nanoseconds) {
|
|
int ret = 0;
|
|
|
|
self->nanoseconds = nanoseconds;
|
|
self->zone.tag = UTC;
|
|
if (seconds < 0) {
|
|
yearNew(&self->year, 1969);
|
|
while (seconds < yearGetSeconds(&self->year)) {
|
|
seconds += yearGetSeconds(&self->year);
|
|
yearDecrement(&self->year);
|
|
}
|
|
self->month = December;
|
|
while (ret == 0) {
|
|
if (-seconds < monthGetSeconds(self->month, &self->year)) break;
|
|
seconds += monthGetSeconds(self->month, &self->year);
|
|
ret = monthDecrement(&self->month);
|
|
}
|
|
ret = 0;
|
|
self->day = monthGetDays(self->month, &self->year);
|
|
while (self->day > 0 && seconds < -SECONDS_PER_DAY) {
|
|
seconds += SECONDS_PER_DAY;
|
|
self->day -=1;
|
|
}
|
|
self->hour = 23;
|
|
while (self->hour > 0 && seconds < -SECONDS_PER_HOUR) {
|
|
seconds += SECONDS_PER_HOUR;
|
|
self->hour -= 1;
|
|
}
|
|
self->minute = 59;
|
|
while (self->minute > 0 && seconds < -60) {
|
|
seconds += 60;
|
|
self->minute -= 1;
|
|
}
|
|
self->second = seconds + 60;
|
|
} else if (seconds > 0) {
|
|
yearNew(&self->year, 1970);
|
|
while (yearGetSeconds(&self->year) < seconds) {
|
|
seconds -= yearGetSeconds(&self->year);
|
|
yearIncrement(&self->year);
|
|
}
|
|
self->month = January;
|
|
while (ret == 0 && monthGetSeconds(self->month, &self->year) < seconds) {
|
|
seconds -= (int64_t)monthGetSeconds(self->month, &self->year);
|
|
ret = monthIncrement(&self->month);
|
|
}
|
|
self->day = seconds / SECONDS_PER_DAY + 1;
|
|
seconds %= SECONDS_PER_DAY;
|
|
self->hour = seconds / SECONDS_PER_HOUR;
|
|
seconds %= SECONDS_PER_HOUR;
|
|
self->minute = seconds / 60;
|
|
seconds %= 60;
|
|
self->second = seconds;
|
|
} else {
|
|
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(self, ts->tv_sec, ts->tv_nsec);
|
|
}
|
|
|