Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make isleap faster #4852

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Sources/CoreFoundation/CFDate.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,14 @@ static const uint8_t daysInMonth[16] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 3
static const uint16_t daysBeforeMonth[16] = {INVALID_MONTH_RESULT, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, INVALID_MONTH_RESULT, INVALID_MONTH_RESULT};
static const uint16_t daysAfterMonth[16] = {365, 334, 306, 275, 245, 214, 184, 153, 122, 92, 61, 31, 0, 0, 0, 0};

CF_INLINE bool isleap(int64_t year) {
int64_t y = (year + 1) % 400; /* correct to nearest multiple-of-400 year, then find the remainder */
if (y < 0) y = -y;
return (0 == (y & 3) && 100 != y && 200 != y && 300 != y);
CF_INLINE bool isleap(int64_t year)
{
year++; // year is current year minus 1, so add 1 back
if (year < 0) year = -year;
if ((year & 3) != 0) return false;
if (year % 400) return true;
if (year % 100) return false;
return true;
}

/* year arg is absolute year; Gregorian 2001 == year 0; 2001/1/1 = absolute date 0 */
Expand Down