# تقویم شمسی (جلالی) در C / C++

> پیاده‌سازی بدون وابستگی تقویم جلالی (هجری شمسی) در C / C++: تشخیص سال کبیسه، اعتبارسنجی تاریخ، تبدیل به میلادی و شمارهٔ روز جولیانی، روز هفته و روز سال؛ روزبه‌روز برای سال‌های ۱۱۷۸ تا ۳۰۰۰ راستی‌آزمایی شده. مجوز MIT.

- پیش‌نیاز: C99 / C++
- تست‌شده با: Zig 0.16 (Clang), also as C++17
- مقایسه‌شده با: ICU4C نسخهٔ 76.1 تا 79.1 (`dlopen` / `LoadLibrary`)
- بررسی‌های اختصاصی: `-Wconversion -Werror`، تست با UBSan و ASan، معماری ۳۲ بیتی x86، `INT64_MIN/MAX` و خروجی `NULL`
- مجوز: MIT
- نسخه: 1.0.0
- دانلود همهٔ فایل‌ها (zip): https://goudarzjafari.com/persian-calendar/files/persian-calendar-c.zip

## شروع سریع

1. فایل کد را کپی یا دانلود کنید و در پروژه‌تان بگذارید (بدون هیچ وابستگی).

2. در صورت تمایل تست‌هایش را هم اجرا کنید. فایل zip همان ساختار پوشه‌ای را دارد که تست‌ها لازم دارند؛ از داخل پوشهٔ زبان: `implementations/c`

   ```sh
   cc -std=c99 -O2 persian_calendar.c test.c -o t && ./t
   ```

3. توابع مشترک را صدا بزنید: https://goudarzjafari.com/fa/persian-calendar/implementations/#common-api

## persian_calendar.h

```c
// Copyright (C) 2026 Goudarz Jafari <hi@goudarzjafari.com>
// SPDX-License-Identifier: MIT

/*
 * Solar Hijri (Persian, Jalali, Shamsi) calendar (C99, also C++).
 *
 * Algorithm: 33-year arithmetic rule + 78-year correction table,
 * the same algorithm as ICU 77.1+ and ICU4X. Matches the astronomical calendar (52.5 E meridian)
 * for 1178-3000 AP. Months are 1-based; weekdays 0 = Saturday ... 6 = Friday.
 *
 * No allocation, no libc dependency beyond <stdint.h>; suitable for embedded use.
 */
#ifndef PERSIAN_CALENDAR_H
#define PERSIAN_CALENDAR_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Years checked against the astronomical calendar. */
#define PERSIAN_VERIFIED_MIN_YEAR 1178
#define PERSIAN_VERIFIED_MAX_YEAR 3000

/* Accepted input range. Within it every intermediate value fits in 32 bits.
 * Functions return 0 (or -1 / false) for anything outside it; no input causes
 * undefined behavior, overflow or unbounded loops. */
#define PERSIAN_MIN_YEAR (-100000)
#define PERSIAN_MAX_YEAR 100000
#define PERSIAN_MIN_JDN (-34576287LL) /* 1 Farvardin -100000 = Gregorian -99379-03-29 */
#define PERSIAN_MAX_JDN 38472562LL    /* 29 Esfand 100000 = Gregorian 100622-03-13 */

typedef struct {
    int32_t year;
    int32_t month; /* 1 = Farvardin ... 12 = Esfand */
    int32_t day;
} persian_date;

extern const char *const PERSIAN_MONTH_NAMES_FA[12];   /* UTF-8 */
extern const char *const PERSIAN_MONTH_NAMES_EN[12];
extern const char *const PERSIAN_WEEKDAY_NAMES_FA[7];  /* UTF-8, index 0 = Saturday */
extern const char *const PERSIAN_WEEKDAY_NAMES_EN[7];

/* 1 if the Persian year has 366 days. Defined for every int32_t (arithmetic extrapolation
 * outside PERSIAN_MIN_YEAR..PERSIAN_MAX_YEAR). */
int persian_is_leap_year(int32_t year);
/* 29, 30 or 31; 0 if month is not 1..12. */
int persian_days_in_month(int32_t year, int32_t month);
/* 1 for a real Persian date in PERSIAN_MIN_YEAR..PERSIAN_MAX_YEAR, else 0. */
int persian_is_valid(int32_t year, int32_t month, int32_t day);

/* The functions below return 1 on success and 0 for invalid or out-of-range input
 * (or a NULL output pointer); on failure the outputs are left unchanged. */

/* Persian date -> Julian Day Number. */
int persian_to_jdn(int32_t year, int32_t month, int32_t day, int64_t *jdn);
/* Julian Day Number (PERSIAN_MIN_JDN..PERSIAN_MAX_JDN) -> Persian date. */
int persian_from_jdn(int64_t jdn, persian_date *out);

/* Proleptic Gregorian calendar (month 1..12). Invalid dates (e.g. 2025-02-30) are rejected. */
int gregorian_to_jdn(int32_t year, int32_t month, int32_t day, int64_t *jdn);
int jdn_to_gregorian(int64_t jdn, int32_t *year, int32_t *month, int32_t *day);

/* 0 = Saturday ... 6 = Friday, for any int64_t. */
int persian_day_of_week_jdn(int64_t jdn);
/* 0 = Saturday ... 6 = Friday; -1 if the date is invalid. */
int persian_day_of_week(int32_t year, int32_t month, int32_t day);
/* 1 ... 365/366; 0 if the date is invalid. */
int persian_day_of_year(int32_t year, int32_t month, int32_t day);

#ifdef __cplusplus
}
#endif

#endif /* PERSIAN_CALENDAR_H */
```

## persian_calendar.c

```c
// Copyright (C) 2026 Goudarz Jafari <hi@goudarzjafari.com>
// SPDX-License-Identifier: MIT

/* Solar Hijri (Persian) calendar. See persian_calendar.h. */
#include <stddef.h>
#include "persian_calendar.h"

#define EPOCH_JDN 1948320

/* Sorted. Years the 33-year rule calls leap but are not; the next year is leap instead. */
static const int16_t NON_LEAP_CORRECTION[78] = {
    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,
};

const char *const PERSIAN_MONTH_NAMES_FA[12] = {
    "\xD9\x81\xD8\xB1\xD9\x88\xD8\xB1\xD8\xAF\xDB\x8C\xD9\x86",                 /* فروردین */
    "\xD8\xA7\xD8\xB1\xD8\xAF\xDB\x8C\xD8\xA8\xD9\x87\xD8\xB4\xD8\xAA",         /* اردیبهشت */
    "\xD8\xAE\xD8\xB1\xD8\xAF\xD8\xA7\xD8\xAF",                                 /* خرداد */
    "\xD8\xAA\xDB\x8C\xD8\xB1",                                                 /* تیر */
    "\xD9\x85\xD8\xB1\xD8\xAF\xD8\xA7\xD8\xAF",                                 /* مرداد */
    "\xD8\xB4\xD9\x87\xD8\xB1\xDB\x8C\xD9\x88\xD8\xB1",                         /* شهریور */
    "\xD9\x85\xD9\x87\xD8\xB1",                                                 /* مهر */
    "\xD8\xA2\xD8\xA8\xD8\xA7\xD9\x86",                                         /* آبان */
    "\xD8\xA2\xD8\xB0\xD8\xB1",                                                 /* آذر */
    "\xD8\xAF\xDB\x8C",                                                         /* دی */
    "\xD8\xA8\xD9\x87\xD9\x85\xD9\x86",                                         /* بهمن */
    "\xD8\xA7\xD8\xB3\xD9\x81\xD9\x86\xD8\xAF",                                 /* اسفند */
};
const char *const PERSIAN_MONTH_NAMES_EN[12] = {
    "Farvardin", "Ordibehesht", "Khordad", "Tir", "Mordad", "Shahrivar",
    "Mehr", "Aban", "Azar", "Dey", "Bahman", "Esfand",
};
const char *const PERSIAN_WEEKDAY_NAMES_FA[7] = {
    "\xD8\xB4\xD9\x86\xD8\xA8\xD9\x87",                                         /* شنبه */
    "\xDB\x8C\xDA\xA9\xD8\xB4\xD9\x86\xD8\xA8\xD9\x87",                         /* یکشنبه */
    "\xD8\xAF\xD9\x88\xD8\xB4\xD9\x86\xD8\xA8\xD9\x87",                         /* دوشنبه */
    "\xD8\xB3\xD9\x87\xE2\x80\x8C\xD8\xB4\xD9\x86\xD8\xA8\xD9\x87",             /* سه‌شنبه (with ZWNJ) */
    "\xDA\x86\xD9\x87\xD8\xA7\xD8\xB1\xD8\xB4\xD9\x86\xD8\xA8\xD9\x87",         /* چهارشنبه */
    "\xD9\xBE\xD9\x86\xD8\xAC\xD8\xB4\xD9\x86\xD8\xA8\xD9\x87",                 /* پنجشنبه */
    "\xD8\xAC\xD9\x85\xD8\xB9\xD9\x87",                                         /* جمعه */
};
const char *const PERSIAN_WEEKDAY_NAMES_EN[7] = {
    "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
};

/* C's / and % truncate toward zero; the formulas need floor semantics. */
static int64_t fdiv(int64_t a, int64_t b) {
    int64_t q = a / b;
    return (a % b != 0 && ((a < 0) != (b < 0))) ? q - 1 : q;
}

/* Floor modulo for b > 0; defined for every a, including INT64_MIN (no multiplication). */
static int64_t fmod64(int64_t a, int64_t b) {
    int64_t r = a % b;
    return r < 0 ? r + b : r;
}

static int is_correction(int64_t year) {
    int lo = 0, hi = 77;
    if (year < NON_LEAP_CORRECTION[0] || year > NON_LEAP_CORRECTION[77]) return 0;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        if (NON_LEAP_CORRECTION[mid] == year) return 1;
        if (NON_LEAP_CORRECTION[mid] < year) lo = mid + 1; else hi = mid - 1;
    }
    return 0;
}

int persian_is_leap_year(int32_t year) {
    if (is_correction(year)) return 0;
    if (is_correction((int64_t)year - 1)) return 1;
    return fmod64(25 * (int64_t)year + 11, 33) < 8;
}

int persian_days_in_month(int32_t year, int32_t month) {
    if (month < 1 || month > 12) return 0;
    if (month <= 6) return 31;
    if (month <= 11) return 30;
    return persian_is_leap_year(year) ? 30 : 29;
}

int persian_is_valid(int32_t year, int32_t month, int32_t day) {
    return year >= PERSIAN_MIN_YEAR && year <= PERSIAN_MAX_YEAR && month >= 1 && month <= 12 && day >= 1 && day <= persian_days_in_month(year, month);
}

static int64_t nowruz_jdn(int64_t year) {
    int64_t jdn = EPOCH_JDN + 365 * (year - 1) + fdiv(8 * year + 21, 33);
    if (is_correction(year - 1)) jdn -= 1;
    return jdn;
}

static int64_t days_before_month(int32_t month) {
    return month <= 7 ? 31 * (month - 1) : 30 * (month - 1) + 6;
}

int persian_to_jdn(int32_t year, int32_t month, int32_t day, int64_t *jdn) {
    if (jdn == NULL || !persian_is_valid(year, month, day)) return 0;
    *jdn = nowruz_jdn(year) + days_before_month(month) + day - 1;
    return 1;
}

int persian_from_jdn(int64_t jdn, persian_date *out) {
    persian_date r;
    int64_t year, doy;
    if (out == NULL || jdn < PERSIAN_MIN_JDN || jdn > PERSIAN_MAX_JDN) return 0;
    year = 1 + fdiv(33 * (jdn - EPOCH_JDN) + 3, 12053);
    while (jdn >= nowruz_jdn(year + 1)) year++;
    while (jdn < nowruz_jdn(year)) year--;
    doy = jdn - nowruz_jdn(year); /* 0-based */
    r.year = (int32_t)year;
    if (doy < 186) {
        r.month = (int32_t)(doy / 31 + 1);
        r.day = (int32_t)(doy % 31 + 1);
    } else {
        doy -= 186;
        r.month = (int32_t)(doy / 30 + 7);
        r.day = (int32_t)(doy % 30 + 1);
    }
    *out = r;
    return 1;
}

static int gregorian_days_in_month(int32_t year, int32_t month) {
    static const int8_t days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int leap = fmod64(year, 4) == 0 && (fmod64(year, 100) != 0 || fmod64(year, 400) == 0);
    return month == 2 ? 28 + leap : days[month - 1];
}

int gregorian_to_jdn(int32_t year, int32_t month, int32_t day, int64_t *jdn) {
    int64_t a, y, m, result;
    if (jdn == NULL || year < -99379 || year > 100622 || month < 1 || month > 12 ||
        day < 1 || day > gregorian_days_in_month(year, month)) {
        return 0;
    }
    a = fdiv(14 - month, 12);
    y = (int64_t)year + 4800 - a;
    m = month + 12 * a - 3;
    result = day + fdiv(153 * m + 2, 5) + 365 * y + fdiv(y, 4) - fdiv(y, 100) + fdiv(y, 400) - 32045;
    if (result < PERSIAN_MIN_JDN || result > PERSIAN_MAX_JDN) return 0;
    *jdn = result;
    return 1;
}

int jdn_to_gregorian(int64_t jdn, int32_t *year, int32_t *month, int32_t *day) {
    int64_t a, b, c, d, e, m;
    if (year == NULL || month == NULL || day == NULL || jdn < PERSIAN_MIN_JDN || jdn > PERSIAN_MAX_JDN) return 0;
    a = jdn + 32044;
    b = fdiv(4 * a + 3, 146097);
    c = a - fdiv(146097 * b, 4);
    d = fdiv(4 * c + 3, 1461);
    e = c - fdiv(1461 * d, 4);
    m = fdiv(5 * e + 2, 153);
    *year = (int32_t)(100 * b + d - 4800 + fdiv(m, 10));
    *month = (int32_t)(m + 3 - 12 * fdiv(m, 10));
    *day = (int32_t)(e - fdiv(153 * m + 2, 5) + 1);
    return 1;
}

/* fmod64(jdn, 7) first, so jdn + 2 cannot overflow even for INT64_MAX. */
int persian_day_of_week_jdn(int64_t jdn) { return (int)((fmod64(jdn, 7) + 2) % 7); }

int persian_day_of_week(int32_t year, int32_t month, int32_t day) {
    int64_t jdn;
    return persian_to_jdn(year, month, day, &jdn) ? persian_day_of_week_jdn(jdn) : -1;
}

int persian_day_of_year(int32_t year, int32_t month, int32_t day) {
    return persian_is_valid(year, month, day) ? (int)(days_before_month(month) + day) : 0;
}
```

## test.c

```c
// Copyright (C) 2026 Goudarz Jafari <hi@goudarzjafari.com>
// SPDX-License-Identifier: MIT

/* Checks persian_calendar.c against /data.
 * Build & run: cc -std=c99 -Wall -Wextra -O2 persian_calendar.c test.c -o test && ./test ../../data */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "persian_calendar.h"

static long checks = 0;
#define CHECK(ok, ...) do { checks++; if (!(ok)) { fprintf(stderr, "failed: " __VA_ARGS__); fputc('\n', stderr); exit(1); } } while (0)

/* Wrappers for inputs that are known to be valid: they also check that the call succeeds. */
static persian_date pfj(int64_t jdn) {
    persian_date p;
    CHECK(persian_from_jdn(jdn, &p), "persian_from_jdn(%lld) failed", (long long)jdn);
    return p;
}
static int64_t g2j(int32_t y, int32_t m, int32_t d) {
    int64_t jdn = 0;
    CHECK(gregorian_to_jdn(y, m, d, &jdn), "gregorian_to_jdn(%d-%d-%d) failed", y, m, d);
    return jdn;
}
static void j2g(int64_t jdn, int32_t *y, int32_t *m, int32_t *d) {
    CHECK(jdn_to_gregorian(jdn, y, m, d), "jdn_to_gregorian(%lld) failed", (long long)jdn);
}

static FILE *open_csv(const char *dir, const char *name) {
    char path[1024], header[256];
    FILE *f;
    snprintf(path, sizeof path, "%s/%s", dir, name);
    f = fopen(path, "r");
    if (!f) { fprintf(stderr, "cannot open %s\n", path); exit(1); }
    if (!fgets(header, sizeof header, f)) exit(1);
    return f;
}

int main(int argc, char **argv) {
    const char *dir = argc > 1 ? argv[1] : "../../data";
    FILE *f;
    int y, leap, days, gy, gm, gd, wd, jy, jm, jd, n = 0;
    int32_t ry, rm, rd;
    long long jdn_in;
    int64_t jdn, first, last;
    persian_date p, prev;

    f = open_csv(dir, "years.csv");
    while (fscanf(f, "%d,%d,%d,%d-%d-%d,%lld,%d", &y, &leap, &days, &gy, &gm, &gd, &jdn_in, &wd) == 8) {
        CHECK(persian_is_leap_year(y) == leap, "leap %d", y);
        CHECK(persian_days_in_month(y, 12) == (leap ? 30 : 29), "esfand %d", y);
        CHECK(persian_to_jdn(y, 1, 1, &jdn) && jdn == jdn_in, "jdn %d", y);
        j2g(jdn, &ry, &rm, &rd);
        CHECK(ry == gy && rm == gm && rd == gd, "nowruz %d", y);
        CHECK(persian_day_of_week_jdn(jdn) == wd, "weekday %d", y);
        { /* every day of the year against the ground truth, both directions */
            int doy;
            for (doy = 0; doy < days; doy++) {
                int32_t m = doy < 186 ? doy / 31 + 1 : (doy - 186) / 30 + 7;
                int32_t d = doy < 186 ? doy % 31 + 1 : (doy - 186) % 30 + 1;
                int64_t back;
                persian_date p = pfj(jdn_in + doy);
                CHECK(p.year == y && p.month == m && p.day == d, "from_jdn %d/%d/%d", y, m, d);
                CHECK(persian_to_jdn(y, m, d, &back) && back == jdn_in + doy, "to_jdn %d/%d/%d", y, m, d);
            }
        }
        n++;
    }
    fclose(f);
    CHECK(n == 1823, "years.csv rows: %d", n);

    n = 0;
    f = open_csv(dir, "test-vectors.csv");
    while (fscanf(f, "%d,%d,%d,%d,%d,%d,%lld,%d", &jy, &jm, &jd, &gy, &gm, &gd, &jdn_in, &wd) == 8) {
        CHECK(persian_to_jdn(jy, jm, jd, &jdn) && jdn == jdn_in, "to_jdn %d/%d/%d", jy, jm, jd);
        CHECK(g2j(gy, gm, gd) == jdn_in, "gregorian_to_jdn %d-%d-%d", gy, gm, gd);
        j2g(jdn, &ry, &rm, &rd);
        CHECK(ry == gy && rm == gm && rd == gd, "to gregorian %d/%d/%d", jy, jm, jd);
        p = pfj(jdn_in);
        CHECK(p.year == jy && p.month == jm && p.day == jd, "from_jdn %lld", jdn_in);
        CHECK(persian_day_of_week_jdn(jdn) == wd, "weekday %d/%d/%d", jy, jm, jd);
        n++;
    }
    fclose(f);
    CHECK(n > 8000, "test-vectors.csv rows: %d", n);

    persian_to_jdn(PERSIAN_VERIFIED_MIN_YEAR, 1, 1, &first);
    persian_to_jdn(PERSIAN_VERIFIED_MAX_YEAR, 12, persian_days_in_month(PERSIAN_VERIFIED_MAX_YEAR, 12), &last);
    prev = pfj(first - 1);
    for (jdn = first; jdn <= last; jdn++) {
        int64_t back;
        int consecutive;
        p = pfj(jdn);
        CHECK(persian_to_jdn(p.year, p.month, p.day, &back) && back == jdn, "round trip %lld", (long long)jdn);
        consecutive = (p.day == prev.day + 1 && p.month == prev.month && p.year == prev.year) ||
                      (p.day == 1 && ((p.month == prev.month + 1 && p.year == prev.year) ||
                                      (p.month == 1 && prev.month == 12 && p.year == prev.year + 1)));
        CHECK(consecutive, "consecutive %lld", (long long)jdn);
        prev = p;
    }

    /* Extrapolated years: years <= 0 make intermediate values negative (floor division matters). */
    CHECK(persian_to_jdn(-1000, 1, 1, &jdn) && jdn == 1582713, "-1000");
    CHECK(persian_to_jdn(0, 1, 1, &jdn) && jdn == 1947955, "0");
    CHECK(persian_to_jdn(4000, 1, 1, &jdn) && jdn == 3408925, "4000");
    p = pfj(0);
    CHECK(p.year == -5334 && p.month == 9 && p.day == 3, "jdn 0");
    CHECK(persian_day_of_week_jdn(1582713) == 1, "weekday -1000");
    CHECK(persian_is_leap_year(-3) && !persian_is_leap_year(-1000), "negative leap years");
    {
        static const int32_t ranges[2][2] = {{-1000, 1177}, {3001, 4000}};
        int r;
        for (r = 0; r < 2; r++) {
            int64_t a, b, back;
            persian_to_jdn(ranges[r][0], 1, 1, &a);
            persian_to_jdn(ranges[r][1], 12, persian_days_in_month(ranges[r][1], 12), &b);
            prev = pfj(a - 1);
            for (jdn = a; jdn <= b; jdn++) {
                int consecutive;
                p = pfj(jdn);
                CHECK(persian_to_jdn(p.year, p.month, p.day, &back) && back == jdn, "round trip %lld", (long long)jdn);
                j2g(jdn, &ry, &rm, &rd);
                CHECK(g2j(ry, rm, rd) == jdn, "gregorian %lld", (long long)jdn);
                consecutive = (p.day == prev.day + 1 && p.month == prev.month && p.year == prev.year) ||
                              (p.day == 1 && ((p.month == prev.month + 1 && p.year == prev.year) ||
                                              (p.month == 1 && prev.month == 12 && p.year == prev.year + 1)));
                CHECK(consecutive, "consecutive %lld", (long long)jdn);
                prev = p;
            }
        }
    }
    CHECK(persian_is_valid(1403, 12, 30), "1403/12/30");
    CHECK(!persian_is_valid(1404, 12, 30), "1404/12/30");
    CHECK(persian_is_valid(1404, 6, 31), "1404/6/31");
    CHECK(!persian_is_valid(1404, 7, 31), "1404/7/31");
    CHECK(!persian_is_valid(1404, 13, 1), "1404/13/1");
    CHECK(!persian_is_valid(1404, 0, 1), "1404/0/1");
    CHECK(!persian_is_valid(1404, 1, 0), "1404/1/0");
    CHECK(!persian_to_jdn(1404, 12, 30, &jdn), "invalid date must fail");
    p = pfj(g2j(2025, 3, 21));
    CHECK(p.year == 1404 && p.month == 1 && p.day == 1, "2025-03-21");
    p = pfj(g2j(1979, 2, 11));
    CHECK(p.year == 1357 && p.month == 11 && p.day == 22, "1979-02-11");
    CHECK(persian_day_of_week(1404, 1, 1) == 6 && persian_day_of_week(1404, 12, 30) == -1, "day_of_week");
    CHECK(persian_day_of_year(1404, 7, 1) == 187 && persian_day_of_year(1403, 12, 30) == 366 && persian_day_of_year(1404, 13, 1) == 0, "day_of_year");
    persian_to_jdn(1404, 1, 1, &jdn);
    CHECK(strcmp(PERSIAN_WEEKDAY_NAMES_EN[persian_day_of_week_jdn(jdn)], "Friday") == 0, "friday");
    CHECK(strcmp(PERSIAN_MONTH_NAMES_FA[0], "فروردین") == 0, "utf-8 month name");
    /* Limits: exact boundaries; everything outside is rejected (no UB, no hang). */
    CHECK(persian_to_jdn(PERSIAN_MIN_YEAR, 1, 1, &jdn) && jdn == PERSIAN_MIN_JDN, "MIN_JDN");
    CHECK(persian_to_jdn(PERSIAN_MAX_YEAR, 12, 29, &jdn) && jdn == PERSIAN_MAX_JDN, "MAX_JDN");
    p = pfj(PERSIAN_MIN_JDN);
    CHECK(p.year == PERSIAN_MIN_YEAR && p.month == 1 && p.day == 1, "from MIN_JDN");
    p = pfj(PERSIAN_MAX_JDN);
    CHECK(p.year == PERSIAN_MAX_YEAR && p.month == 12 && p.day == 29, "from MAX_JDN");
    j2g(PERSIAN_MIN_JDN, &ry, &rm, &rd);
    CHECK(ry == -99379 && rm == 3 && rd == 29, "gregorian MIN_JDN");
    j2g(PERSIAN_MAX_JDN, &ry, &rm, &rd);
    CHECK(ry == 100622 && rm == 3 && rd == 13, "gregorian MAX_JDN");
    {
        static const int64_t bad_jdn[] = {PERSIAN_MIN_JDN - 1, PERSIAN_MAX_JDN + 1, INT64_MAX, INT64_MIN, (int64_t)1 << 53};
        static const int32_t bad_year[] = {PERSIAN_MIN_YEAR - 1, PERSIAN_MAX_YEAR + 1, INT32_MAX, INT32_MIN};
        static const int32_t bad_greg[][3] = {{2025, 2, 29}, {2025, 2, 30}, {2025, 13, 1}, {2025, 0, 1},
                                              {2025, 4, 31}, {2025, 1, 0}, {100622, 3, 14}, {INT32_MAX, 1, 1}};
        size_t i;
        persian_date untouched = {7, 7, 7};
        for (i = 0; i < sizeof bad_jdn / sizeof bad_jdn[0]; i++) {
            CHECK(!persian_from_jdn(bad_jdn[i], &untouched), "from_jdn %lld", (long long)bad_jdn[i]);
            CHECK(!jdn_to_gregorian(bad_jdn[i], &ry, &rm, &rd), "jdn_to_gregorian %lld", (long long)bad_jdn[i]);
            CHECK(persian_day_of_week_jdn(bad_jdn[i]) >= 0 && persian_day_of_week_jdn(bad_jdn[i]) <= 6, "weekday");
        }
        CHECK(untouched.year == 7 && untouched.month == 7 && untouched.day == 7, "output untouched on failure");
        for (i = 0; i < sizeof bad_year / sizeof bad_year[0]; i++) {
            CHECK(!persian_is_valid(bad_year[i], 1, 1), "is_valid %d", bad_year[i]);
            CHECK(!persian_to_jdn(bad_year[i], 1, 1, &jdn), "to_jdn %d", bad_year[i]);
            (void)persian_is_leap_year(bad_year[i]); /* defined for every int32_t */
        }
        for (i = 0; i < sizeof bad_greg / sizeof bad_greg[0]; i++) {
            CHECK(!gregorian_to_jdn(bad_greg[i][0], bad_greg[i][1], bad_greg[i][2], &jdn), "gregorian %d", (int)i);
        }
        CHECK(!persian_from_jdn(2460756, NULL) && !persian_to_jdn(1404, 1, 1, NULL) &&
              !gregorian_to_jdn(2025, 3, 21, NULL) && !jdn_to_gregorian(2460756, NULL, &rm, &rd), "NULL outputs");
        CHECK(g2j(2024, 2, 29) == 2460370, "Gregorian leap day");
    }
    /* Every year MIN..MAX: years are contiguous, first and last day round-trip, and the leap years
     * sit where every other implementation (and ICU 77.1+) puts them (checksums). */
    {
        int64_t expected = PERSIAN_MIN_JDN, leaps = 0, nowruz_sum = 0;
        int32_t y;
        for (y = PERSIAN_MIN_YEAR; y <= PERSIAN_MAX_YEAR; y++) {
            int32_t leap = persian_is_leap_year(y) ? 1 : 0;
            persian_date first, last;
            CHECK(persian_to_jdn(y, 1, 1, &jdn) && jdn == expected, "nowruz %d", (int)y);
            first = pfj(jdn);
            last = pfj(jdn + 364 + leap);
            CHECK(first.year == y && first.month == 1 && first.day == 1 && persian_days_in_month(y, 12) == 29 + leap &&
                      last.year == y && last.month == 12 && last.day == 29 + leap,
                  "year %d", (int)y);
            leaps += leap;
            nowruz_sum += jdn;
            expected = jdn + 365 + leap;
        }
        CHECK(expected - 1 == PERSIAN_MAX_JDN && leaps == 48485 && nowruz_sum == INT64_C(389592978180), "full-range checksums");
    }
    printf("OK: %ld checks passed\n", checks);
    return 0;
}
```

---

نشانی صفحه: https://goudarzjafari.com/fa/persian-calendar/implementations/c/
مجوز: متن CC BY 4.0، کد MIT، داده CC0 1.0. نسخهٔ 1.0.0 از https://github.com/Goudarz/PersianCalendar
