Systems-programming-labs/Week 2-workshop/year_utils.c
2021-08-09 17:20:19 +08:00

33 lines
805 B
C

#include <time.h>
#include <stdbool.h>
static char *DAY_NAME[] = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
static char *MONTH_NAME[] = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"
};
bool isLeapYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
int monthLimit(int month, int year)
{
bool leapYear = isLeapYear(year);
switch (month)
{
case 2:
return 28 + leapYear;
default:
return 31 - (month - 1) % 7 % 2;
}
}
struct tm getCurrentTime() {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
return tm;
}