Skip to content

Commit

Permalink
Change leap year calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
arashm committed May 26, 2024
1 parent a4a2e28 commit 5f6405c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,22 @@ module.exports = {
ABBR_DAYS: ['۱ش', '۲ش', '۳ش', '۴ش', '۵ش', 'ج', 'ش'],
DAYS_NAMES: ['یکشنبه', 'دوشنبه', 'سه‌شنبه', 'چهارشنبه', 'پنج‌شنبه', 'جمعه', 'شنبه'],
GREGORIAN_EPOCH: 1721425.5,
PERSIAN_EPOCH: 1948320.5
PERSIAN_EPOCH: 1948320.5,
NON_LEAP_CORRECTION: [
1502,
1601, 1634, 1667,
1700, 1733, 1766, 1799,
1832, 1865, 1898,
1931, 1964, 1997,
2030, 2059, 2063, 2096,
2129, 2158, 2162, 2191, 2195,
2224, 2228, 2257, 2261, 2290, 2294,
2323, 2327, 2356, 2360, 2389, 2393,
2422, 2426, 2455, 2459, 2488, 2492,
2521, 2525, 2554, 2558, 2587, 2591,
2620, 2624, 2653, 2657, 2686, 2690,
2719, 2723, 2748, 2752, 2756, 2781, 2785, 2789,
2818, 2822, 2847, 2851, 2855, 2880, 2884, 2888,
2913, 2917, 2921, 2946, 2950, 2954, 2979, 2983, 2987
]
};
13 changes: 8 additions & 5 deletions src/converter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mod } from './helpers';
import { GREGORIAN_EPOCH, PERSIAN_EPOCH } from './constants';
import { GREGORIAN_EPOCH, PERSIAN_EPOCH, NON_LEAP_CORRECTION } from './constants';

export default class Converter {
// LEAP_GREGORIAN -- Is a given year in the Gregorian calendar a leap year?
Expand Down Expand Up @@ -57,10 +57,13 @@ export default class Converter {

// LEAP_PERSIAN -- Is a given year a leap year in the Persian calendar ?
static leapPersian(year) {
if (year === 1403) return true; // Well, algorithms are not perfect \o/
return (
(((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816
) < 682;
if (NON_LEAP_CORRECTION.includes(year)) {
return false;
} if (NON_LEAP_CORRECTION.includes(year - 1)) {
return true;
}

return (25 * year + 11) % 33 < 8;
}

// PERSIAN_TO_JD -- Determine Julian day from Persian date
Expand Down

0 comments on commit 5f6405c

Please sign in to comment.