69 lines
1.5 KiB
C
69 lines
1.5 KiB
C
#include <stdint.h>
|
|
#include <stddef.h> // NULL
|
|
#include <string.h> // memcmp
|
|
#include <strings.h>
|
|
#include "epoch.h"
|
|
|
|
uint8_t monthGetDays(Month month, Year *year) {
|
|
switch (month) {
|
|
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 month, Year *year) {
|
|
return (int64_t)monthGetDays(month, year) * SECONDS_PER_DAY;
|
|
}
|
|
|
|
int monthIncrement(Month *month) {
|
|
if (*month == December)
|
|
return 1;
|
|
else
|
|
*month += 1;
|
|
return 0;
|
|
}
|
|
|
|
int monthDecrement(Month *month) {
|
|
if (*month == January)
|
|
return 1;
|
|
else
|
|
*month -= 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 month) {
|
|
return MonthNames[month - 1];
|
|
}
|
|
|
|
const char* monthAbbr(Month month) {
|
|
return MonthAbbrs[month - 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;
|
|
}
|