libepoch/year.c
2024-02-11 19:14:28 -05:00

77 lines
2.7 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 <stdint.h>
#include "epoch.h"
void yearNew(Year *self, int32_t inner) {
self->year = inner;
if (inner % 4 == 0 && (inner % 100 != 0 || inner % 400 == 0))
self->tag = leapYear;
else
self->tag = normalYear;
}
uint16_t yearGetDays(Year *self) {
switch (self->tag) {
case normalYear:
return 365;
case leapYear:
return 366;
default:
return 365;
}
}
int64_t yearGetSeconds(Year *self) {
return (int64_t)yearGetDays(self) * SECONDS_PER_DAY;
}
int32_t yearGetInner(Year *self) {
return self->year;
}
void yearIncrement(Year *self) {
self->year += 1;
if (self->year % 4 == 0 && (self->year % 100 != 0 || self->year % 400 == 0))
self->tag = leapYear;
else
self->tag = normalYear;
}
void yearDecrement(Year *self) {
self->year -= 1;
if (self->year % 4 == 0 && (self->year % 100 != 0 || self->year % 400 == 0))
self->tag = leapYear;
else
self->tag = normalYear;
}