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

101 lines
3.2 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 <stddef.h> // NULL
#include <string.h> // memcmp
#include <strings.h>
#include "epoch.h"
uint8_t monthGetDays(Month self, Year *year) {
switch (self) {
case January:
case March:
case May:
case July:
case August:
case October:
case December:
return 31;
case February:
switch (year->tag) {
case leapYear:
return 28;
default:
return 29;
}
default:
return 30;
}
}
uint32_t monthGetSeconds(Month self, Year *year) {
return (int64_t)monthGetDays(self, year) * SECONDS_PER_DAY;
}
int monthIncrement(Month *self) {
if (*self == December)
return 1;
else
*self += 1;
return 0;
}
int monthDecrement(Month *self) {
if (*self == January)
return 1;
else
*self -= 1;
return 0;
}
const char *MonthNames[] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
const char *MonthAbbrs[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec" };
const char* monthName(Month self) {
return MonthNames[self - 1];
}
const char* monthAbbr(Month self) {
return MonthAbbrs[self - 1];
}
int parseMonth(const char *s) {
int i;
for (i = 0; i < 12; i++) {
if (strncasecmp(s, MonthAbbrs[i], 4) == 0 || strncasecmp(s, MonthNames[i], 10) == 0)
return i + 1;
}
return -1;
}