Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "m_basic.h"
- #include "lb_reki.h"
- /* Conversion table -- holds modern calendar {month, day} pairs to convert from Japanese Lunar date to Modern date */
- static unchar l_lbRk_ConvertTable[][lbRk_KYUU_LEAP_MONTH][2] = {
- {{0x02, 0x05}, {0x03, 0x06}, {0x04, 0x05}, {0x05, 0x04}, {0x06, 0x02}, {0x07, 0x02}, {0x07, 0x1f}, {0x08, 0x1d}, {0x09, 0x1c}, {0x0a, 0x1b}, {0x0b, 0x1a}, {0x0c, 0x1a}, {0x00, 0x00}},
- {{0x01, 0x18}, {0x02, 0x17}, {0x03, 0x19}, {0x04, 0x18}, {0x06, 0x15}, {0x07, 0x15}, {0x08, 0x13}, {0x09, 0x11}, {0x0a, 0x11}, {0x0b, 0x0f}, {0x0c, 0x0f}, {0x01, 0x0d}, {0x05, 0x17}},
- {{0x02, 0x0c}, {0x03, 0x0e}, {0x04, 0x0d}, {0x05, 0x0c}, {0x06, 0x0b}, {0x07, 0x0a}, {0x08, 0x09}, {0x09, 0x07}, {0x0a, 0x06}, {0x0b, 0x05}, {0x0c, 0x04}, {0x01, 0x03}, {0x00, 0x00}},
- {{0x02, 0x01}, {0x03, 0x03}, {0x04, 0x02}, {0x05, 0x01}, {0x05, 0x1f}, {0x06, 0x1e}, {0x07, 0x1d}, {0x08, 0x1c}, {0x09, 0x1d}, {0x0a, 0x19}, {0x0b, 0x18}, {0x0c, 0x17}, {0x00, 0x00}},
- {{0x01, 0x16}, {0x02, 0x14}, {0x04, 0x13}, {0x05, 0x13}, {0x06, 0x12}, {0x07, 0x11}, {0x08, 0x10}, {0x09, 0x0e}, {0x0a, 0x0e}, {0x0b, 0x0c}, {0x0c, 0x0c}, {0x01, 0x0a}, {0x03, 0x15}},
- {{0x02, 0x09}, {0x03, 0x0a}, {0x04, 0x09}, {0x05, 0x08}, {0x06, 0x07}, {0x07, 0x06}, {0x08, 0x05}, {0x09, 0x04}, {0x0a, 0x03}, {0x0b, 0x02}, {0x0c, 0x02}, {0x0c, 0x1f}, {0x00, 0x00}},
- {{0x01, 0x1d}, {0x02, 0x1c}, {0x03, 0x1d}, {0x04, 0x1c}, {0x05, 0x1b}, {0x06, 0x1a}, {0x07, 0x19}, {0x09, 0x16}, {0x0a, 0x16}, {0x0b, 0x15}, {0x0c, 0x14}, {0x01, 0x13}, {0x08, 0x18}},
- {{0x02, 0x12}, {0x03, 0x13}, {0x04, 0x11}, {0x05, 0x11}, {0x06, 0x0f}, {0x07, 0x0e}, {0x08, 0x0d}, {0x09, 0x0b}, {0x0a, 0x0b}, {0x0b, 0x0a}, {0x0c, 0x0a}, {0x01, 0x08}, {0x00, 0x00}},
- {{0x02, 0x07}, {0x03, 0x08}, {0x04, 0x06}, {0x05, 0x05}, {0x06, 0x04}, {0x07, 0x03}, {0x08, 0x01}, {0x08, 0x1f}, {0x09, 0x1d}, {0x0a, 0x1d}, {0x0b, 0x1c}, {0x0c, 0x1b}, {0x00, 0x00}},
- {{0x01, 0x1a}, {0x02, 0x19}, {0x03, 0x1b}, {0x04, 0x19}, {0x05, 0x18}, {0x07, 0x16}, {0x08, 0x14}, {0x09, 0x13}, {0x0a, 0x12}, {0x0b, 0x11}, {0x0c, 0x10}, {0x01, 0x0f}, {0x06, 0x17}},
- {{0x02, 0x0e}, {0x03, 0x10}, {0x04, 0x0e}, {0x05, 0x0e}, {0x06, 0x0c}, {0x07, 0x0c}, {0x08, 0x0a}, {0x09, 0x08}, {0x0a, 0x08}, {0x0b, 0x06}, {0x0c, 0x06}, {0x01, 0x04}, {0x00, 0x00}},
- {{0x02, 0x03}, {0x03, 0x05}, {0x04, 0x03}, {0x05, 0x03}, {0x06, 0x02}, {0x07, 0x01}, {0x07, 0x1f}, {0x08, 0x1d}, {0x09, 0x1b}, {0x0a, 0x1b}, {0x0b, 0x19}, {0x0c, 0x19}, {0x00, 0x00}},
- {{0x01, 0x17}, {0x02, 0x16}, {0x03, 0x16}, {0x05, 0x15}, {0x06, 0x14}, {0x07, 0x13}, {0x08, 0x12}, {0x09, 0x10}, {0x0a, 0x0f}, {0x0b, 0x0e}, {0x0c, 0x0d}, {0x01, 0x0c}, {0x04, 0x15}},
- {{0x02, 0x0a}, {0x03, 0x0c}, {0x04, 0x0a}, {0x05, 0x0a}, {0x06, 0x09}, {0x07, 0x08}, {0x08, 0x07}, {0x09, 0x05}, {0x0a, 0x05}, {0x0b, 0x03}, {0x0c, 0x03}, {0x01, 0x01}, {0x00, 0x00}},
- {{0x01, 0x1f}, {0x03, 0x01}, {0x03, 0x1f}, {0x04, 0x1d}, {0x05, 0x1d}, {0x06, 0x1b}, {0x07, 0x1b}, {0x08, 0x19}, {0x09, 0x18}, {0x0b, 0x16}, {0x0c, 0x16}, {0x01, 0x14}, {0x0a, 0x18}},
- {{0x02, 0x13}, {0x03, 0x14}, {0x04, 0x13}, {0x05, 0x12}, {0x06, 0x10}, {0x07, 0x10}, {0x08, 0x0e}, {0x09, 0x0d}, {0x0a, 0x0d}, {0x0b, 0x0c}, {0x0c, 0x0b}, {0x01, 0x0a}, {0x00, 0x00}},
- {{0x02, 0x08}, {0x03, 0x09}, {0x04, 0x07}, {0x05, 0x07}, {0x06, 0x05}, {0x07, 0x04}, {0x08, 0x03}, {0x09, 0x01}, {0x0a, 0x01}, {0x0a, 0x1f}, {0x0b, 0x1d}, {0x0c, 0x1d}, {0x00, 0x00}},
- {{0x01, 0x1c}, {0x02, 0x1a}, {0x03, 0x1c}, {0x04, 0x1a}, {0x05, 0x1a}, {0x07, 0x17}, {0x08, 0x16}, {0x09, 0x14}, {0x0a, 0x14}, {0x0b, 0x12}, {0x0c, 0x12}, {0x01, 0x11}, {0x06, 0x18}},
- {{0x02, 0x10}, {0x03, 0x11}, {0x04, 0x10}, {0x05, 0x0f}, {0x06, 0x0e}, {0x07, 0x0d}, {0x08, 0x0b}, {0x09, 0x0a}, {0x0a, 0x09}, {0x0b, 0x08}, {0x0c, 0x1b}, {0x01, 0x06}, {0x00, 0x00}},
- {{0x02, 0x05}, {0x03, 0x07}, {0x04, 0x05}, {0x05, 0x05}, {0x06, 0x03}, {0x07, 0x03}, {0x08, 0x01}, {0x08, 0x1e}, {0x09, 0x1d}, {0x0a, 0x1c}, {0x0b, 0x1b}, {0x0c, 0x1a}, {0x00, 0x00}},
- {{0x01, 0x19}, {0x02, 0x18}, {0x03, 0x18}, {0x04, 0x17}, {0x06, 0x15}, {0x07, 0x15}, {0x08, 0x13}, {0x09, 0x11}, {0x0a, 0x11}, {0x0b, 0x0f}, {0x0c, 0x0f}, {0x01, 0x0d}, {0x05, 0x17}},
- {{0x02, 0x0c}, {0x03, 0x0d}, {0x04, 0x0c}, {0x05, 0x0c}, {0x06, 0x0a}, {0x07, 0x0a}, {0x08, 0x08}, {0x09, 0x07}, {0x0a, 0x06}, {0x0b, 0x05}, {0x0c, 0x04}, {0x01, 0x03}, {0x00, 0x00}},
- {{0x02, 0x01}, {0x03, 0x03}, {0x04, 0x01}, {0x05, 0x01}, {0x05, 0x1e}, {0x06, 0x1d}, {0x07, 0x1d}, {0x08, 0x1b}, {0x09, 0x1a}, {0x0a, 0x19}, {0x0b, 0x18}, {0x0c, 0x17}, {0x00, 0x00}},
- {{0x01, 0x16}, {0x02, 0x14}, {0x03, 0x16}, {0x05, 0x14}, {0x06, 0x12}, {0x07, 0x12}, {0x08, 0x10}, {0x09, 0x0f}, {0x0a, 0x0f}, {0x0b, 0x0d}, {0x0c, 0x0d}, {0x01, 0x0b}, {0x04, 0x14}},
- {{0x02, 0x0a}, {0x03, 0x0a}, {0x04, 0x09}, {0x05, 0x08}, {0x06, 0x06}, {0x07, 0x06}, {0x08, 0x04}, {0x09, 0x03}, {0x0a, 0x03}, {0x0b, 0x01}, {0x0c, 0x01}, {0x0c, 0x1f}, {0x00, 0x00}},
- {{0x01, 0x1d}, {0x02, 0x1c}, {0x03, 0x1d}, {0x04, 0x1c}, {0x05, 0x1b}, {0x06, 0x19}, {0x08, 0x17}, {0x09, 0x16}, {0x0a, 0x15}, {0x0b, 0x14}, {0x0c, 0x14}, {0x01, 0x13}, {0x07, 0x19}},
- {{0x02, 0x11}, {0x03, 0x13}, {0x04, 0x11}, {0x05, 0x11}, {0x06, 0x0f}, {0x07, 0x0e}, {0x08, 0x0d}, {0x09, 0x0b}, {0x0a, 0x0b}, {0x0b, 0x09}, {0x0c, 0x09}, {0x01, 0x08}, {0x00, 0x00}},
- {{0x02, 0x07}, {0x03, 0x08}, {0x04, 0x07}, {0x05, 0x06}, {0x06, 0x05}, {0x07, 0x04}, {0x08, 0x02}, {0x09, 0x01}, {0x09, 0x1e}, {0x0a, 0x1d}, {0x0b, 0x1c}, {0x0c, 0x1c}, {0x00, 0x00}},
- {{0x01, 0x1b}, {0x02, 0x19}, {0x03, 0x1a}, {0x04, 0x19}, {0x05, 0x18}, {0x07, 0x16}, {0x08, 0x14}, {0x09, 0x13}, {0x0a, 0x12}, {0x0b, 0x10}, {0x0c, 0x10}, {0x01, 0x0f}, {0x06, 0x17}},
- {{0x02, 0x0d}, {0x03, 0x0f}, {0x04, 0x0e}, {0x05, 0x0d}, {0x06, 0x0c}, {0x07, 0x0c}, {0x08, 0x0a}, {0x09, 0x08}, {0x0a, 0x08}, {0x0b, 0x06}, {0x0c, 0x05}, {0x01, 0x04}, {0x00, 0x00}},
- {{0x02, 0x03}, {0x03, 0x04}, {0x04, 0x03}, {0x05, 0x02}, {0x06, 0x01}, {0x07, 0x01}, {0x07, 0x1e}, {0x08, 0x1d}, {0x09, 0x1b}, {0x0a, 0x1b}, {0x0b, 0x19}, {0x0c, 0x19}, {0x00, 0x00}},
- {{0x01, 0x17}, {0x02, 0x16}, {0x03, 0x17}, {0x04, 0x16}, {0x06, 0x14}, {0x07, 0x13}, {0x08, 0x12}, {0x09, 0x11}, {0x0a, 0x10}, {0x0b, 0x0f}, {0x0c, 0x0e}, {0x01, 0x0d}, {0x05, 0x15}},
- {{0x02, 0x0b}, {0x03, 0x0c}, {0x04, 0x0a}, {0x05, 0x09}, {0x06, 0x08}, {0x07, 0x07}, {0x08, 0x06}, {0x09, 0x05}, {0x0a, 0x04}, {0x0b, 0x03}, {0x0c, 0x03}, {0x01, 0x01}, {0x00, 0x00}}
- };
- static int l_lbRk_leapdays[] = {
- 0, 29, 0, 0, 29, 0, 29, 0,
- 0, 29, 0, 0, 30, 0, 29, 0,
- 0, 29, 0, 0, 29, 0, 0, 30,
- 0, 29, 0, 0, 29, 0, 0, 30,
- 0
- };
- extern lbRTC_DD_t lbRk_SeirekiDays(lbRTC_YY_t year, lbRTC_MM_t month) {
- static lbRTC_DD_t t_seiyo_days_tbl[][lbRTC_MONTH_NUM + 1] = {
- {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, /* Standard days for each month */
- {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} /* Days for each month during leap year */
- };
- return t_seiyo_days_tbl[(year & 3) == 0][month];
- }
- static unchar* lbRk_ToSeiyoMonthAndDay(lbRTC_YY_t year, int month) {
- return l_lbRk_ConvertTable[year - lbRk_YEAR_MIN][month];
- }
- extern int lbRk_KyuurekiLeapDays(lbRTC_YY_t year) {
- return l_lbRk_leapdays[year - lbRk_YEAR_MIN];
- }
- extern int lbRk_IsKyuurekiLeapYear(lbRTC_YY_t year) {
- return l_lbRk_leapdays[year - lbRk_YEAR_MIN] != 0 ? TRUE : FALSE;
- }
- static int lbRk_IsLeapMonth(lbRTC_MM_t month) {
- return month == lbRk_KYUU_LEAP_MONTH ? TRUE : FALSE;
- }
- static int lbRk_IsLeapOnNextMonth(lbRTC_YY_t year, int month) {
- return lbRk_ToSeiyoMonthAndDay(year, month)[0] + 1 == l_lbRk_ConvertTable[year - lbRk_YEAR_MIN][lbRk_KYUU_LEAP_MONTH - 1][0];
- }
- extern int lbRk_KyuurekiDays(lbRTC_YY_t year, int month) {
- unchar* seiyo_month_day = lbRk_ToSeiyoMonthAndDay(year, month);
- if (lbRk_IsLeapMonth(month) == TRUE) {
- return lbRk_KyuurekiLeapDays(year);
- }
- unchar* next_seiyo_month_day;
- if (month == lbRk_KYUU_MONTH_END) {
- next_seiyo_month_day = lbRk_ToSeiyoMonthAndDay(year + 1, lbRk_KYUU_MONTH_START);
- }
- else {
- next_seiyo_month_day = lbRk_IsLeapOnNextMonth(year, month) == TRUE ?
- lbRk_ToSeiyoMonthAndDay(year, lbRk_KYUU_LEAP_MONTH) :
- lbRk_ToSeiyoMonthAndDay(year, month + 1);
- }
- if (next_seiyo_month_day[0] == seiyo_month_day[0]) {
- return next_seiyo_month_day[1] - seiyo_month_day[1];
- }
- if (month > seiyo_month_day[0]) year++;
- int days = lbRk_SeirekiDays(year, seiyo_month_day[0]) - seiyo_month_day[1]; /* Days in standard month - conversion days */
- if (next_seiyo_month_day[0] - seiyo_month_day[0] > 1) {
- days += lbRk_SeirekiDays(year, seiyo_month_day[0] + 1);
- }
- days += next_seiyo_month_day[1];
- return days;
- }
- /* Japanese Lunar Calendar -> Modern Calendar */
- extern int lbRk_ToSeiyouReki(lbRTC_ymd_c* dst, lbRTC_ymd_c* src) {
- if (src->year == 0 || src->year > lbRk_YEAR_MAX) {
- return FALSE; /* Year out of bounds */
- }
- if (src->month > lbRk_KYUU_LEAP_MONTH) {
- return FALSE; /* Month out of bounds */
- }
- if (lbRk_IsKyuurekiLeapYear(src->year) == FALSE) {
- if (src->month > lbRk_KYUU_MONTH_END) {
- return FALSE; /* Month out of bounds, leap month excluded */
- }
- }
- if (lbRk_KyuurekiDays(src->year, src->month) < src->day) {
- return FALSE; /* Day out of bounds */
- }
- unchar* seiyo_month_day = lbRk_ToSeiyoMonthAndDay(src->year, src->month);
- lbRTC_YY_t year = src->year;
- lbRTC_MM_t month = seiyo_month_day[0];
- lbRTC_DD_t day = (seiyo_month_day[1] + src->day) - 1;
- lbRTC_DD_t seireki_days = lbRk_SeirekiDays(year, month);
- if (day > seireki_days) {
- month++;
- day -= seireki_days;
- if (month > lbRTC_MONTH_NUM) {
- month = 1; /* This is almost certainly a bug. There should be a year++ here. */
- }
- }
- if (src->month > month) {
- if (src->month != lbRk_KYUU_LEAP_MONTH) {
- year++; /* Add a year if we aren't on the leap month. */
- }
- }
- dst->year = year;
- dst->month = month;
- dst->day = day;
- }
- /* Modern Calendar -> Japanese Lunar Calendar */
- extern int lbRk_ToKyuuReki(lbRTC_ymd_c* dst, lbRTC_ymd_c* src) {
- static lbRTC_ymd_c min_date = { lbRk_YEAR_MIN, 01, 01 }; /* Jan 1st, 2000 */
- lbRTC_ymd_c min_date_tmp = min_date;
- while (TRUE) {
- lbRTC_ymd_c min_date_seiyou;
- if (lbRk_ToSeiyouReki(&min_date_seiyou, &min_date_tmp) == FALSE) {
- return FALSE;
- }
- if (src->day == min_date_seiyou.day && src->month == min_date_seiyou.month && src->year == min_date_seiyou.year) break;
- min_date_tmp.day++;
- if (lbRk_KyuurekiDays(min_date_tmp.year, min_date_tmp.month) < min_date_tmp.day) {
- min_date_tmp.month++;
- min_date_tmp.day = 1;
- if (lbRk_IsKyuurekiLeapYear(min_date_tmp.year) == FALSE) {
- if (min_date_tmp.month < lbRTC_MONTH_NUM) {
- min_date_tmp.month = 1;
- min_date_tmp.year++;
- if (min_date_tmp.year > lbRk_YEAR_MAX) return FALSE; /* Out of year bounds */
- }
- }
- else if (min_date_tmp.month > lbRk_KYUU_LEAP_MONTH) {
- min_date_tmp.month = 1;
- min_date_tmp.year++;
- if (min_date_tmp.year > lbRk_YEAR_MAX) return FALSE; /* Out of year bounds */
- }
- }
- }
- dst->day = min_date_tmp.day;
- dst->month = min_date_tmp.month;
- dst->year = min_date_tmp.year;
- }
Advertisement
Add Comment
Please, Sign In to add comment