/* _,.---._ .-._ .--.-. ,--.--------. * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ * |==|,| | -|==| , '=' |==| - _ | |==|- | * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ * `-.`.____.' `--`--'' `--`./ `--` `--`--` * _ __ ,---. .-._ .=-.-. _,.----. * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' * `--`---' `--` `--`./ `--``--`-` * * @(#)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 #include 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); }