fix(sub): convert far expiry dates to Jalali in constant time

gregorianToJalali walked the calendar one month at a time from a 2024
reference, so an expiry near the int64 limit (a node reports expiries
unchecked) cost seconds of CPU on every subscription fetch that renders
{{JALALI_EXPIRE_DATE}}. Jalali leap years repeat every 33 years, so
whole 33-year blocks are skipped before the walk.
This commit is contained in:
MHSanaei
2026-10-31 14:28:18 +01:00
parent fe5d704d67
commit 47e9ef1a07
2 changed files with 23 additions and 0 deletions
+7
View File
@@ -423,6 +423,13 @@ func gregorianToJalali(gy, gm, gd int) (jy, jm, jd int) {
refJDN := 2460311
days := int64(jdn - refJDN)
jy, jm, jd = 1402, 10, 11
// Leap years follow y % 33, so whole 33-year blocks are skipped: a node-reported expiry
// can lie hundreds of millions of years away, and the walk below goes month by month.
const jalaliCycleDays = 33*365 + 8
if cycles := days / jalaliCycleDays; cycles != 0 {
jy += int(cycles) * 33
days -= cycles * jalaliCycleDays
}
// Walk forward
for days > 0 {