#include <stdio.h>
#include <string.h>
#include <stdbool.h>
static size_t month_days[] = {29, 30};
static size_t days_month [][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static size_t datas[][17] = {
{23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0},
{41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1},
{30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1},
{49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
{38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
{26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0},
{45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
{35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
{24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1},
{43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1},
{32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1},
{21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
{40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
{28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
{47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1},
{36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
{26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 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1, 1, 0, 1, 0, 1, 0}
};
bool is_leap_year(size_t year)
{
if(year % 400 == 0)
return true;
if(year % 100 == 0)
return false;
if(year % 4 == 0)
return true;
return false;
}
size_t day_of_year(size_t year, size_t month, size_t day)
{
const static size_t day_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
size_t days = 0;
for(size_t i = 0; i < month - 1; ++i)
days += day_of_month[i];
days += day;
if(is_leap_year(year))
++days;
return days;
}
void print_gregorian_calendar(size_t year, size_t month, size_t day)
{
size_t index = year - 1936;
//month += 1;
size_t end_index = month;
if(datas[index][1] != 0 && month > datas[index][1])
++end_index;
ssize_t day_num = 0;
for(size_t i = 0; i < (end_index - 1); ++i)
day_num += month_days[datas[index][4 + i]];
day_num += day;
day_num += datas[index][0];
ssize_t year_days = 365;
if(is_leap_year(year))
year_days = 366;
if(day_num > year_days) {
++year;
day_num -= year_days;
}
size_t day_of_month[12];
memcpy(day_of_month, days_month[0], sizeof(size_t) * 12);
if(is_leap_year(year))
memcpy(day_of_month, days_month[1], sizeof(size_t) * 12);
month = 1;
size_t i;
for(i = 0; i < 12; ++i) {
day_num -= day_of_month[i];
if(day_num <= 0)
break;
++month;
}
day = day_of_month[i] + day_num;
printf("%lu - %lu - %lu\n", year, month, day);
}
void print_lunar_calendar(size_t year, size_t month, size_t day)
{
if(year < 1936 || year > 2028)
return;
size_t index = year - 1936;
size_t day_year = day_of_year(year, month, day);
size_t days;
if(day_year >= datas[index][0]) days = day_year - datas[index][0];
else {
--index;
--year;
days = day_of_year(year, 12, 31) + day_year - datas[index][0];
}
size_t i;
size_t day_num = 0;
month = 1;
for(i = 0; i < 13; ++i) {
day_num += month_days[datas[index][i + 4]];
if(day_num >= days)
break;
++month;
}
day = month_days[datas[index][i + 4]] - (day_num - days);
if(datas[index][1] != 0 && month > datas[index][1])
--month;
printf("%lu - %lu - %lu\n", year, month, day);
}
int main(void)
{
print_gregorian_calendar(2020, 7, 19);
print_lunar_calendar(2020, 7, 19);
return 0;
}