うるう年の条件をいつも忘れてしまうのでメモ。
うるう年は
- 西暦年が4で割り切れる年は閏年
- ただし、西暦年が100で割り切れる年は平年
- ただし、西暦年が400で割り切れる年は閏年
Leap year is…
if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
then leap
else no_leap
from Wikipedia
#define TRUE 0;
#define FALSE -1;
int IsLeapYear(int year)
{
if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0) {
return TRUE;
} else {
return FALSE;
}
}
そして、うるう年の計算が必要な場合はつまり月末の日付が知りたいときなので、
うるう年判定関数を利用して、その月の月末日を取得しましょう。
When you would like to know whether it is leap year or not,
you would like to know the last day of month.
/* get the last day of month */
int GetLastDay(int year, int month)
{
int leap;
int lmdays[] = {31,29,31,30,31,30,31,31,30,31,30,31};
int mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
leap = IsLeapYear(year);
/* うるう年 */
if (leap == TRUE) {
return lmdays[month - 1];
/* うるう年以外 */
} else {
return mdays[month - 1];
}
}
