├── NEWS ├── debian ├── compat ├── libcalendars1.dirs ├── source │ └── format ├── libcalendars1.install ├── libcalendars-dev.dirs ├── libcalendars-dev.install ├── shlibs.local ├── changelog ├── rules ├── control └── copyright ├── AUTHORS ├── lib ├── win32 │ └── libcalendars.rc ├── cmake │ └── libcalendarsConfig.cmake ├── libcalendars.pc.in ├── src │ ├── cl-math.h │ ├── cl-math.c │ ├── cl-egyptian.c │ ├── cl-gregorian.c │ ├── cl-islamic-civil.c │ ├── cl-babylonian.c │ ├── cl-julian.c │ ├── cl-milankovic.c │ ├── cl-solar-hijri.c │ ├── cl-calendars.c │ └── cl-jewish.c ├── include │ └── libcalendars │ │ ├── cl-gregorian.h │ │ ├── cl-export.h │ │ ├── cl-julian.h │ │ ├── cl-egyptian.h │ │ ├── cl-babylonian.h │ │ ├── cl-milankovic.h │ │ ├── cl-solar-hijri.h │ │ ├── cl-islamic-civil.h │ │ ├── cl-jewish.h │ │ └── cl-calendars.h └── CMakeLists.txt ├── ChangeLog ├── CMakeLists.txt ├── tests ├── jewish.c ├── egyptian.c ├── babylonian.c ├── milankovic.c ├── islamic-civil.c ├── gregorian.c ├── julian.c ├── solar-hijri.c ├── calendar-arithmetic.h ├── CMakeLists.txt └── calendar-arithmetic.c ├── .travis.yml ├── appveyor.yml ├── rpm └── libcalendars.spec ├── CODE_OF_CONDUCT.md ├── .gitignore ├── README ├── README.md └── COPYING /NEWS: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /debian/libcalendars1.dirs: -------------------------------------------------------------------------------- 1 | usr/lib 2 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (quilt) 2 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Soroush Rabiei 2 | -------------------------------------------------------------------------------- /debian/libcalendars1.install: -------------------------------------------------------------------------------- 1 | usr/lib/lib*.so.* 2 | -------------------------------------------------------------------------------- /debian/libcalendars-dev.dirs: -------------------------------------------------------------------------------- 1 | usr/lib 2 | usr/include 3 | -------------------------------------------------------------------------------- /debian/libcalendars-dev.install: -------------------------------------------------------------------------------- 1 | usr/include/* 2 | usr/lib/lib*.so 3 | usr/lib/pkgconfig/* 4 | -------------------------------------------------------------------------------- /debian/shlibs.local: -------------------------------------------------------------------------------- 1 | liblibcalendars 1.0.0 libcalendars (>> 1.0.0-0), libcalendars (<< 1.0.0-99) 2 | -------------------------------------------------------------------------------- /lib/win32/libcalendars.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soroush/libcalendars/HEAD/lib/win32/libcalendars.rc -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2017-08-29 Soroush Rabiei 2 | 3 | * *.*: Initial implementation for Solar Hijri calendar added. 4 | 5 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | libcalendars (1.0.0-1) artful; urgency=medium 2 | 3 | * Initial release 4 | 5 | -- Soroush Rabiei Wed, 14 Mar 2018 23:39:49 +0330 6 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | # See debhelper(7) (uncomment to enable) 3 | # output every command that modifies files on the build system. 4 | #export DH_VERBOSE = 1 5 | 6 | 7 | # see FEATURE AREAS in dpkg-buildflags(1) 8 | #export DEB_BUILD_MAINT_OPTIONS = hardening=+all 9 | 10 | # see ENVIRONMENT in dpkg-buildflags(1) 11 | # package maintainers to append CFLAGS 12 | #export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic 13 | # package maintainers to append LDFLAGS 14 | #export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed 15 | 16 | include /usr/share/cdbs/1/rules/debhelper.mk 17 | include /usr/share/cdbs/1/class/autotools.mk 18 | 19 | # dh_make generated override targets 20 | # This is example for Cmake (See https://bugs.debian.org/641051 ) 21 | #override_dh_auto_configure: 22 | # dh_auto_configure -- # -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) 23 | 24 | -------------------------------------------------------------------------------- /lib/cmake/libcalendarsConfig.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021-2025 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | 17 | include(CMakeFindDependencyMacro) 18 | include("${CMAKE_CURRENT_LIST_DIR}/libcalendarsTargets.cmake") -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: libcalendars 2 | Priority: optional 3 | Maintainer: Soroush Rabiei 4 | Build-Depends: debhelper (>= 9), autotools-dev, cdbs 5 | Standards-Version: 3.9.8 6 | Section: libs 7 | Homepage: https://soroush.github.io/libcalendars 8 | Vcs-Git: https://github.com/soroush/libcalendars.git 9 | Vcs-Browser: https://github.com/soroush/libcalendars 10 | 11 | Package: libcalendars-dev 12 | Section: libdevel 13 | Architecture: any 14 | Depends: libcalendars1 (= ${binary:Version}), ${misc:Depends} 15 | Description: Calendar Arithmetic library - development headers 16 | Header files and develipment libs for libcalendars. 17 | 18 | Package: libcalendars1 19 | Architecture: any 20 | Depends: ${shlibs:Depends}, ${misc:Depends} 21 | Description: Calendar Arithmetic library 22 | A precise C library to provide arithmentic for the most common calendar 23 | systems. Currently Gregorian, Julian, Milankovic, Solar Hijri (also known as 24 | Shamsi or Jalali), Islamic Civil and Jewish (also know as Hebrew) calendar 25 | systems are provided. 26 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021-2025 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | # 17 | 18 | cmake_minimum_required(VERSION 3.16.0) 19 | 20 | project(calendars 21 | VERSION 1.1.0 22 | DESCRIPTION "A calendar arithmetic library" 23 | HOMEPAGE_URL "https://github.com/soroush/libcalendars" 24 | LANGUAGES C 25 | ) 26 | 27 | enable_testing() 28 | 29 | add_subdirectory(lib) 30 | add_subdirectory(tests) 31 | -------------------------------------------------------------------------------- /lib/libcalendars.pc.in: -------------------------------------------------------------------------------- 1 | # Copyright 2021 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | 17 | prefix=@prefix@ 18 | exec_prefix=@exec_prefix@ 19 | libdir=@libdir@ 20 | includedir=@includedir@ 21 | 22 | Name: @PACKAGE_NAME@ 23 | Description: A Calendar Arithmetic Library 24 | Version: @PACKAGE_VERSION@ 25 | URL: @PACKAGE_URL@ 26 | #Requires: @AX_PACKAGE_REQUIRES@ 27 | #Requires.private: @AX_PACKAGE_REQUIRES_PRIVATE@ 28 | Libs: -L${libdir} -lcalendars -lm 29 | Cflags: -I${includedir}/ 30 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Upstream-Name: libcalendars 3 | Source: https://github.com/soroush/libcalendars 4 | 5 | Files: * 6 | Copyright: 2017 - 2018 Soroush Rabiei 7 | License: GPL-3.0+ 8 | 9 | Files: debian/* 10 | Copyright: 2018 Soroush Rabiei 11 | License: GPL-3.0+ 12 | 13 | License: GPL-3.0+ 14 | This program is free software: you can redistribute it and/or modify 15 | it under the terms of the GNU General Public License as published by 16 | the Free Software Foundation, either version 3 of the License, or 17 | (at your option) any later version. 18 | . 19 | This package is distributed in the hope that it will be useful, 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | GNU General Public License for more details. 23 | . 24 | You should have received a copy of the GNU General Public License 25 | along with this program. If not, see . 26 | . 27 | On Debian systems, the complete text of the GNU General 28 | Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". 29 | 30 | -------------------------------------------------------------------------------- /lib/src/cl-math.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_MATH_H 21 | #define LIBCALENDAR_MATH_H 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /** 31 | * \brief Division with Positive Ramainder 32 | */ 33 | div_t pdiv(int d, int v); 34 | 35 | /** 36 | * \brief Floor Division Function 37 | */ 38 | int fdiv(int a, int b); 39 | 40 | /** 41 | * \brief Modular Division Function 42 | */ 43 | int mod(int a, int b); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif /* LIBCALENDAR_MATH_H */ 50 | -------------------------------------------------------------------------------- /tests/jewish.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int main(void) 29 | { 30 | const test_context ctx = { 31 | .to_jdn = &jw_to_jdn, 32 | .from_jdn = &jdn_to_jw, 33 | .to_gr = &jw_to_gr, 34 | .from_gr = &gr_to_jw, 35 | .jdn_to_gr = &jdn_to_gr, 36 | .min_jd = 0, 37 | .max_jd = 2488069, 38 | }; 39 | 40 | test_julian_day(&ctx); 41 | test_gregorian_calendar(&ctx); 42 | } -------------------------------------------------------------------------------- /tests/egyptian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int main(void) 29 | { 30 | const test_context ctx = { 31 | .to_jdn = &eg_to_jdn, 32 | .from_jdn = &jdn_to_eg, 33 | .to_gr = &eg_to_gr, 34 | .from_gr = &gr_to_eg, 35 | .jdn_to_gr = &jdn_to_gr, 36 | .min_jd = 0, 37 | .max_jd = 2488069, 38 | }; 39 | 40 | test_julian_day(&ctx); 41 | test_gregorian_calendar(&ctx); 42 | } -------------------------------------------------------------------------------- /tests/babylonian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int main(void) 29 | { 30 | const test_context ctx = { 31 | .to_jdn = &ba_to_jdn, 32 | .from_jdn = &jdn_to_ba, 33 | .to_gr = &ba_to_gr, 34 | .from_gr = &gr_to_ba, 35 | .jdn_to_gr = &jdn_to_gr, 36 | .min_jd = 0, 37 | .max_jd = 2488069, 38 | }; 39 | 40 | test_julian_day(&ctx); 41 | test_gregorian_calendar(&ctx); 42 | } -------------------------------------------------------------------------------- /tests/milankovic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int main(void) 29 | { 30 | const test_context ctx = { 31 | .to_jdn = &ml_to_jdn, 32 | .from_jdn = &jdn_to_ml, 33 | .to_gr = &ml_to_gr, 34 | .from_gr = &gr_to_ml, 35 | .jdn_to_gr = &jdn_to_gr, 36 | .min_jd = 0, 37 | .max_jd = 2488069, 38 | }; 39 | 40 | test_julian_day(&ctx); 41 | test_gregorian_calendar(&ctx); 42 | } -------------------------------------------------------------------------------- /tests/islamic-civil.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int main(void) 29 | { 30 | const test_context ctx = { 31 | .to_jdn = &is_to_jdn, 32 | .from_jdn = &jdn_to_is, 33 | .to_gr = &is_to_gr, 34 | .from_gr = &gr_to_is, 35 | .jdn_to_gr = &jdn_to_gr, 36 | .min_jd = 0, 37 | .max_jd = 2488069, 38 | }; 39 | 40 | test_julian_day(&ctx); 41 | test_gregorian_calendar(&ctx); 42 | } -------------------------------------------------------------------------------- /tests/gregorian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | int main(void) 28 | { 29 | const test_context ctx = { 30 | .to_jdn = &gr_to_jdn, 31 | .from_jdn = &jdn_to_gr, 32 | .to_gr = NULL, 33 | .from_gr = NULL, 34 | .jdn_to_gr = &jdn_to_gr, 35 | .days_in_month = &gr_days_in_month, 36 | .days_in_year = &gr_days_in_year, 37 | .month_in_year = &gr_month_in_year, 38 | .min_jd = 0, 39 | .max_jd = 2488069, 40 | }; 41 | 42 | test_julian_day(&ctx); 43 | test_continuity(&ctx); 44 | 45 | return EXIT_SUCCESS; 46 | } -------------------------------------------------------------------------------- /tests/julian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int main(void) 29 | { 30 | const test_context ctx = { 31 | .to_jdn = &ju_to_jdn, 32 | .from_jdn = &jdn_to_ju, 33 | .to_gr = &ju_to_gr, 34 | .from_gr = &gr_to_ju, 35 | .jdn_to_gr = &jdn_to_gr, 36 | .days_in_month = &ju_days_in_month, 37 | .days_in_year = &ju_days_in_year, 38 | .month_in_year = &ju_month_in_year, 39 | .min_jd = 0, 40 | .max_jd = 2488069, 41 | }; 42 | 43 | test_julian_day(&ctx); 44 | test_gregorian_calendar(&ctx); 45 | test_continuity(&ctx); 46 | 47 | return EXIT_SUCCESS; 48 | } -------------------------------------------------------------------------------- /tests/solar-hijri.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include "calendar-arithmetic.h" 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int main(void) 29 | { 30 | const test_context ctx = { 31 | .to_jdn = &sh_to_jdn, 32 | .from_jdn = &jdn_to_sh, 33 | .to_gr = &sh_to_gr, 34 | .from_gr = &gr_to_sh, 35 | .jdn_to_gr = &jdn_to_gr, 36 | .days_in_month = &sh_days_in_month, 37 | .days_in_year = &sh_days_in_year, 38 | .month_in_year = &sh_month_in_year, 39 | .min_jd = 0, 40 | .max_jd = 2488069, 41 | }; 42 | 43 | test_julian_day(&ctx); 44 | test_gregorian_calendar(&ctx); 45 | // test_continuity(&ctx); 46 | 47 | return EXIT_SUCCESS; 48 | } -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-gregorian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_GREGORIAN_H 21 | #define LIBCALENDAR_GREGORIAN_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t gr_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t gr_days_in_month(uint8_t month, int16_t year); 32 | LIBCALENDAR_API uint16_t gr_days_in_year(int16_t year); 33 | LIBCALENDAR_API uint8_t gr_month_in_year(int16_t year); 34 | LIBCALENDAR_API uint8_t gr_is_valid(int16_t year, uint8_t month, uint16_t day); 35 | LIBCALENDAR_API void gr_to_jdn(uint32_t* jd, 36 | int16_t year, uint8_t month, uint16_t day); 37 | LIBCALENDAR_API void jdn_to_gr(uint32_t jdn, 38 | int16_t* year, uint8_t* month, uint16_t* day); 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif /* LIBCALENDAR_GREGORIAN_H */ 45 | -------------------------------------------------------------------------------- /tests/calendar-arithmetic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | #include 24 | 25 | typedef struct test_context_t 26 | { 27 | void (*to_jdn)(uint32_t*, int16_t, uint8_t, uint16_t); 28 | void (*from_jdn)(uint32_t, int16_t*, uint8_t*, uint16_t*); 29 | void (*to_gr)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*); 30 | void (*from_gr)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*); 31 | void (*jdn_to_gr)(uint32_t, int16_t*, uint8_t*, uint16_t*); 32 | uint8_t (*days_in_month)(uint8_t month, int16_t year); 33 | uint16_t (*days_in_year)(int16_t year); 34 | uint8_t (*month_in_year)(int16_t year); 35 | uint8_t (*is_valid)(int16_t year, uint8_t month, uint16_t day); 36 | uint32_t min_jd; 37 | uint32_t max_jd; 38 | } test_context; 39 | 40 | void test_julian_day(const test_context* const ctx); 41 | void test_gregorian_calendar(const test_context* const ctx); 42 | void test_continuity(const test_context* const ctx); -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-export.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDARS_EXPORT_H 21 | #define LIBCALENDARS_EXPORT_H 22 | 23 | #if defined libcalendars_STATIC 24 | #define LIBCALENDAR_API 25 | #define LIBCALENDAR_PRIVATE 26 | #else 27 | #if defined _WIN32 || defined __CYGWIN__ || defined __MINGW32__ 28 | #ifdef libcalendars_EXPORTS 29 | #ifdef __GNUC__ 30 | #define LIBCALENDAR_API __attribute__ ((dllexport)) 31 | #else 32 | #define LIBCALENDAR_API __declspec(dllexport) 33 | #endif 34 | #else 35 | #ifdef __GNUC__ 36 | #define LIBCALENDAR_API __attribute__ ((dllimport)) 37 | #else 38 | #define LIBCALENDAR_API __declspec(dllimport) 39 | #endif 40 | #endif 41 | #define LIBCALENDAR_PRIVATE 42 | #else 43 | #if __GNUC__ >= 4 44 | #define LIBCALENDAR_API __attribute__ ((visibility ("default"))) 45 | #define LIBCALENDAR_PRIVATE __attribute__ ((visibility ("hidden"))) 46 | #else 47 | #define LIBCALENDAR_API 48 | #define LIBCALENDAR_PRIVATE 49 | #endif 50 | #endif 51 | #endif 52 | 53 | #endif /* LIBCALENDARS_EXPORT_H */ -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-julian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_JULIAN_H 21 | #define LIBCALENDAR_JULIAN_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t ju_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t ju_days_in_month(uint8_t month, int16_t year); 32 | LIBCALENDAR_API uint16_t ju_days_in_year(int16_t year); 33 | LIBCALENDAR_API uint8_t ju_month_in_year(int16_t year); 34 | LIBCALENDAR_API uint8_t ju_is_valid(int16_t year, uint8_t month, uint16_t day); 35 | LIBCALENDAR_API void ju_to_jdn(uint32_t* jd, 36 | int16_t year, uint8_t month, uint16_t day); 37 | LIBCALENDAR_API void jdn_to_ju(uint32_t jdn, 38 | int16_t* year, uint8_t* month, uint16_t* day); 39 | LIBCALENDAR_API void ju_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 40 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday); 41 | LIBCALENDAR_API void gr_to_ju(int16_t gyear, uint8_t gmonth, uint16_t gday, 42 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* LIBCALENDAR_GREGORIAN_H */ 49 | -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-egyptian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_EGYPTIAN_H 21 | #define LIBCALENDAR_EGYPTIAN_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t eg_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t eg_days_in_month(uint8_t month, int16_t year); 32 | LIBCALENDAR_API uint16_t eg_days_in_year(int16_t year); 33 | LIBCALENDAR_API uint8_t eg_month_in_year(int16_t year); 34 | LIBCALENDAR_API uint8_t eg_is_valid(int16_t year, uint8_t month, uint16_t day); 35 | LIBCALENDAR_API void eg_to_jdn(uint32_t* jd, 36 | int16_t year, uint8_t month, uint16_t day); 37 | LIBCALENDAR_API void jdn_to_eg(uint32_t jdn, 38 | int16_t* year, uint8_t* month, uint16_t* day); 39 | LIBCALENDAR_API void eg_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 40 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday); 41 | LIBCALENDAR_API void gr_to_eg(int16_t gyear, uint8_t gmonth, uint16_t gday, 42 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* LIBCALENDAR_EGYPTIAN_H */ 49 | -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-babylonian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_BABYLONIAN_H 21 | #define LIBCALENDAR_BABYLONIAN_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t ba_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t ba_days_in_month(uint8_t month, int16_t year); 32 | LIBCALENDAR_API uint16_t ba_days_in_year(int16_t year); 33 | LIBCALENDAR_API uint8_t ba_month_in_year(int16_t year); 34 | LIBCALENDAR_API uint8_t ba_is_valid(int16_t year, uint8_t month, uint16_t day); 35 | LIBCALENDAR_API void ba_to_jdn(uint32_t* jd, 36 | int16_t year, uint8_t month, uint16_t day); 37 | LIBCALENDAR_API void jdn_to_ba(uint32_t jdn, 38 | int16_t* year, uint8_t* month, uint16_t* day); 39 | LIBCALENDAR_API void ba_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 40 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday); 41 | LIBCALENDAR_API void gr_to_ba(int16_t gyear, uint8_t gmonth, uint16_t gday, 42 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* LIBCALENDAR_BABYLONIAN_H */ 49 | -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-milankovic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_MILANKOVIC_H 21 | #define LIBCALENDAR_MILANKOVIC_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t ml_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t ml_days_in_month(uint8_t month, int16_t year); 32 | LIBCALENDAR_API uint16_t ml_days_in_year(int16_t year); 33 | LIBCALENDAR_API uint8_t ml_month_in_year(int16_t year); 34 | LIBCALENDAR_API uint8_t ml_is_valid(int16_t year, uint8_t month, uint16_t day); 35 | LIBCALENDAR_API void ml_to_jdn(uint32_t* jd, 36 | int16_t year, uint8_t month, uint16_t day); 37 | LIBCALENDAR_API void jdn_to_ml(uint32_t jdn, 38 | int16_t* year, uint8_t* month, uint16_t* day); 39 | LIBCALENDAR_API void ml_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 40 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday); 41 | LIBCALENDAR_API void gr_to_ml(int16_t gyear, uint8_t gmonth, uint16_t gday, 42 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* LIBCALENDAR_MILANKOVIC_H */ 49 | -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-solar-hijri.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_SOLAR_HIJRI_H 21 | #define LIBCALENDAR_SOLAR_HIJRI_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t sh_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t sh_days_in_month(uint8_t month, int16_t year); 32 | LIBCALENDAR_API uint16_t sh_days_in_year(int16_t year); 33 | LIBCALENDAR_API uint8_t sh_month_in_year(int16_t year); 34 | LIBCALENDAR_API uint8_t sh_is_valid(int16_t year, uint8_t month, uint16_t day); 35 | LIBCALENDAR_API void sh_to_jdn(uint32_t* jd, 36 | int16_t year, uint8_t month, uint16_t day); 37 | LIBCALENDAR_API void jdn_to_sh(uint32_t jdn, 38 | int16_t* year, uint8_t* month, uint16_t* day); 39 | LIBCALENDAR_API void sh_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 40 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday); 41 | LIBCALENDAR_API void gr_to_sh(int16_t gyear, uint8_t gmonth, uint16_t gday, 42 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* LIBCALENDAR_SOLAR_HIJRI_H */ 49 | -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-islamic-civil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_ISLAMIC_CIVIL_H 21 | #define LIBCALENDAR_ISLAMIC_CIVIL_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t is_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t is_days_in_month(uint8_t month, int16_t year); 32 | LIBCALENDAR_API uint16_t is_days_in_year(int16_t year); 33 | LIBCALENDAR_API uint8_t is_month_in_year(int16_t year); 34 | LIBCALENDAR_API uint8_t is_is_valid(int16_t year, uint8_t month, uint16_t day); 35 | 36 | LIBCALENDAR_API void is_to_jdn(uint32_t* jd, 37 | int16_t year, uint8_t month, uint16_t day); 38 | LIBCALENDAR_API void jdn_to_is(uint32_t jdn, 39 | int16_t* year, uint8_t* month, uint16_t* day); 40 | LIBCALENDAR_API void is_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 41 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday); 42 | LIBCALENDAR_API void gr_to_is(int16_t gyear, uint8_t gmonth, uint16_t gday, 43 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif /* LIBCALENDAR_ISLAMIC_CIVIL_H */ 50 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021-2025 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | # 17 | 18 | language: c 19 | matrix: 20 | include: 21 | - os: linux 22 | addons: 23 | apt: 24 | sources: 25 | - ubuntu-toolchain-r-test 26 | packages: 27 | - g++-4.7 28 | env: 29 | - MATRIX_EVAL="CC=gcc-4.7" 30 | 31 | - os: linux 32 | addons: 33 | apt: 34 | sources: 35 | - ubuntu-toolchain-r-test 36 | packages: 37 | - g++-4.8 38 | env: 39 | - MATRIX_EVAL="CC=gcc-4.8" 40 | 41 | - os: linux 42 | addons: 43 | apt: 44 | sources: 45 | - ubuntu-toolchain-r-test 46 | packages: 47 | - g++-4.9 48 | env: 49 | - MATRIX_EVAL="CC=gcc-4.9" 50 | 51 | - os: linux 52 | addons: 53 | apt: 54 | sources: 55 | - ubuntu-toolchain-r-test 56 | packages: 57 | - g++-5 58 | env: 59 | - MATRIX_EVAL="CC=gcc-5" 60 | 61 | - os: linux 62 | addons: 63 | apt: 64 | sources: 65 | - ubuntu-toolchain-r-test 66 | packages: 67 | - g++-6 68 | env: 69 | - MATRIX_EVAL="CC=gcc-6" 70 | 71 | before_install: 72 | - eval "${MATRIX_EVAL}" 73 | 74 | script: autoreconf -f -i && ./configure && make && make check 75 | -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-jewish.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_JEWISH_H 21 | #define LIBCALENDAR_JEWISH_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | LIBCALENDAR_API uint8_t jw_is_leap(int16_t year); 31 | LIBCALENDAR_API uint8_t jw_is_complete(int16_t year); 32 | LIBCALENDAR_API uint8_t jw_is_deficient(int16_t year); 33 | LIBCALENDAR_API uint8_t jw_is_regular(int16_t year); 34 | LIBCALENDAR_API uint8_t jw_days_in_month(uint8_t month, int16_t year); 35 | LIBCALENDAR_API uint16_t jw_days_in_year(int16_t year); 36 | LIBCALENDAR_API uint8_t jw_month_in_year(int16_t year); 37 | LIBCALENDAR_API uint8_t jw_is_valid(int16_t year, uint8_t month, uint16_t day); 38 | LIBCALENDAR_API void jw_to_jdn(uint32_t* jd, 39 | int16_t year, uint8_t month, uint16_t day); 40 | LIBCALENDAR_API void jdn_to_jw(uint32_t jdn, 41 | int16_t* year, uint8_t* month, uint16_t* day); 42 | LIBCALENDAR_API void jw_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 43 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday); 44 | LIBCALENDAR_API void gr_to_jw(int16_t gyear, uint8_t gmonth, uint16_t gday, 45 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday); 46 | 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #endif /* LIBCALENDAR_JEWISH_H */ 52 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021-2025 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | 17 | version: 0.1.{build} 18 | image: 19 | - Visual Studio 2017 20 | - Visual Studio 2015 21 | 22 | configuration: 23 | - Debug 24 | - Release 25 | - StaticRelease 26 | - StaticDebug 27 | 28 | platform: 29 | - x64 30 | - x86 31 | 32 | before_build: 33 | - ps: >- 34 | mkdir C:\sysroot 35 | 36 | mkdir C:\sysroot\bin 37 | 38 | mkdir C:\sysroot\include 39 | 40 | mkdir C:\sysroot\lib 41 | 42 | environment: 43 | SYSROOT: C:\sysroot\ 44 | INCLUDE: C:\sysroot\include\ 45 | LIBS: C:\sysroot\libs\ 46 | 47 | build: 48 | project: win32/libcalendars.sln 49 | verbosity: detailed 50 | 51 | after_build: 52 | - ps: >- 53 | $env:BUILD_ARCH = $env:PLATFORM 54 | 55 | switch($env:CONFIGURATION) 56 | { 57 | "Debug" {$env:BUILD_TYPE = "debug-shared"} 58 | "Release" {$env:BUILD_TYPE = "release-shared"} 59 | "StaticDebug" {$env:BUILD_TYPE = "debug-static"} 60 | "StaticRelease" {$env:BUILD_TYPE = "release-static"} 61 | } 62 | 63 | switch($env:APPVEYOR_BUILD_WORKER_IMAGE) 64 | { 65 | "Visual Studio 2017" {$env:BUILD_MSVC = "msvc14_1"} 66 | "Visual Studio 2015" {$env:BUILD_MSVC = "msvc14"} 67 | } 68 | 69 | - cmd: 7z a .\%APPVEYOR_PROJECT_NAME%-%BUILD_ARCH%-%BUILD_MSVC%-%BUILD_TYPE%.zip C:\sysroot\* 70 | 71 | artifacts: 72 | - path: .\*.zip 73 | type: zip 74 | 75 | skip_commits: 76 | message: /\[ci skip\]/ -------------------------------------------------------------------------------- /lib/src/cl-math.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include "cl-math.h" 21 | 22 | /** 23 | * Division of a whole number \f$y\f$ by another whole number \f$x\f$ 24 | * results in a quotient \f$q\f$ and a remainder \f$r\f$. In our calculations, 25 | * the remainder never should be negative. Then \f$0 ≤ r \lt \left|x\right|\f$ 26 | * As a result: 27 | * 28 | * \f{eqnarray*}{ 29 | * q &=& \left\lfloor\frac{y}{x}\right\rfloor 30 | * \\ r &=& y \bmod x = y − x\left\lfloor\frac{y}{x}\right\rfloor 31 | * \\ y &=& qx + r 32 | * \f} 33 | */ 34 | div_t pdiv(int y, int x) { 35 | div_t rv = div(y, x); 36 | if(rv.rem < 0) { 37 | if(x>0) { 38 | rv.quot -= 1; 39 | rv.rem += x; 40 | } else { 41 | rv.quot += 1; 42 | rv.rem -= x; 43 | } 44 | } 45 | return rv; 46 | } 47 | 48 | /** 49 | * C implentation of integer division results in truncation toward zero. While 50 | * we ofthen need floor, ceil or rounding. This function simulates floor 51 | * division in an optimized way. It returns: 52 | * 53 | * \f[ 54 | * \left\lfloor\frac{a}{b}\right\rfloor 55 | * \f] 56 | */ 57 | int fdiv(int a, int b) { 58 | return (a - (a < 0 ? b - 1 : 0)) / b; 59 | } 60 | 61 | /** 62 | * This function returns non-negative remainder from division of 63 | * \f$y\f$ by \f$x\f$, i.e. \f$x \bmod y\f$ 64 | * 65 | * \f[ 66 | * x \bmod y=x-y\left\lfloor\frac{x}{y}\right\rfloor 67 | * \f] 68 | */ 69 | int mod(int x, int y) { 70 | return x - fdiv(x, y) * y; 71 | } 72 | -------------------------------------------------------------------------------- /lib/src/cl-egyptian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "cl-math.h" 25 | 26 | LIBCALENDAR_API 27 | uint8_t eg_is_leap(int16_t year) { 28 | return 0; 29 | } 30 | 31 | LIBCALENDAR_API 32 | uint8_t eg_days_in_month(uint8_t month, int16_t year) { 33 | if(month == 13) 34 | return 5; 35 | return 30; 36 | } 37 | 38 | LIBCALENDAR_API 39 | uint16_t eg_days_in_year(int16_t year) { 40 | return 365; 41 | } 42 | 43 | LIBCALENDAR_API 44 | uint8_t eg_month_in_year(int16_t year) { 45 | return 13; 46 | } 47 | 48 | LIBCALENDAR_API 49 | uint8_t eg_is_valid(int16_t year, uint8_t month, uint16_t day) { 50 | if(day > 0 && day <= eg_days_in_month(month, year)) { 51 | return 1; 52 | } 53 | return 0; 54 | } 55 | 56 | LIBCALENDAR_API 57 | void eg_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) { 58 | *jd = 365 * year + 30 * month + day + 1448242; 59 | } 60 | 61 | LIBCALENDAR_API 62 | void jdn_to_eg(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) { 63 | const int32_t y2 = jd - 1448638; 64 | const int32_t x2 = fdiv(y2, 365); 65 | const int32_t y1 = mod(y2, 365); 66 | *year = (int16_t)(x2 + 1); 67 | *month = (uint8_t)(fdiv(y1, 30) + 1); 68 | *day = (uint8_t)(mod(y1, 30) + 1); 69 | } 70 | 71 | LIBCALENDAR_API 72 | void eg_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 73 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday) { 74 | uint32_t jdn = 0; 75 | eg_to_jdn(&jdn, jyear, jmonth, jday); 76 | jdn_to_gr(jdn, gyear, gmonth, gday); 77 | } 78 | 79 | LIBCALENDAR_API 80 | void gr_to_eg(int16_t gyear, uint8_t gmonth, uint16_t gday, 81 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday) { 82 | uint32_t jdn = 0; 83 | gr_to_jdn(&jdn, gyear, gmonth, gday); 84 | jdn_to_eg(jdn, jyear, jmonth, jday); 85 | } 86 | -------------------------------------------------------------------------------- /rpm/libcalendars.spec: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021-2025 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | 17 | %define name libcalendars 18 | %define version 1.0.0 19 | %define release 1 20 | 21 | Name: %{name} 22 | Version: %{version} 23 | Release: %{release}%{?dist} 24 | License: GPLv3 25 | URL: https://github.com/soroush/libcalendars 26 | Source0: %{name}-%{version}.tar.gz 27 | BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) 28 | Summary: A calendar arithmetic library 29 | Group: Development/Libraries 30 | 31 | BuildRequires: gcc 32 | BuildRequires: libtool 33 | BuildRequires: make 34 | 35 | %package devel 36 | Summary: Development files for using ${name} 37 | Group: Development/Libraries 38 | Requires: %{name} = %{version}-%{release} 39 | Requires: pkgconfig 40 | Provides: %{name}-devel = %{version}-%{release} 41 | 42 | %description 43 | A precise C library to provide arithmentic for the most common 44 | calendar systems. Currently Gregorian, Julian, Milankovic, Solar 45 | Hijri (also known as Shamsi or Jalali), Islamic Civil and 46 | Jewish (also know as Hebrew) calendar systems are provided. 47 | 48 | 49 | %description devel 50 | This package contain development files for ${name}. It should be 51 | installed if you want to develop programs that will use the ${name} 52 | library. 53 | 54 | %prep 55 | %setup -q 56 | 57 | %build 58 | %configure 59 | make %{?_smp_mflags} 60 | 61 | %install 62 | rm -rf %{buildroot} 63 | %make_install 64 | 65 | %clean 66 | rm -rf %{buildroot} 67 | 68 | %post -p /sbin/ldconfig 69 | 70 | %postun -p /sbin/ldconfig 71 | 72 | %files 73 | %{_libdir}/*.so.* 74 | %exclude %{_libdir}/*.a* 75 | %exclude %{_libdir}/*.la* 76 | %exclude %{_libdir}/pkgconfig/* 77 | %exclude %{_includedir}/* 78 | 79 | %files devel 80 | %{_libdir}/*.so 81 | %{_libdir}/*.a* 82 | %{_libdir}/*.la* 83 | %{_libdir}/pkgconfig/* 84 | %{_includedir}/* 85 | %exclude %{_libdir}/*.so.* 86 | 87 | %changelog 88 | * Tue Nov 20 2018 Soroush Rabiei 1.0-1 89 | - Initial package files for the library (release 1) 90 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021-2025 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | 17 | cmake_minimum_required(VERSION 3.16.0) 18 | 19 | include_directories(PUBLIC $) 20 | 21 | # Commont test mathematics 22 | add_library(calendar_test_common OBJECT calendar-arithmetic.c) 23 | target_link_libraries(calendar_test_common PUBLIC calendars) 24 | 25 | link_libraries(calendar_test_common calendars) 26 | 27 | add_executable(gregorian gregorian.c) 28 | add_executable(milankovic milankovic.c) 29 | add_executable(julian julian.c) 30 | add_executable(jewish jewish.c) 31 | add_executable(islamic_civil islamic-civil.c) 32 | add_executable(solar_hijri solar-hijri.c) 33 | add_executable(egyptian egyptian.c) 34 | add_executable(babylonian babylonian.c) 35 | 36 | set_target_properties( 37 | gregorian julian milankovic solar_hijri jewish islamic_civil egyptian babylonian 38 | PROPERTIES 39 | C_STANDARD 90 40 | DEBUG_POSTFIX "_d" 41 | ) 42 | 43 | # Add tests 44 | 45 | add_test(NAME "Gregorian" COMMAND gregorian WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 46 | add_test(NAME "Julian" COMMAND julian WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 47 | add_test(NAME "Milankovic" COMMAND milankovic WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 48 | add_test(NAME "SolarHijri" COMMAND solar_hijri WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 49 | add_test(NAME "Jewish" COMMAND jewish WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 50 | add_test(NAME "IslamicCivil" COMMAND islamic_civil WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 51 | add_test(NAME "Egyptian" COMMAND egyptian WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 52 | add_test(NAME "Babylonian" COMMAND babylonian WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 53 | 54 | if(WIN32) 55 | set_tests_properties(Gregorian Julian Milankovic SolarHijri Jewish IslamicCivil Egyptian Babylonian PROPERTIES 56 | ENVIRONMENT "PATH=$;$ENV{PATH}" 57 | ) 58 | else() 59 | set_tests_properties(Gregorian Julian Milankovic SolarHijri Jewish IslamicCivil Egyptian Babylonian PROPERTIES 60 | ENVIRONMENT "LD_LIBRARY_PATH=$:$ENV{LD_LIBRARY_PATH}" 61 | ) 62 | endif() -------------------------------------------------------------------------------- /lib/src/cl-gregorian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include "cl-math.h" 24 | 25 | LIBCALENDAR_API 26 | uint8_t gr_is_leap(int16_t year) { 27 | if((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0)) { 28 | return 1; 29 | } else { 30 | return 0; 31 | } 32 | } 33 | 34 | LIBCALENDAR_API 35 | uint8_t gr_days_in_month(uint8_t month, int16_t year) { 36 | switch(month) { 37 | case 1: 38 | case 3: 39 | case 5: 40 | case 7: 41 | case 8: 42 | case 10: 43 | case 12: 44 | return 31; 45 | break; 46 | case 4: 47 | case 6: 48 | case 9: 49 | case 11: 50 | return 30; 51 | break; 52 | case 2: 53 | return gr_is_leap(year) ? 29 : 28; 54 | break; 55 | default: 56 | return 0; 57 | break; 58 | } 59 | } 60 | 61 | LIBCALENDAR_API 62 | uint16_t gr_days_in_year(int16_t year) { 63 | return gr_is_leap(year) ? 366 : 365; 64 | } 65 | 66 | LIBCALENDAR_API 67 | uint8_t gr_month_in_year(int16_t year) { 68 | return 12; 69 | } 70 | 71 | LIBCALENDAR_API 72 | uint8_t gr_is_valid(int16_t year, uint8_t month, uint16_t day) { 73 | if(month <= gr_month_in_year(year) && day <= gr_days_in_month(month, year)) { 74 | return 1; 75 | } 76 | return 0; 77 | } 78 | 79 | LIBCALENDAR_API 80 | void gr_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) { 81 | const int8_t c0 = fdiv((month - 3) , 12); 82 | const int16_t x1 = month - (12 * c0) - 3; 83 | const int16_t x4 = year + c0; 84 | const div_t d = pdiv(x4, 100); 85 | *jd = fdiv(146097 * d.quot, 4) 86 | + fdiv(36525 * d.rem, 100) 87 | + fdiv(153 * x1 + 2 , 5) 88 | + day + 1721119; 89 | } 90 | 91 | LIBCALENDAR_API 92 | void jdn_to_gr(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) { 93 | const div_t x3 = pdiv(4 * jd - 6884477, 146097); 94 | const div_t x2 = pdiv(100 * (x3.rem / 4) + 99, 36525); 95 | const div_t x1 = pdiv(5 * (x2.rem / 100) + 2, 153); 96 | const uint16_t c0 = (x1.quot + 2) / 12; 97 | *day = (x1.rem / 5) + 1; 98 | *month = x1.quot - 12 * c0 + 3; 99 | *year = 100 * x3.quot + x2.quot + c0; 100 | } 101 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at soroush.rabiei@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /lib/src/cl-islamic-civil.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 - Sorouis Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as publiised by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You isould have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include "cl-math.h" 24 | 25 | LIBCALENDAR_API 26 | uint8_t is_is_leap(int16_t year) { 27 | if(year < 0) { 28 | ++year; 29 | } 30 | if(mod(year * 11 + 14, 30) < 11) { 31 | return 1; 32 | } 33 | return 0; 34 | } 35 | 36 | LIBCALENDAR_API 37 | uint8_t is_days_in_month(uint8_t month, int16_t year) { 38 | if(year == 0) { 39 | return 0; 40 | } 41 | if(month == 12 && is_is_leap(year)) { 42 | return 30; 43 | } 44 | return month % 2 == 0 ? 29 : 30; 45 | } 46 | 47 | LIBCALENDAR_API 48 | uint16_t is_days_in_year(int16_t year) { 49 | return is_is_leap(year) ? 355 : 354; 50 | } 51 | 52 | LIBCALENDAR_API uint8_t 53 | is_month_in_year(int16_t year) { 54 | if(year == 0) { 55 | return 0; 56 | } 57 | return 12; 58 | } 59 | 60 | LIBCALENDAR_API 61 | uint8_t is_is_valid(int16_t year, uint8_t month, uint16_t day) { 62 | if(year != 0 && month <= is_month_in_year(year) && day <= is_days_in_month(month, year)) { 63 | return 1; 64 | } 65 | return 0; 66 | } 67 | 68 | LIBCALENDAR_API 69 | void is_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) { 70 | if(year <= 0) { 71 | ++year; 72 | } 73 | *jd = fdiv(10631 * year - 10617, 30) 74 | + fdiv(325 * month - 320, 11) 75 | + day + 1948439; 76 | } 77 | 78 | LIBCALENDAR_API 79 | void jdn_to_is(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) { 80 | const int32_t k2 = 30 * (jd - 1948440) + 15; 81 | const int32_t k1 = 11 * fdiv(mod(k2, 10631), 30) + 5; 82 | int16_t effective_year = fdiv(k2, 10631) + 1; 83 | if(effective_year <= 0) { 84 | --effective_year; 85 | } 86 | *year = effective_year; 87 | *month = fdiv(k1, 325) + 1; 88 | *day = fdiv(mod(k1, 325), 11) + 1; 89 | } 90 | 91 | LIBCALENDAR_API 92 | void is_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 93 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday) { 94 | uint32_t jdn = 0; 95 | is_to_jdn(&jdn, jyear, jmonth, jday); 96 | jdn_to_gr(jdn, gyear, gmonth, gday); 97 | } 98 | 99 | LIBCALENDAR_API 100 | void gr_to_is(int16_t gyear, uint8_t gmonth, uint16_t gday, 101 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday) { 102 | uint32_t jdn = 0; 103 | gr_to_jdn(&jdn, gyear, gmonth, gday); 104 | jdn_to_is(jdn, jyear, jmonth, jday); 105 | } 106 | -------------------------------------------------------------------------------- /lib/include/libcalendars/cl-calendars.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef LIBCALENDAR_CALENDARS_H 21 | #define LIBCALENDAR_CALENDARS_H 22 | 23 | #include 24 | #include "cl-export.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | #define CAL_GREGORIAN 0x01 /* 0b00000001 */ 31 | #define CAL_JULIAN 0x02 /* 0b00000010 */ 32 | #define CAL_MILANKOVIC 0x04 /* 0b00000100 */ 33 | #define CAL_REVISED_JULIAN CAL_MILANKOVIC 34 | #define CAL_ISLAMIC_CIVIL 0x08 /* 0b00001000 */ 35 | #define CAL_HIJRI CAL_ISLAMIC_CIVIL 36 | #define CAL_JEWSISH 0x10 /* 0b00010000 */ 37 | #define CAL_HEBREW CAL_JEWSISH 38 | #define CAL_SOLAR_HIJRI 0x20 /* 0b00100000 */ 39 | #define CAL_SHAMSI CAL_SOLAR_HIJRI 40 | #define CAL_PERSIAN CAL_SOLAR_HIJRI 41 | 42 | /** 43 | * Decide wheter a given year number in a specified calendar is a leap year 44 | * (have some extra number of month or days) or not. 45 | */ 46 | LIBCALENDAR_API uint8_t is_leap(uint8_t calendar, int16_t year); 47 | 48 | /** 49 | * Returns number of days in a given year for specified calendar type. 50 | */ 51 | LIBCALENDAR_API uint8_t days_in_month(uint8_t calendar, uint8_t month, int16_t year); 52 | 53 | /** 54 | * Returns number of days of a specific month in a given year for specified 55 | * calendar type. 56 | */ 57 | LIBCALENDAR_API uint16_t days_in_year(uint8_t calendar, int16_t year); 58 | 59 | 60 | /** 61 | * Returns number of month in a given year for specified calendar type. 62 | */ 63 | LIBCALENDAR_API uint8_t month_in_year(uint8_t calendar, int16_t year); 64 | 65 | 66 | /** 67 | * Checks if a givend date is valid is specified calendar or not (exists, or 68 | * fits in calendar rules). 69 | */ 70 | LIBCALENDAR_API uint8_t is_valid(uint8_t calendar, 71 | int16_t year, uint8_t month, uint16_t day); 72 | 73 | /** 74 | * Converts a given date (year, month, day) to Julian Day Number. 75 | */ 76 | LIBCALENDAR_API void to_jdn(uint8_t calendar, uint32_t* jd, 77 | int16_t year, uint8_t month, uint16_t day); 78 | 79 | /** 80 | * Converts a Julian Day Number to date (year, month, day). 81 | */ 82 | LIBCALENDAR_API void to_date(uint8_t calendar, uint32_t jd, 83 | int16_t* year, uint8_t* month, uint16_t* day); 84 | 85 | /** 86 | * Converts a date in a calendar to corresponding date in another calendar. 87 | */ 88 | LIBCALENDAR_API void convert_date(uint8_t icalendar, uint8_t ocalendar, 89 | int16_t iyear, uint8_t imonth, uint16_t iday, 90 | int16_t* oyear, uint8_t* omonth, uint16_t* oday); 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif /* LIBCALENDAR_CALENDARS_H */ 97 | -------------------------------------------------------------------------------- /lib/src/cl-babylonian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "cl-math.h" 25 | 26 | LIBCALENDAR_API 27 | uint8_t ba_is_leap(int16_t year) 28 | { 29 | const int16_t cycle = mod(year, 19); 30 | switch (cycle) 31 | { 32 | case 1: 33 | case 4: 34 | case 7: 35 | case 9: 36 | case 12: 37 | case 15: 38 | case 18: 39 | return 1; 40 | default: 41 | return 0; 42 | } 43 | } 44 | 45 | LIBCALENDAR_API 46 | uint8_t ba_days_in_month(uint8_t month, int16_t year) 47 | { 48 | const int16_t cycle = mod(year, 19); 49 | uint8_t m0 = month; 50 | 51 | switch (cycle) 52 | { 53 | case 1: 54 | case 4: 55 | case 7: 56 | case 9: 57 | case 12: 58 | case 15: 59 | if (month == 13) 60 | m0--; 61 | break; 62 | case 18: 63 | if (month > 6) 64 | m0--; 65 | break; 66 | } 67 | 68 | if (mod(m0, 2) == 1) 69 | return 30; 70 | else 71 | return 29; 72 | } 73 | 74 | LIBCALENDAR_API 75 | uint16_t ba_days_in_year(int16_t year) 76 | { 77 | if (ba_is_leap(year) == 1) 78 | return 383; 79 | else 80 | return 354; 81 | } 82 | 83 | LIBCALENDAR_API 84 | uint8_t ba_month_in_year(int16_t year) 85 | { 86 | if (ba_is_leap(year) == 1) 87 | return 13; 88 | return 12; 89 | } 90 | 91 | LIBCALENDAR_API 92 | uint8_t ba_is_valid(int16_t year, uint8_t month, uint16_t day) 93 | { 94 | if (day > 0 && day <= ba_days_in_month(month, year)) 95 | { 96 | return 1; 97 | } 98 | return 0; 99 | } 100 | 101 | LIBCALENDAR_API 102 | void ba_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) 103 | { 104 | *jd = fdiv(6940 * (fdiv(235 * year + 13, 19) + month - 1), 235) + day + 1607174; 105 | } 106 | 107 | LIBCALENDAR_API 108 | void jdn_to_ba(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) 109 | { 110 | const uint32_t s = jd - 1607175; 111 | const div_t m1_omega1 = pdiv(235 * s + 234, 6940); 112 | const div_t j_omega2 = pdiv(19 * m1_omega1.quot + 5, 235); 113 | *day = fdiv(m1_omega1.rem, 235) + 1; 114 | *month = fdiv(j_omega2.rem, 19) + 1; 115 | *year = j_omega2.quot; 116 | } 117 | 118 | LIBCALENDAR_API 119 | void ba_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 120 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday) 121 | { 122 | uint32_t jdn = 0; 123 | ba_to_jdn(&jdn, jyear, jmonth, jday); 124 | jdn_to_gr(jdn, gyear, gmonth, gday); 125 | } 126 | 127 | LIBCALENDAR_API 128 | void gr_to_ba(int16_t gyear, uint8_t gmonth, uint16_t gday, 129 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday) 130 | { 131 | uint32_t jdn = 0; 132 | gr_to_jdn(&jdn, gyear, gmonth, gday); 133 | jdn_to_ba(jdn, jyear, jmonth, jday); 134 | } 135 | -------------------------------------------------------------------------------- /lib/src/cl-julian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "cl-math.h" 25 | 26 | LIBCALENDAR_API 27 | uint8_t ju_is_leap(int16_t year) { 28 | if(year < 0) { 29 | ++year; 30 | } 31 | return mod(year, 4) == 0 ? 1 : 0; 32 | } 33 | 34 | LIBCALENDAR_API 35 | uint8_t ju_days_in_month(uint8_t month, int16_t year) { 36 | switch(month) { 37 | case 1: 38 | case 3: 39 | case 5: 40 | case 7: 41 | case 8: 42 | case 10: 43 | case 12: 44 | return 31; 45 | break; 46 | case 4: 47 | case 6: 48 | case 9: 49 | case 11: 50 | return 30; 51 | break; 52 | case 02: 53 | return ju_is_leap(year) ? 29 : 28; 54 | break; 55 | default: 56 | return 0; 57 | break; 58 | } 59 | } 60 | 61 | LIBCALENDAR_API 62 | uint16_t ju_days_in_year(int16_t year) { 63 | return ju_is_leap(year) ? 366 : 365; 64 | } 65 | 66 | LIBCALENDAR_API 67 | uint8_t ju_month_in_year(int16_t year) { 68 | return 12; 69 | } 70 | 71 | LIBCALENDAR_API 72 | uint8_t ju_is_valid(int16_t year, uint8_t month, uint16_t day) { 73 | if(day > 0 && day <= ju_days_in_month(month, year)) { 74 | return 1; 75 | } 76 | return 0; 77 | } 78 | 79 | LIBCALENDAR_API 80 | void ju_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) { 81 | int32_t c0 = 0; 82 | int32_t j1 = 0; 83 | int32_t j2 = 0; 84 | if(!year) { 85 | *jd = 0; 86 | } 87 | if(year < 0) { 88 | ++year; 89 | } 90 | c0 = fdiv(month - 3, 12); 91 | j1 = fdiv(1461 * (year + c0), 4); 92 | j2 = fdiv(153 * month - 1836 * c0 - 457, 5); 93 | *jd = j1 + j2 + day + 1721117; 94 | } 95 | 96 | LIBCALENDAR_API 97 | void jdn_to_ju(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) { 98 | const int32_t y2 = jd - 1721118; 99 | const int32_t k2 = 4 * y2 + 3; 100 | const int32_t k1 = 5 * fdiv(mod(k2, 1461), 4) + 2; 101 | const int32_t x1 = fdiv(k1, 153); 102 | const int32_t c0 = fdiv(x1 + 2, 12); 103 | *year = (int16_t)(fdiv(k2, 1461) + c0); 104 | if(*year <= 0) { 105 | --(*year); 106 | } 107 | *month = (uint8_t)(x1 - 12 * c0 + 3); 108 | *day = fdiv(mod(k1, 153), 5) + 1; 109 | } 110 | 111 | LIBCALENDAR_API 112 | void ju_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 113 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday) { 114 | uint32_t jdn = 0; 115 | ju_to_jdn(&jdn, jyear, jmonth, jday); 116 | jdn_to_gr(jdn, gyear, gmonth, gday); 117 | } 118 | 119 | LIBCALENDAR_API 120 | void gr_to_ju(int16_t gyear, uint8_t gmonth, uint16_t gday, 121 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday) { 122 | uint32_t jdn = 0; 123 | gr_to_jdn(&jdn, gyear, gmonth, gday); 124 | jdn_to_ju(jdn, jyear, jmonth, jday); 125 | } 126 | -------------------------------------------------------------------------------- /lib/src/cl-milankovic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "cl-math.h" 25 | 26 | LIBCALENDAR_API 27 | uint8_t ml_is_leap(int16_t year) { 28 | if(year <= 0) { 29 | ++year; 30 | } 31 | if(mod(year, 4) == 0) { 32 | if(mod(year, 100) == 0) { 33 | const int16_t century = mod(fdiv(year, 100), 9); 34 | if(century == 2 || century == 6) { 35 | return 1; 36 | } else { 37 | return 0; 38 | } 39 | } 40 | return 1; 41 | } 42 | return 0; 43 | } 44 | 45 | LIBCALENDAR_API 46 | uint8_t ml_days_in_month(uint8_t month, int16_t year) { 47 | switch(month) { 48 | case 1: 49 | case 3: 50 | case 5: 51 | case 7: 52 | case 8: 53 | case 10: 54 | case 12: 55 | return 31; 56 | break; 57 | case 4: 58 | case 6: 59 | case 9: 60 | case 11: 61 | return 30; 62 | break; 63 | case 02: 64 | return ml_is_leap(year) ? 29 : 28; 65 | break; 66 | default: 67 | return 0; 68 | break; 69 | } 70 | } 71 | 72 | LIBCALENDAR_API 73 | uint16_t ml_days_in_year(int16_t year) { 74 | return ml_is_leap(year) ? 366 : 365; 75 | } 76 | 77 | LIBCALENDAR_API uint8_t ml_month_in_year(int16_t year) { 78 | return 12; 79 | } 80 | 81 | LIBCALENDAR_API 82 | uint8_t ml_is_valid(int16_t year, uint8_t month, uint16_t day) { 83 | 84 | if(day > 0 && day <= ml_days_in_month(month, year)) { 85 | return 1; 86 | } 87 | return 0; 88 | } 89 | 90 | LIBCALENDAR_API 91 | void ml_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) { 92 | int8_t c0; 93 | int16_t x1 = 0; 94 | int16_t x2 = 0; 95 | int16_t x3 = 0; 96 | int16_t x4 = 0; 97 | if(year <= 0) { 98 | ++year; 99 | } 100 | c0 = fdiv((month - 3) , 12); 101 | x1 = month - (12 * c0) - 3; 102 | x4 = year + c0; 103 | x3 = fdiv(x4, 100); 104 | x2 = mod(x4, 100); 105 | *jd = fdiv(328718 * x3 + 6, 9) 106 | + fdiv(36525 * x2 , 100) 107 | + fdiv(153 * x1 + 2 , 5) 108 | + day + 1721119; 109 | } 110 | 111 | LIBCALENDAR_API 112 | void jdn_to_ml(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) { 113 | const int32_t k3 = 9 * (jd - 1721120) + 2; 114 | const int32_t x3 = fdiv(k3, 328718); 115 | const int32_t k2 = 100 * fdiv(mod(k3, 328718), 9) + 99; 116 | const int32_t k1 = fdiv(mod(k2, 36525), 100) * 5 + 2; 117 | const int32_t x2 = fdiv(k2, 36525); 118 | const int32_t x1 = 119 | fdiv(5 * fdiv(mod(k2, 36525), 100) + 2, 153); 120 | const uint32_t c0 = fdiv(x1 + 2, 12); 121 | *year = (int16_t)(100 * x3 + x2 + c0); 122 | if(*year <= 0) { 123 | --(*year); 124 | } 125 | *month = (uint8_t)(x1 - 12 * c0 + 3); 126 | *day = fdiv(mod(k1, 153), 5) + 1; 127 | } 128 | 129 | LIBCALENDAR_API 130 | void ml_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 131 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday) { 132 | uint32_t jdn = 0; 133 | ml_to_jdn(&jdn, jyear, jmonth, jday); 134 | jdn_to_gr(jdn, gyear, gmonth, gday); 135 | } 136 | 137 | LIBCALENDAR_API 138 | void gr_to_ml(int16_t gyear, uint8_t gmonth, uint16_t gday, 139 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday) { 140 | uint32_t jdn = 0; 141 | gr_to_jdn(&jdn, gyear, gmonth, gday); 142 | jdn_to_ml(jdn, jyear, jmonth, jday); 143 | } 144 | 145 | -------------------------------------------------------------------------------- /lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021-2025 - Soroush Rabiei, 2 | # This file is part of libcalendars. 3 | # 4 | # libcalendars is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # libcalendars is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with libcalendars. If not, see . 16 | 17 | cmake_minimum_required(VERSION 3.16.0) 18 | 19 | # Intermediate Object Library 20 | add_library(calendars_object OBJECT 21 | # Headers 22 | "include/libcalendars/cl-calendars.h" 23 | "include/libcalendars/cl-export.h" 24 | "include/libcalendars/cl-gregorian.h" 25 | "include/libcalendars/cl-islamic-civil.h" 26 | "include/libcalendars/cl-jewish.h" 27 | "include/libcalendars/cl-julian.h" 28 | "include/libcalendars/cl-milankovic.h" 29 | "include/libcalendars/cl-solar-hijri.h" 30 | "include/libcalendars/cl-egyptian.h" 31 | "include/libcalendars/cl-babylonian.h" 32 | # Sources 33 | "src/cl-math.h" 34 | "src/cl-math.c" 35 | "src/cl-calendars.c" 36 | "src/cl-gregorian.c" 37 | "src/cl-julian.c" 38 | "src/cl-milankovic.c" 39 | "src/cl-solar-hijri.c" 40 | "src/cl-islamic-civil.c" 41 | "src/cl-jewish.c" 42 | "src/cl-egyptian.c" 43 | "src/cl-babylonian.c" 44 | ) 45 | 46 | if(WIN32) 47 | set(PLATFORM_SOURCES win32/libcalendars.rc) 48 | else() 49 | set(PLATFORM_SOURCES "") 50 | endif() 51 | 52 | target_include_directories(calendars_object 53 | PRIVATE 54 | $ 55 | $ 56 | ) 57 | 58 | set_target_properties(calendars_object PROPERTIES 59 | C_STANDARD 90 60 | DEBUG_POSTFIX "_d" 61 | COMPILE_DEFINITIONS libcalendars_EXPORTS 62 | ) 63 | 64 | # Define the shared library 65 | add_library(calendars SHARED "${PLATFORM_SOURCES}" $) 66 | 67 | # Mark all public headers 68 | file(GLOB_RECURSE ALL_HEADER_FILES "include/libcalendars/*.h") 69 | 70 | # Prepate the target 71 | set_target_properties(calendars PROPERTIES 72 | VERSION 1.1.0 73 | SOVERSION 0 74 | C_STANDARD 90 75 | DEBUG_POSTFIX "_d" 76 | DEFINE_SYMBOL libcalendars_EXPORTS 77 | ) 78 | 79 | target_include_directories(calendars 80 | PUBLIC 81 | $ 82 | $ 83 | $ 84 | PRIVATE 85 | ${CMAKE_CURRENT_SOURCE_DIR}/src 86 | ) 87 | 88 | if(UNIX) 89 | target_link_libraries(calendars PUBLIC m) 90 | endif() 91 | 92 | # Define the static libray 93 | add_library(calendars_static STATIC $) 94 | 95 | # Prepate the target 96 | set_target_properties(calendars_static PROPERTIES 97 | VERSION 1.1.0 98 | SOVERSION 0 99 | C_STANDARD 90 100 | DEBUG_POSTFIX "_d" 101 | ) 102 | 103 | target_compile_definitions(calendars_static INTERFACE libcalendars_STATIC) 104 | 105 | target_include_directories(calendars_static 106 | PUBLIC 107 | $ 108 | $ 109 | $ 110 | PRIVATE 111 | ${CMAKE_CURRENT_SOURCE_DIR}/src 112 | ) 113 | 114 | if(UNIX) 115 | target_link_libraries(calendars PUBLIC m) 116 | endif() 117 | 118 | # Generate package config file 119 | 120 | include(CMakePackageConfigHelpers) 121 | 122 | # Installation 123 | 124 | include(GNUInstallDirs) 125 | 126 | export(TARGETS calendars FILE "libcalendarsTargets.cmake") 127 | 128 | install(TARGETS calendars calendars_static 129 | EXPORT "calendarsTargets" 130 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 131 | ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 132 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" 133 | # PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libcalendars" 134 | INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" 135 | ) 136 | 137 | install(DIRECTORY "include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 138 | 139 | include(CMakePackageConfigHelpers) 140 | 141 | write_basic_package_version_file( 142 | "libcalendarsConfigVersion.cmake" 143 | VERSION ${PROJECT_VERSION} 144 | COMPATIBILITY SameMajorVersion 145 | ) 146 | 147 | install(EXPORT "calendarsTargets" 148 | FILE "libcalendarsTargets.cmake" 149 | DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ 150 | ) 151 | 152 | install(FILES 153 | "cmake/libcalendarsConfig.cmake" 154 | "${CMAKE_CURRENT_BINARY_DIR}/libcalendarsConfigVersion.cmake" 155 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/" 156 | ) 157 | -------------------------------------------------------------------------------- /tests/calendar-arithmetic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendar. 4 | * 5 | * libcalendar is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendar is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendar. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "calendar-arithmetic.h" 25 | 26 | void test_julian_day(const test_context* const ctx) 27 | { 28 | assert(ctx); 29 | int16_t year; 30 | uint8_t month; 31 | uint16_t day; 32 | uint32_t out_jd; 33 | for (uint32_t in_jd = ctx->min_jd; in_jd < ctx->max_jd; in_jd++) 34 | { 35 | ctx->from_jdn(in_jd, &year, &month, &day); 36 | ctx->to_jdn(&out_jd, year, month, day); 37 | assert(in_jd == out_jd); 38 | } 39 | } 40 | 41 | void test_gregorian_calendar(const test_context* const ctx) 42 | { 43 | assert(ctx); 44 | 45 | int16_t year; 46 | uint8_t month; 47 | uint16_t day; 48 | int16_t gyi; 49 | uint8_t gmi; 50 | uint16_t gdi; 51 | int16_t gyo; 52 | uint8_t gmo; 53 | uint16_t gdo; 54 | 55 | for (uint32_t in_jd = ctx->min_jd; in_jd < ctx->max_jd; in_jd++) 56 | { 57 | 58 | ctx->jdn_to_gr(in_jd, &gyi, &gmi, &gdi); 59 | ctx->from_gr(gyi, gmi, gdi, &year, &month, &day); 60 | ctx->to_gr(year, month, day, &gyo, &gmo, &gdo); 61 | assert(gyi == gyo); 62 | assert(gmi == gmo); 63 | assert(gdi == gdo); 64 | } 65 | } 66 | 67 | void test_continuity(const test_context* const ctx) 68 | { 69 | assert(ctx); 70 | uint16_t days_in_year = 0; 71 | uint16_t days_in_month = 0; 72 | int16_t year; 73 | uint8_t month; 74 | uint16_t day; 75 | 76 | ctx->from_jdn(ctx->min_jd, &year, &month, &day); 77 | 78 | for (uint32_t in_jd = ctx->min_jd + 1; in_jd < ctx->max_jd; in_jd++) 79 | { 80 | int16_t new_year; 81 | uint8_t new_month; 82 | uint16_t new_day; 83 | ctx->from_jdn(in_jd, &new_year, &new_month, &new_day); 84 | printf("Date: %04d-%02d-%02d - Previous: %04d-%02d-%02d - DiM: %04d - DiY: %04d\n", 85 | new_year, new_month, new_day, 86 | year, month, day, 87 | days_in_month, days_in_year); 88 | if (new_year == year && new_month == month && new_day == day + 1) 89 | { 90 | // Most common case. Continue counting 91 | if (days_in_year) 92 | days_in_year++; 93 | if (days_in_month) 94 | days_in_month++; 95 | } 96 | else if (new_year == year && new_month == month + 1 && new_day == 1) 97 | { 98 | // Same year, start of the new month 99 | if (days_in_month) 100 | { 101 | if (ctx->days_in_month(month, year) != days_in_month) 102 | { 103 | printf("Number of days in month is wrong! Date: %04d-%02d-%02d\n" 104 | "Counted days since start of the month: %04d\n", 105 | year, month, day, days_in_month); 106 | } 107 | // assert(ctx->days_in_month(month, year) == days_in_month); 108 | } 109 | days_in_month = 1; 110 | if (days_in_year) 111 | days_in_year++; 112 | } 113 | else if (new_year == year + 1 && new_month == 1 && new_day == 1) 114 | { 115 | // In case, we were counting before, check for the days in year and mount 116 | if (days_in_year) 117 | { 118 | if (ctx->days_in_year(year) != days_in_year) 119 | { 120 | printf("Number of days in year is wrong! Date: %04d-%02d-%02d\n" 121 | "Counted days since start of the year: %04d\n", 122 | year, month, day, days_in_year); 123 | } 124 | assert(ctx->days_in_year(year) == days_in_year); 125 | } 126 | if (days_in_month) 127 | { 128 | if (ctx->days_in_month(month, year) != days_in_month) 129 | { 130 | printf("Number of days in month is wrong! Date: %04d-%02d-%02d\n" 131 | "Counted days since start of the month: %04d\n", 132 | year, month, day, days_in_month); 133 | } 134 | assert(ctx->days_in_month(month, year) == days_in_month); 135 | } 136 | // In case we were not counting, Start counting days in the year and month 137 | days_in_year = 1; 138 | days_in_month = 1; 139 | } 140 | else 141 | { 142 | // Should not happen 143 | // abort(); 144 | } 145 | year = new_year; 146 | month = new_month; 147 | day = new_day; 148 | } 149 | } -------------------------------------------------------------------------------- /lib/src/cl-solar-hijri.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "cl-math.h" 25 | 26 | /* Constants */ 27 | 28 | /* Number of days in a cycle */ 29 | static const int32_t cycle_days = 1029983; 30 | /* Number of years in a cycle */ 31 | static const uint16_t cycle_years = 2820; 32 | /* 365 + leapRatio */ 33 | static const double year_length = 365.24219858156028368; 34 | // static const double year_length = 365.242374; 35 | // static const double year_length = 365.2421875; 36 | /* 475/01/01 AP, start of 2820 cycle */ 37 | static const uint32_t hijri_shamsi_epoch = 2121446; 38 | /* 683.0 / 2820.0 */ 39 | static const double leap_threshold = 0.24219858156028368; 40 | 41 | LIBCALENDAR_API 42 | uint8_t sh_is_leap(int16_t year) { 43 | double integral; 44 | double frac; 45 | frac = modf((year + 2346) * leap_threshold, &integral); 46 | if(frac < leap_threshold) { 47 | return 1; 48 | } else { 49 | return 0; 50 | } 51 | } 52 | 53 | LIBCALENDAR_API 54 | uint8_t sh_days_in_month(uint8_t month, int16_t year) { 55 | if(month > 0 && month <= 12) { 56 | return month < 7 ? 31 : month < 12 || sh_is_leap(year) ? 30 : 29; 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | int16_t cycle(uint32_t jdn) { 63 | const int32_t offset = jdn - hijri_shamsi_epoch; 64 | int16_t cycle_no = offset / cycle_days; 65 | if(offset < 0) { 66 | --cycle_no; 67 | } 68 | return cycle_no; 69 | } 70 | 71 | LIBCALENDAR_API 72 | uint16_t sh_days_in_year(int16_t year) { 73 | return sh_is_leap(year) ? 366 : 365; 74 | } 75 | 76 | LIBCALENDAR_API uint8_t sh_month_in_year(int16_t year) { 77 | return 12; 78 | } 79 | 80 | LIBCALENDAR_API uint8_t sh_is_valid(int16_t year, uint8_t month, uint16_t day) { 81 | if(year < 0) 82 | ++year; 83 | if(day > 0 && day <= sh_days_in_month(month, year)) { 84 | return 1; 85 | } 86 | return 0; 87 | } 88 | 89 | static uint32_t cycle_start(uint32_t jdn) { 90 | const int16_t era = cycle(jdn); 91 | const uint32_t start = hijri_shamsi_epoch + era * cycle_days; 92 | return start; 93 | } 94 | 95 | static uint32_t fdoy_c(int year, int cycleNo) { 96 | const double d_c = (year * year_length); /* Day in cycle */ 97 | /* First day of year in a cycle */ 98 | const uint32_t fdoy_c = (uint32_t)(floor(d_c)); 99 | return hijri_shamsi_epoch + cycleNo * cycle_days + fdoy_c ; 100 | } 101 | 102 | static uint32_t fdoy(int year) { 103 | /* Cycle number */ 104 | int16_t c = year / cycle_years; 105 | /* First day of year in a cycle */ 106 | uint32_t fdoy_c; 107 | /* Day number in cycle */ 108 | double d_c; 109 | year -= 475; 110 | if(year < 0) { 111 | c--; 112 | } 113 | year -= (c * cycle_years); 114 | d_c = (year * year_length); 115 | fdoy_c = (uint32_t)(floor(d_c)); 116 | return hijri_shamsi_epoch + c * cycle_days + fdoy_c ; 117 | } 118 | 119 | LIBCALENDAR_API 120 | void sh_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) { 121 | /* Adjust the offset of year 0 */ 122 | int16_t era = 0; 123 | int32_t d_y = 0; 124 | int32_t y_c = 0; 125 | int32_t f_d = 0; 126 | size_t i = 0; 127 | if(year < 0) { 128 | ++year; 129 | } 130 | era = (year - 475) / cycle_years; 131 | if((year - 475) < 0) { 132 | --era; 133 | } 134 | y_c = (year - 475) - era * cycle_years; 135 | f_d = fdoy_c(y_c, era); 136 | d_y = 0; 137 | for(i = 1; i < month; ++i) { 138 | d_y += sh_days_in_month(i, year); 139 | } 140 | d_y += day; 141 | *jd = f_d + d_y - 1; 142 | } 143 | 144 | LIBCALENDAR_API 145 | void jdn_to_sh(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) { 146 | const int c = cycle(jd); 147 | const int16_t y_c = (int16_t)(floor((jd - cycle_start(jd)) / year_length)); 148 | int16_t y = y_c + 475 + c * 2820; 149 | uint16_t d = jd - fdoy_c(y_c, c) + 1; 150 | uint8_t m = 0; 151 | if(d > sh_days_in_year(y)) { 152 | y++; 153 | d = 1; 154 | } 155 | if(y <= 0) { 156 | y--; 157 | } 158 | for(m = 1; m < 12; ++m) { 159 | if(d > sh_days_in_month(m, y)) { 160 | d -= sh_days_in_month(m, y); 161 | } else { 162 | break; 163 | } 164 | } 165 | *year = y; 166 | *month = m; 167 | *day = d; 168 | } 169 | 170 | LIBCALENDAR_API 171 | void sh_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 172 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday) { 173 | uint32_t jdn = 0; 174 | sh_to_jdn(&jdn, jyear, jmonth, jday); 175 | jdn_to_gr(jdn, gyear, gmonth, gday); 176 | } 177 | 178 | LIBCALENDAR_API 179 | void gr_to_sh(int16_t gyear, uint8_t gmonth, uint16_t gday, 180 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday) { 181 | uint32_t jdn = 0; 182 | gr_to_jdn(&jdn, gyear, gmonth, gday); 183 | jdn_to_sh(jdn, jyear, jmonth, jday); 184 | } 185 | -------------------------------------------------------------------------------- /lib/src/cl-calendars.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | LIBCALENDAR_API 29 | uint8_t is_leap(uint8_t calendar, int16_t year) { 30 | switch(calendar) { 31 | case CAL_GREGORIAN: 32 | return gr_is_leap(year); 33 | case CAL_JULIAN: 34 | return ju_is_leap(year); 35 | case CAL_MILANKOVIC: 36 | return ml_is_leap(year); 37 | case CAL_ISLAMIC_CIVIL: 38 | return is_is_leap(year); 39 | case CAL_JEWSISH: 40 | return jw_is_leap(year); 41 | case CAL_SOLAR_HIJRI: 42 | return sh_is_leap(year); 43 | default: 44 | return 0; 45 | } 46 | } 47 | 48 | LIBCALENDAR_API uint8_t 49 | days_in_month(uint8_t calendar, uint8_t month, int16_t year) { 50 | switch(calendar) { 51 | case CAL_GREGORIAN: 52 | return gr_days_in_month(month, year); 53 | case CAL_JULIAN: 54 | return ju_days_in_month(month, year); 55 | case CAL_MILANKOVIC: 56 | return ml_days_in_month(month, year); 57 | case CAL_ISLAMIC_CIVIL: 58 | return is_days_in_month(month, year); 59 | case CAL_JEWSISH: 60 | return jw_days_in_month(month, year); 61 | case CAL_SOLAR_HIJRI: 62 | return sh_days_in_month(month, year); 63 | default: 64 | return 0; 65 | } 66 | } 67 | 68 | LIBCALENDAR_API 69 | uint16_t days_in_year(uint8_t calendar, int16_t year) { 70 | switch(calendar) { 71 | case CAL_GREGORIAN: 72 | return gr_days_in_year(year); 73 | case CAL_JULIAN: 74 | return ju_days_in_year(year); 75 | case CAL_MILANKOVIC: 76 | return ml_days_in_year(year); 77 | case CAL_ISLAMIC_CIVIL: 78 | return is_days_in_year(year); 79 | case CAL_JEWSISH: 80 | return jw_days_in_year(year); 81 | case CAL_SOLAR_HIJRI: 82 | return sh_days_in_year(year); 83 | default: 84 | return 0; 85 | } 86 | } 87 | 88 | LIBCALENDAR_API uint8_t month_in_year(uint8_t calendar, int16_t year) { 89 | switch(calendar) { 90 | case CAL_GREGORIAN: 91 | return gr_month_in_year(year); 92 | case CAL_JULIAN: 93 | return ju_month_in_year(year); 94 | case CAL_MILANKOVIC: 95 | return ml_month_in_year(year); 96 | case CAL_ISLAMIC_CIVIL: 97 | return is_month_in_year(year); 98 | case CAL_JEWSISH: 99 | return jw_month_in_year(year); 100 | case CAL_SOLAR_HIJRI: 101 | return sh_month_in_year(year); 102 | default: 103 | return 0; 104 | } 105 | } 106 | 107 | LIBCALENDAR_API uint8_t is_valid(uint8_t calendar, 108 | int16_t year, uint8_t month, uint16_t day) { 109 | switch(calendar) { 110 | case CAL_GREGORIAN: 111 | return gr_is_valid(year, month, day); 112 | case CAL_JULIAN: 113 | return ju_is_valid(year, month, day); 114 | case CAL_MILANKOVIC: 115 | return ml_is_valid(year, month, day); 116 | case CAL_ISLAMIC_CIVIL: 117 | return is_is_valid(year, month, day); 118 | case CAL_JEWSISH: 119 | return jw_is_valid(year, month, day); 120 | case CAL_SOLAR_HIJRI: 121 | return sh_is_valid(year, month, day); 122 | default: 123 | return 0; 124 | } 125 | } 126 | 127 | LIBCALENDAR_API void to_jdn(uint8_t calendar, uint32_t* jd, 128 | int16_t year, uint8_t month, uint16_t day) { 129 | switch(calendar) { 130 | case CAL_GREGORIAN: 131 | gr_to_jdn(jd, year, month, day); 132 | break; 133 | case CAL_JULIAN: 134 | ju_to_jdn(jd, year, month, day); 135 | break; 136 | case CAL_MILANKOVIC: 137 | ml_to_jdn(jd, year, month, day); 138 | break; 139 | case CAL_ISLAMIC_CIVIL: 140 | is_to_jdn(jd, year, month, day); 141 | break; 142 | case CAL_JEWSISH: 143 | jw_to_jdn(jd, year, month, day); 144 | break; 145 | case CAL_SOLAR_HIJRI: 146 | sh_to_jdn(jd, year, month, day); 147 | break; 148 | default: 149 | *jd = 0; 150 | break; 151 | } 152 | } 153 | 154 | LIBCALENDAR_API void to_date(uint8_t calendar, uint32_t jd, 155 | int16_t* year, uint8_t* month, uint16_t* day) { 156 | switch(calendar) { 157 | case CAL_GREGORIAN: 158 | jdn_to_gr(jd, year, month, day); 159 | break; 160 | case CAL_JULIAN: 161 | jdn_to_ju(jd, year, month, day); 162 | break; 163 | case CAL_MILANKOVIC: 164 | jdn_to_ml(jd, year, month, day); 165 | break; 166 | case CAL_ISLAMIC_CIVIL: 167 | jdn_to_is(jd, year, month, day); 168 | break; 169 | case CAL_JEWSISH: 170 | jdn_to_jw(jd, year, month, day); 171 | break; 172 | case CAL_SOLAR_HIJRI: 173 | jdn_to_sh(jd, year, month, day); 174 | break; 175 | default: 176 | *year = 0; 177 | *month = 0; 178 | *day = 0; 179 | break; 180 | } 181 | } 182 | 183 | LIBCALENDAR_API 184 | void convert_date(uint8_t icalendar, uint8_t ocalendar, 185 | int16_t iyear, uint8_t imonth, uint16_t iday, 186 | int16_t* oyear, uint8_t* omonth, uint16_t* oday) { 187 | uint32_t jdn = 0; 188 | if(!is_valid(icalendar, iyear, imonth, iday)) { 189 | *oyear = 0; 190 | *omonth = 0; 191 | *oday = 0; 192 | return; 193 | } 194 | if(icalendar == ocalendar) { 195 | *oyear = iyear; 196 | *omonth = imonth; 197 | *oday = iday; 198 | return; 199 | } 200 | /* Handle exceptions: Pair of calendars that can be converted using more 201 | * optimized algorithms. For all other pairs, use jdn as an intermediate 202 | * variable to convert dates. 203 | */ 204 | to_jdn(icalendar, &jdn, iyear, imonth, iday); 205 | to_date(ocalendar, jdn, oyear, omonth, oday); 206 | } 207 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/c,c++,linux,windows,autotools,visualstudio 3 | 4 | ### Autotools ### 5 | # http://www.gnu.org/software/automake 6 | 7 | /ar-lib 8 | /mdate-sh 9 | /py-compile 10 | /test-driver 11 | /ylwrap 12 | 13 | # http://www.gnu.org/software/autoconf 14 | 15 | /autom4te.cache 16 | /autoscan.log 17 | /autoscan-*.log 18 | /aclocal.m4 19 | /compile 20 | /config.guess 21 | /config.h.in 22 | /config.sub 23 | /configure 24 | /configure.scan 25 | /depcomp 26 | /install-sh 27 | /missing 28 | /stamp-h1 29 | 30 | # https://www.gnu.org/software/libtool/ 31 | # Edit: Why would I want to ignore libtool?! 32 | #/ltmain.sh 33 | 34 | # http://www.gnu.org/software/texinfo 35 | 36 | /texinfo.tex 37 | 38 | ### C ### 39 | # Prerequisites 40 | *.d 41 | 42 | # Object files 43 | *.o 44 | *.ko 45 | *.obj 46 | *.elf 47 | 48 | # Linker output 49 | *.ilk 50 | *.map 51 | *.exp 52 | 53 | # Precompiled Headers 54 | *.gch 55 | *.pch 56 | 57 | # Libraries 58 | *.lib 59 | *.a 60 | *.la 61 | *.lo 62 | 63 | # Shared objects (inc. Windows DLLs) 64 | *.dll 65 | *.so 66 | *.so.* 67 | *.dylib 68 | 69 | # Executables 70 | *.exe 71 | *.out 72 | *.app 73 | *.i*86 74 | *.x86_64 75 | *.hex 76 | 77 | # Debug files 78 | *.dSYM/ 79 | *.su 80 | *.idb 81 | *.pdb 82 | 83 | # Kernel Module Compile Results 84 | *.mod* 85 | *.cmd 86 | .tmp_versions/ 87 | modules.order 88 | Module.symvers 89 | Mkfile.old 90 | dkms.conf 91 | 92 | ### C++ ### 93 | # Prerequisites 94 | 95 | # Compiled Object files 96 | *.slo 97 | 98 | # Precompiled Headers 99 | 100 | # Compiled Dynamic libraries 101 | 102 | # Fortran module files 103 | *.mod 104 | *.smod 105 | 106 | # Compiled Static libraries 107 | *.lai 108 | 109 | # Executables 110 | 111 | ### Linux ### 112 | *~ 113 | 114 | # temporary files which can be created if a process still has a handle open of a deleted file 115 | .fuse_hidden* 116 | 117 | # KDE directory preferences 118 | .directory 119 | 120 | # Linux trash folder which might appear on any partition or disk 121 | .Trash-* 122 | 123 | # .nfs files are created when an open file is removed but is still being accessed 124 | .nfs* 125 | 126 | ### Windows ### 127 | # Windows thumbnail cache files 128 | Thumbs.db 129 | ehthumbs.db 130 | ehthumbs_vista.db 131 | 132 | # Folder config file 133 | Desktop.ini 134 | 135 | # Recycle Bin used on file shares 136 | $RECYCLE.BIN/ 137 | 138 | # Windows Installer files 139 | *.cab 140 | *.msi 141 | *.msm 142 | *.msp 143 | 144 | # Windows shortcuts 145 | *.lnk 146 | 147 | ### VisualStudio ### 148 | ## Ignore Visual Studio temporary files, build results, and 149 | ## files generated by popular Visual Studio add-ons. 150 | ## 151 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 152 | 153 | # User-specific files 154 | *.suo 155 | *.user 156 | *.userosscache 157 | *.sln.docstates 158 | 159 | # User-specific files (MonoDevelop/Xamarin Studio) 160 | *.userprefs 161 | 162 | # Build results 163 | Static*/ 164 | [Dd]ebug/ 165 | [Dd]ebugPublic/ 166 | [Rr]elease/ 167 | [Rr]eleases/ 168 | x64/ 169 | x86/ 170 | bld/ 171 | [Bb]in/ 172 | [Oo]bj/ 173 | [Ll]og/ 174 | 175 | # Visual Studio 2015 cache/options directory 176 | .vs/ 177 | # Uncomment if you have tasks that create the project's static files in wwwroot 178 | #wwwroot/ 179 | 180 | # MSTest test Results 181 | [Tt]est[Rr]esult*/ 182 | [Bb]uild[Ll]og.* 183 | 184 | # NUNIT 185 | *.VisualState.xml 186 | TestResult.xml 187 | 188 | # Build Results of an ATL Project 189 | [Dd]ebugPS/ 190 | [Rr]eleasePS/ 191 | dlldata.c 192 | 193 | # .NET Core 194 | project.lock.json 195 | project.fragment.lock.json 196 | artifacts/ 197 | **/Properties/launchSettings.json 198 | 199 | *_i.c 200 | *_p.c 201 | *_i.h 202 | *.meta 203 | *.pgc 204 | *.pgd 205 | *.rsp 206 | *.sbr 207 | *.tlb 208 | *.tli 209 | *.tlh 210 | *.tmp 211 | *.tmp_proj 212 | *.log 213 | *.vspscc 214 | *.vssscc 215 | .builds 216 | *.pidb 217 | *.svclog 218 | *.scc 219 | 220 | # Chutzpah Test files 221 | _Chutzpah* 222 | 223 | # Visual C++ cache files 224 | ipch/ 225 | *.aps 226 | *.ncb 227 | *.opendb 228 | *.opensdf 229 | *.sdf 230 | *.cachefile 231 | *.VC.db 232 | *.VC.VC.opendb 233 | 234 | # Visual Studio profiler 235 | *.psess 236 | *.vsp 237 | *.vspx 238 | *.sap 239 | 240 | # TFS 2012 Local Workspace 241 | $tf/ 242 | 243 | # Guidance Automation Toolkit 244 | *.gpState 245 | 246 | # ReSharper is a .NET coding add-in 247 | _ReSharper*/ 248 | *.[Rr]e[Ss]harper 249 | *.DotSettings.user 250 | 251 | # JustCode is a .NET coding add-in 252 | .JustCode 253 | 254 | # TeamCity is a build add-in 255 | _TeamCity* 256 | 257 | # DotCover is a Code Coverage Tool 258 | *.dotCover 259 | 260 | # Visual Studio code coverage results 261 | *.coverage 262 | *.coveragexml 263 | 264 | # NCrunch 265 | _NCrunch_* 266 | .*crunch*.local.xml 267 | nCrunchTemp_* 268 | 269 | # MightyMoose 270 | *.mm.* 271 | AutoTest.Net/ 272 | 273 | # Web workbench (sass) 274 | .sass-cache/ 275 | 276 | # Installshield output folder 277 | [Ee]xpress/ 278 | 279 | # DocProject is a documentation generator add-in 280 | DocProject/buildhelp/ 281 | DocProject/Help/*.HxT 282 | DocProject/Help/*.HxC 283 | DocProject/Help/*.hhc 284 | DocProject/Help/*.hhk 285 | DocProject/Help/*.hhp 286 | DocProject/Help/Html2 287 | DocProject/Help/html 288 | 289 | # Click-Once directory 290 | publish/ 291 | 292 | # Publish Web Output 293 | *.[Pp]ublish.xml 294 | *.azurePubxml 295 | # TODO: Uncomment the next line to ignore your web deploy settings. 296 | # By default, sensitive information, such as encrypted password 297 | # should be stored in the .pubxml.user file. 298 | #*.pubxml 299 | *.pubxml.user 300 | *.publishproj 301 | 302 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 303 | # checkin your Azure Web App publish settings, but sensitive information contained 304 | # in these scripts will be unencrypted 305 | PublishScripts/ 306 | 307 | # NuGet Packages 308 | *.nupkg 309 | # The packages folder can be ignored because of Package Restore 310 | **/packages/* 311 | # except build/, which is used as an MSBuild target. 312 | !**/packages/build/ 313 | # Uncomment if necessary however generally it will be regenerated when needed 314 | #!**/packages/repositories.config 315 | # NuGet v3's project.json files produces more ignorable files 316 | *.nuget.props 317 | *.nuget.targets 318 | 319 | # Microsoft Azure Build Output 320 | csx/ 321 | *.build.csdef 322 | 323 | # Microsoft Azure Emulator 324 | ecf/ 325 | rcf/ 326 | 327 | # Windows Store app package directories and files 328 | AppPackages/ 329 | BundleArtifacts/ 330 | Package.StoreAssociation.xml 331 | _pkginfo.txt 332 | 333 | # Visual Studio cache files 334 | # files ending in .cache can be ignored 335 | *.[Cc]ache 336 | # but keep track of directories ending in .cache 337 | !*.[Cc]ache/ 338 | 339 | # Others 340 | ClientBin/ 341 | ~$* 342 | *.dbmdl 343 | *.dbproj.schemaview 344 | *.jfm 345 | *.pfx 346 | *.publishsettings 347 | orleans.codegen.cs 348 | 349 | # Since there are multiple workflows, uncomment next line to ignore bower_components 350 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 351 | #bower_components/ 352 | 353 | # RIA/Silverlight projects 354 | Generated_Code/ 355 | 356 | # Backup & report files from converting an old project file 357 | # to a newer Visual Studio version. Backup files are not needed, 358 | # because we have git ;-) 359 | _UpgradeReport_Files/ 360 | Backup*/ 361 | UpgradeLog*.XML 362 | UpgradeLog*.htm 363 | 364 | # SQL Server files 365 | *.mdf 366 | *.ldf 367 | *.ndf 368 | 369 | # Business Intelligence projects 370 | *.rdl.data 371 | *.bim.layout 372 | *.bim_*.settings 373 | 374 | # Microsoft Fakes 375 | FakesAssemblies/ 376 | 377 | # GhostDoc plugin setting file 378 | *.GhostDoc.xml 379 | 380 | # Node.js Tools for Visual Studio 381 | .ntvs_analysis.dat 382 | node_modules/ 383 | 384 | # Typescript v1 declaration files 385 | typings/ 386 | 387 | # Visual Studio 6 build log 388 | *.plg 389 | 390 | # Visual Studio 6 workspace options file 391 | *.opt 392 | 393 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 394 | *.vbw 395 | 396 | # Visual Studio LightSwitch build output 397 | **/*.HTMLClient/GeneratedArtifacts 398 | **/*.DesktopClient/GeneratedArtifacts 399 | **/*.DesktopClient/ModelManifest.xml 400 | **/*.Server/GeneratedArtifacts 401 | **/*.Server/ModelManifest.xml 402 | _Pvt_Extensions 403 | 404 | # Paket dependency manager 405 | .paket/paket.exe 406 | paket-files/ 407 | 408 | # FAKE - F# Make 409 | .fake/ 410 | 411 | # JetBrains Rider 412 | .idea/ 413 | *.sln.iml 414 | 415 | # CodeRush 416 | .cr/ 417 | 418 | # Python Tools for Visual Studio (PTVS) 419 | __pycache__/ 420 | *.pyc 421 | 422 | # Cake - Uncomment if you are using it 423 | # tools/** 424 | # !tools/packages.config 425 | 426 | # Telerik's JustMock configuration file 427 | *.jmconfig 428 | 429 | # BizTalk build output 430 | *.btp.cs 431 | *.btm.cs 432 | *.odx.cs 433 | *.xsd.cs 434 | 435 | ### VisualStudio Patch ### 436 | # By default, sensitive information, such as encrypted password 437 | # should be stored in the .pubxml.user file. 438 | 439 | # End of https://www.gitignore.io/api/c,c++,linux,windows,autotools,visualstudio 440 | 441 | build/* 442 | out/* 443 | checks.json 444 | CMakePresets.json 445 | .vscode/settings.json 446 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | A calendar implementation library 2 | ********************************* 3 | 4 | `libcalendar' is an experimental C library to provide arithmentic for 5 | several calendars. 6 | 7 | Why? 8 | ==== 9 | 10 | Why implement yet another calendaring library? Well, there are plenty of 11 | excellent implementations out there, though it seems there is no free, 12 | GPL-compliant, C implementation. Besides there is no precise implementation of 13 | non-gregorian calendars, most importantly Solar Hijri and Islamic Civil 14 | calendars. 15 | 16 | API Design Philosophy 17 | ===================== 18 | 19 | This library is API-less by design. This means the library is not intended 20 | to be used directly as a standalone API. Instead, it only provides arithmetic 21 | implementations meant to be integrated into existing date-time APIs or utilized 22 | by developers when creating their own APIs for handling date-time 23 | operations. 24 | 25 | Key Principles: 26 | 27 | * No Custom Data Structures: The library 28 | deliberately avoids introducing structures for representing calendar components 29 | (e.g., `year`, `month`, `day`). Instead, it operates exclusively on Plain Data 30 | Types (PDTs), such as integers or other native types, ensuring simplicity and 31 | compatibility with diverse applications. 32 | 33 | * Flexibility: By focusing solely on 34 | the arithmetic logic, the library provides a robust foundation for building or 35 | extending higher-level APIs. This design empowers developers to adapt the 36 | library seamlessly to their specific domain requirements without being 37 | constrained by predefined abstractions. 38 | 39 | * Integration-Ready: The library 40 | complements existing date-time APIs by adding precise calendar arithmetic 41 | capabilities without altering their design philosophy. It can also be used to 42 | prototype and implement custom solutions efficiently. 43 | 44 | How to use libcalendar? 45 | ======================= 46 | 47 | Almost all algorithms in libcalendar are implemented using Julian Day 48 | calculations. You can convert any date on supported calendars to JDN and vice 49 | versa. For example: 50 | 51 | uint32_t jdn = 0; 52 | sh_to_jdn(&jdn, 1392, 04, 15); 53 | printf("Julian Day for 1392/04/15 AP is: %l\n", jdn); 54 | 55 | Which prints: 56 | 57 | $ Julian Day for 1392/04/15 AP is: 2456480 58 | 59 | You can also use non-jdn broken-down date API. For example you can check if a 60 | year in Solar Hijri calendar is leap or not: 61 | 62 | 63 | if(sh_is_leap(1395)) /* returns 0 for regular years and 1 for leap years */ 64 | printf("Yep\n"); 65 | 66 | Or convert calendar dates to/from Gregorian calendar: 67 | 68 | int16_t y; 69 | uint8_t m; 70 | uint16_t d; 71 | sh_to_gr(1369, 06, 20, &y, &m, &d); 72 | printf("1369/06/20 AP is %04d-%02d-%02d\n", y, m, d); 73 | gr_to_sh(2017, 09, 11, &y, &m, &d); 74 | printf("2017-09-11 is %04d-%02d-%02d AP\n", y, m, d); 75 | 76 | which will print: 77 | 78 | $ 1369/06/20 AP is 2017-09-11 79 | $ 2017-09-11 is 1369/06/18 AP 80 | 81 | Algorithms 82 | ========== 83 | 84 | This library is imolemented in C programming language, using no external 85 | dependecies. The C standard library used in libcalendar is C11. Though it 86 | should be possible to compile this library with a C99 compiler. 87 | 88 | Most of the conversion algorithms for JDN to calendar and vice versa are 89 | implemented based on Dr. Louis Strous's work. (available online on "Astronomy 90 | Page" at `http://aa.quae.nl/en/reken/juliaansedag.html') Namely Gregorian, 91 | Julian, Milankovic and Islamic Civil calendars and their JDN calculations are 92 | adopted from above page. 93 | 94 | Solar Hijri (Shamsi) and Jalali calendar calculations are implemented based 95 | on Dr. Mousa Akrami's work on median year length for Persian calendar. (See 96 | notes on Solar Hijri calendar). 97 | 98 | Calendars 99 | ========= 100 | 101 | Following is a list of supported calendars, and a short description (mostly 102 | from wikipedia) about them. 103 | 104 | Gregorian 105 | --------- 106 | 107 | The Gregorian calendar is internationally the most widely used civil 108 | calendar. 109 | It is named after Pope Gregory XIII, who introduced it in October 1582. 110 | 111 | The calendar was a refinement to the Julian calendar involving a 0.002% 112 | correction in the length of the year. The motivation for the reform was to stop 113 | the drift of the calendar with respect to the equinoxes and solstices - 114 | particularly the northern vernal equinox, which helps set the date for Easter. 115 | Transition to the Gregorian calendar would restore the holiday to the time of 116 | the year in which it was celebrated when introduced by the early Church. The 117 | reform was adopted initially by the Catholic countries of Europe. Protestants 118 | and Eastern Orthodox countries continued to use the traditional Julian calendar 119 | and adopted the Gregorian reform after a time, at least for civil purposes and 120 | for the sake of convenience in international trade. The last European country 121 | to adopt the reform was Greece, in 1923. Many (but not all) countries that have 122 | traditionally used the Islamic and other religious calendars have come to adopt 123 | this calendar for civil purposes. 124 | 125 | Julian 126 | ------ 127 | 128 | The Julian calendar, proposed by Julius Caesar in 46 BC (708 AUC), was a 129 | reform 130 | of the Roman calendar. It took effect on 1 January 45 BC (AUC 709), by edict. 131 | It was the predominant calendar in the Roman world, most of Europe, and in 132 | European settlements in the Americas and elsewhere, until it was refined and 133 | gradually replaced by the Gregorian calendar, promulgated in 1582 by Pope 134 | Gregory XIII. The Julian calendar gains against the mean tropical year at the 135 | rate of one day in 128 years. For the Gregorian the figure is one day in 3,030 136 | years. The difference in the average length of the year between Julian (365.25 137 | days) and Gregorian (365.2425 days) is 0.002%. 138 | 139 | Milankovic 140 | ---------- 141 | 142 | The Revised Julian calendar, also known as the Milankovic calendar, or, less 143 | formally, new calendar, is a calendar, developed and proposed by the Serbian 144 | scientist Milutin Milankovic in 1923, which effectively discontinued the 340 145 | years of divergence between the naming of dates sanctioned by those Eastern 146 | Orthodox churches adopting it and the Gregorian calendar that has come to 147 | predominate worldwide. This calendar was intended to replace the ecclesiastical 148 | calendar based on the Julian calendar hitherto in use by all of the Eastern 149 | Orthodox Church. The Revised Julian calendar temporarily aligned its dates with 150 | the Gregorian calendar proclaimed in 1582 by Pope Gregory XIII for adoption by 151 | the Christian world. The calendar has been adopted by the Orthodox churches of 152 | Constantinople, Albania, Alexandria, Antioch, Bulgaria, Cyprus, Greece, Poland, 153 | and Romania. 154 | 155 | Solar Hijri 156 | ----------- 157 | 158 | The Solar Hijri calendar, also called the Solar Hejri calendar or Shamsi 159 | Hijri calendar, and abbreviated as SH, is the official calendar of Iran and 160 | Afghanistan. It begins on the vernal equinox (Nowruz) as determined by 161 | astronomical calculation for the Iran Standard Time meridian (52.5°E or 162 | GMT+3.5h). This determination of starting moment is more accurate than the 163 | Gregorian calendar for predicting the date of the vernal equinox, because it 164 | uses astronomical observations rather than mathematical rules. 165 | 166 | Each of the twelve months corresponds with a zodiac sign. The first six 167 | months have 31 days, the next five have 30 days, and the last month has 29 days 168 | in usual years but 30 days in leap years. The New Year's Day always falls on 169 | the March equinox. 170 | 171 | Notes on Solar Hijri 172 | -------------------- 173 | 174 | My implementation of Solar Hijri (Shamsi) calendar is based on median year 175 | calculation obtained from Muousa Akrami's work: `The development of Iranian 176 | calendar: historical and astronomical foundations - 2014` (available online at 177 | `https://arxiv.org/pdf/1111.4926.pdf') This method is more accurate than 178 | 33-year algorithm and supports a wider range of dates, both in Solar Hijri <-> 179 | Gregorian comversions, and in JDN calculations. 180 | 181 | Egyptian 182 | -------- 183 | 184 | The Egyptian calendar is one of the earliest known timekeeping systems, 185 | developed in ancient Egypt to align with the Nile's annual flood cycles. It 186 | played a vital role in organizing agricultural activities and religious 187 | festivals. This calendar is notable for its remarkable simplicity and its 188 | influence on later timekeeping systems, including the Julian and Gregorian 189 | calendars. 190 | 191 | The Egyptian calendar was based on a solar year divided into three 192 | seasons of four months each, reflecting the natural cycles of the Nile: 193 | 194 | * Akhet (Inundation): The flood season, when the Nile overflowed, 195 | replenishing the 196 | soil. 197 | * Peret (Emergence): The growing season, when crops were planted and 198 | cultivated. 199 | * Shemu (Harvest): The dry season, when crops were harvested. 200 | 201 | Each of the twelve months contained 30 days, making up a total of 360 days in 202 | the year. To reconcile this structure with the solar year of approximately 203 | 365.25 days, the Egyptians added five additional days, known as the "epagomenal 204 | days," at the end of the year. These days were considered outside the normal 205 | calendar and were dedicated to the birthdays of key deities, including Osiris, 206 | Isis, and Horus. 207 | 208 | The calendar was not leap-adjusted, meaning it gradually drifted out 209 | of sync with the solar year over centuries. However, its consistency made it 210 | highly practical for everyday use and administrative tasks. This robust 211 | simplicity, combined with its cultural significance, helped the Egyptian 212 | calendar endure for millennia and leave a lasting legacy on the history of 213 | timekeeping. 214 | 215 | Babylonian 216 | ---------- 217 | The Babylonian calendar, developed in ancient Mesopotamia, is one of the 218 | earliest recorded lunar calendars. It played a crucial role in the 219 | administrative, agricultural, and religious life of the Babylonians. Rooted in 220 | astronomical observations, this calendar reflects the sophisticated 221 | understanding of celestial movements by Babylonian scholars. 222 | 223 | The calendar was a lunisolar system, aligning months with the lunar cycle and 224 | years with the solar cycle. It relied on the Metonic Cycle, which states that 225 | 235 synodical months are equal to 19 tropical years. These 19 years alternated 226 | between 12 and 13 months, with long years (13 months) occurring in the 1st, 227 | 4th, 7th, 9th, 12th, 15th, and 18th years of the cycle. This structure included 228 | 125 months of 30 days and 110 months of 29 days, adding up to 6,940 days in 229 | total. In most long years, the 12th month was doubled, but in the 18th year, 230 | the 6th month was doubled instead. Day 1 of month 1 (Nisannu) of year 1 in the 231 | Era of Seleukos corresponded to 3 April 310 BCE in the Julian Calendar (CJDN 232 | 1607558). 233 | 234 | The Babylonians determined the beginning of each month by observing the phases 235 | of the moon. Since the calendar was partly based on direct observations, the 236 | length of months and years was not entirely fixed. Factors such as weather 237 | conditions could delay the official start of a month if the moon was obscured 238 | by clouds. This variability meant the distribution of months into years 239 | operated independently from the distribution of days into months. 240 | 241 | To reconstruct the Babylonian calendar predictably, we can use a mathematically 242 | derived version that closely approximates the historical system. Such a version 243 | would differ from the original calendar by at most one day, capturing its 244 | structure while avoiding the unpredictability of direct lunar observations. 245 | -------------------------------------------------------------------------------- /lib/src/cl-jewish.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021-2025 - Soroush Rabiei, 3 | * This file is part of libcalendars. 4 | * 5 | * libcalendars is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * libcalendars is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with libcalendars. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include "cl-math.h" 24 | 25 | uint8_t jw_year_type(int16_t year) { 26 | div_t qr = pdiv(7 * year - 6, 19); 27 | double k_p = 0.178117457 * year + 0.777965458 * qr.rem + 0.2533747; 28 | double i; 29 | double k = modf(k_p, &i); 30 | if(qr.rem < 5) { 31 | if(k >= 0.752248) { 32 | return 7; 33 | } 34 | if(k >= 0.714282) { 35 | return 6; 36 | } 37 | if(k >= 0.661835) { 38 | return 5; 39 | } 40 | if(k >= 0.376121) { 41 | return 4; 42 | } 43 | if(k >= 0.271103) { 44 | return 3; 45 | } 46 | if(k >= 0.090410) { 47 | return 2; 48 | } 49 | if(k >= 0.000000) { 50 | return 1; 51 | } 52 | } else if(qr.rem >= 5 && qr.rem < 8) { 53 | if(k >= 0.804693) { 54 | return 7; 55 | } 56 | if(k >= 0.714282) { 57 | return 6; 58 | } 59 | if(k >= 0.661835) { 60 | return 5; 61 | } 62 | if(k >= 0.376121) { 63 | return 4; 64 | } 65 | if(k >= 0.271103) { 66 | return 3; 67 | } 68 | if(k >= 0.090410) { 69 | return 2; 70 | } 71 | if(k >= 0.000000) { 72 | return 1; 73 | } 74 | } else if(qr.rem >= 8 && qr.rem < 12) { 75 | /* FIXME: This is the same as previous case... seems to be an error[?] */ 76 | if(k >= 0.804693) { 77 | return 7; 78 | } 79 | if(k >= 0.714282) { 80 | return 6; 81 | } 82 | if(k >= 0.661835) { 83 | return 5; 84 | } 85 | if(k >= 0.376121) { 86 | return 4; 87 | } 88 | if(k >= 0.271103) { 89 | return 3; 90 | } 91 | if(k >= 0.090410) { 92 | return 2; 93 | } 94 | if(k >= 0.000000) { 95 | return 1; 96 | } 97 | } else if(qr.rem > 12) { 98 | if(k >= 0.871750) { 99 | return 14; 100 | } 101 | if(k >= 0.714282) { 102 | return 13; 103 | } 104 | if(k >= 0.533590) { 105 | return 12; 106 | } 107 | if(k >= 0.428570) { 108 | return 11; 109 | } 110 | if(k >= 0.285711) { 111 | return 10; 112 | } 113 | if(k >= 0.157466) { 114 | return 9; 115 | } 116 | if(k >= 0.000000) { 117 | return 8; 118 | } 119 | } 120 | return 0; 121 | } 122 | 123 | LIBCALENDAR_API 124 | uint8_t jw_is_leap(int16_t year) { 125 | return mod((7 * year + 1), 19) < 7 ? 1 : 0; 126 | } 127 | 128 | LIBCALENDAR_API 129 | uint8_t jw_is_complete(int16_t year) { 130 | switch(jw_year_type(year)) { 131 | case 2: 132 | case 5: 133 | case 7: 134 | case 9: 135 | case 12: 136 | case 14: 137 | return 1; 138 | default: 139 | return 0; 140 | } 141 | } 142 | 143 | LIBCALENDAR_API 144 | uint8_t jw_is_deficient(int16_t year) { 145 | switch(jw_year_type(year)) { 146 | case 1: 147 | case 6: 148 | case 8: 149 | case 11: 150 | case 13: 151 | return 1; 152 | default: 153 | return 0; 154 | } 155 | } 156 | 157 | LIBCALENDAR_API 158 | uint8_t jw_is_regular(int16_t year) { 159 | switch(jw_year_type(year)) { 160 | case 3: 161 | case 5: 162 | case 10: 163 | return 1; 164 | default: 165 | return 0; 166 | } 167 | } 168 | 169 | 170 | LIBCALENDAR_API 171 | uint8_t jw_days_in_month(uint8_t month, int16_t year) { 172 | switch(month) { 173 | case 1: 174 | return 30; 175 | case 2: 176 | return 29; 177 | case 3: 178 | return 30; 179 | case 4: 180 | return 29; 181 | case 5: 182 | return 30; 183 | case 6: 184 | return 29; 185 | case 7: 186 | return 30; 187 | case 8: 188 | if(jw_is_regular(year) || jw_is_deficient(year)) { 189 | return 29; 190 | } else if(jw_is_complete(year)) { 191 | return 30; 192 | } 193 | case 9: 194 | if(jw_is_regular(year) || jw_is_complete(year)) { 195 | return 30; 196 | } else if(jw_is_deficient(year)) { 197 | return 29; 198 | } 199 | break; 200 | case 10: 201 | return 29; 202 | case 11: 203 | return 30; 204 | case 12: 205 | if(jw_is_leap(year)) { 206 | /* Adar Rishon */ 207 | return 30; 208 | } else { 209 | /* Adar */ 210 | return 29; 211 | } 212 | break; 213 | case 13: 214 | if(jw_is_leap(year)) { 215 | /* Adar Sheni */ 216 | return 29; 217 | } else { 218 | /* Invalid */ 219 | return 0; 220 | } 221 | break; 222 | default: 223 | break; 224 | } 225 | return 0; 226 | } 227 | 228 | LIBCALENDAR_API 229 | uint16_t jw_days_in_year(int16_t year) { 230 | if(year == 0) { 231 | return 0; 232 | } 233 | if(jw_is_leap(year)) { 234 | if(jw_is_regular(year)) { 235 | return 354; 236 | } else if(jw_is_complete(year)) { 237 | return 355; 238 | } else if(jw_is_deficient(year)) { 239 | return 353; 240 | } 241 | } else { 242 | if(jw_is_regular(year)) { 243 | return 384; 244 | } else if(jw_is_complete(year)) { 245 | return 385; 246 | } else if(jw_is_deficient(year)) { 247 | return 383; 248 | } 249 | } 250 | return 0; 251 | } 252 | 253 | LIBCALENDAR_API 254 | uint8_t jw_month_in_year(int16_t year) { 255 | if(year == 0) { 256 | return 0; 257 | } 258 | if(jw_is_leap(year)) { 259 | return 13; 260 | } 261 | return 12; 262 | } 263 | 264 | LIBCALENDAR_API 265 | uint8_t jw_is_valid(int16_t year, uint8_t month, uint16_t day) { 266 | if(day > 0 && day <= jw_days_in_month(month, year)) { 267 | return 1; 268 | } 269 | return 0; 270 | } 271 | 272 | static inline int32_t q(int32_t x) { 273 | return fdiv(fdiv(235 * x + 1, 19), 1095); 274 | } 275 | 276 | static inline int32_t r(int32_t x) { 277 | return mod(fdiv(235 * x + 1, 19), 1095); 278 | } 279 | 280 | static inline int32_t v1(int32_t x) { 281 | const int32_t c1 = fdiv(235 * x + 1, 19); 282 | const int32_t qx = fdiv(c1, 1095); 283 | return 32336 * qx 284 | + fdiv(15 * qx + 765433 * mod(c1, 1095) + 12084, 25920); 285 | } 286 | 287 | static inline int32_t v2(int32_t x) { 288 | const int32_t c1 = fdiv(235 * x + 1, 19); 289 | const int32_t qx = fdiv(c1, 1095); 290 | const int32_t v1x = 32336 * qx 291 | + fdiv(15 * qx + 765433 * mod(c1, 1095) + 12084, 25920); 292 | return v1x + mod(fdiv(6 * mod(v1x, 7), 7), 2); 293 | } 294 | 295 | static inline int32_t L2(int32_t x) { 296 | return v2(x + 1) - v2(x); 297 | } 298 | 299 | static inline int32_t v3(int32_t x) { 300 | return 2 * mod(fdiv(L2(x) + 19, 15), 2); 301 | } 302 | 303 | static inline int32_t v4(int32_t x) { 304 | return mod(fdiv(L2(x - 1) + 7, 15), 2); 305 | } 306 | 307 | static inline int32_t c2(int32_t x) { 308 | return v2(x) + v3(x) + v4(x); 309 | } 310 | 311 | static inline int32_t L(int32_t x) { 312 | return c2(x + 1) - c2(x); 313 | } 314 | 315 | static inline int32_t c3(int32_t x) { 316 | return fdiv(384 * x + 7, 13) 317 | + mod(fdiv(L(x) + 7, 2), 15) * fdiv(x + 4, 12) 318 | - mod(fdiv(385 - L(x), 2), 15) * fdiv(x + 3, 12); 319 | } 320 | 321 | static inline int32_t c4(int32_t x, int32_t y) { 322 | 323 | const int32_t c1 = fdiv(235 * x + 1, 19); 324 | const int32_t qx = fdiv(c1, 1095); 325 | const int32_t Ly = L(y); 326 | const int32_t rx = mod(c1, 1095); 327 | const int32_t v1x = 32336 * qx 328 | + fdiv(15 * qx + 765433 * rx + 12084, 25920); 329 | return 330 | v1x 331 | + mod(fdiv(6 * mod(v1x, 7), 7), 2) 332 | + 2 * mod(fdiv(L2(x) + 19, 15), 2) 333 | + mod(fdiv(L2(x - 1) + 7, 15), 2) 334 | + fdiv(384 * y + 7, 13) 335 | + mod(fdiv(Ly + 7, 2), 15) * fdiv(y + 4, 12) 336 | - mod(fdiv(385 - Ly, 2), 15) * fdiv(y + 3, 12); 337 | } 338 | 339 | LIBCALENDAR_API 340 | void jw_to_jdn(uint32_t* jd, int16_t year, uint8_t month, uint16_t day) { 341 | const int32_t j0 = 347998; 342 | const int32_t c0 = fdiv(13 - month, 7); 343 | const int32_t x1 = year - 1 + c0; 344 | const int32_t x3 = month - 1; 345 | const int32_t z4 = day - 1; 346 | const int32_t qx = q(x1); 347 | *jd = j0 - 177 348 | + 32336 * qx 349 | + fdiv(15 * qx + 765433 * r(x1) + 12084, 25920) 350 | + mod(fdiv(6 * mod(v1(x1), 7), 7), 2) 351 | + 2 * mod(fdiv(L2(x1) + 19, 15), 2) 352 | + mod(fdiv(L2(x1 - 1) + 7, 15), 2) 353 | + fdiv(384 * x3 + 7, 13) 354 | + mod(fdiv(L(x3) + 7, 2), 15) * fdiv(x3 + 4, 12) 355 | - mod(fdiv(385 - L(x3), 2), 15) 356 | * fdiv(x3 + 3, 12) + z4; 357 | } 358 | 359 | LIBCALENDAR_API 360 | void jdn_to_jw(uint32_t jd, int16_t* year, uint8_t* month, uint16_t* day) { 361 | const int32_t y4 = jd - 347821; 362 | const int32_t q = fdiv(y4, 1447); 363 | const int32_t r = mod(y4, 1447); 364 | const int32_t y1_p = 49 * q 365 | + fdiv(23 * q + 25920 * r + 13835, 765433); 366 | const int32_t gamma_1 = y1_p + 1; 367 | const int32_t xi_1 = fdiv(19 * gamma_1 + 17, 235); 368 | const int32_t mu_1 = gamma_1 - fdiv(235 * xi_1 + 1, 19); 369 | const int32_t zeta_1 = y4 - c4(xi_1, mu_1); 370 | const int32_t gamma_2 = gamma_1 + fdiv(zeta_1, 33); 371 | const int32_t xi_2 = fdiv(19 * gamma_2 + 17, 235); 372 | const int32_t mu_2 = gamma_2 - fdiv(235 * xi_2 + 1, 19); 373 | const int32_t zeta_2 = y4 - c4(xi_2, mu_2); 374 | const int32_t gamma_3 = gamma_2 + fdiv(zeta_2, 33); 375 | const int32_t xi_3 = fdiv(19 * gamma_3 + 17, 235); 376 | const int32_t mu_3 = gamma_3 - fdiv(235 * xi_3 + 1, 19); 377 | const int32_t zeta_3 = y4 - c4(xi_3, mu_3); 378 | const int32_t c = fdiv(12 - mu_3, 7); 379 | *year = xi_3 + 1 - c; 380 | *month = mu_3 + 1; 381 | *day = zeta_3 + 1; 382 | } 383 | 384 | LIBCALENDAR_API 385 | void jw_to_gr(int16_t jyear, uint8_t jmonth, uint16_t jday, 386 | int16_t* gyear, uint8_t* gmonth, uint16_t* gday) { 387 | uint32_t jdn = 0; 388 | jw_to_jdn(&jdn, jyear, jmonth, jday); 389 | jdn_to_gr(jdn, gyear, gmonth, gday); 390 | } 391 | 392 | LIBCALENDAR_API 393 | void gr_to_jw(int16_t gyear, uint8_t gmonth, uint16_t gday, 394 | int16_t* jyear, uint8_t* jmonth, uint16_t* jday) { 395 | uint32_t jdn = 0; 396 | gr_to_jdn(&jdn, gyear, gmonth, gday); 397 | jdn_to_jw(jdn, jyear, jmonth, jday); 398 | } 399 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A calendar implementation library 2 | 3 | ![language](https://img.shields.io/badge/language-c-blue.svg) 4 | ![c](https://img.shields.io/badge/std-c99-blue.svg) 5 | ![GCC](https://img.shields.io/badge/GCC-13.0-blue.svg) 6 | ![MSVC](https://img.shields.io/badge/MSVC-14-blue.svg) 7 | ![license](https://img.shields.io/badge/license-GPLv3-blue.svg) 8 | [![Build Status](https://travis-ci.org/soroush/libcalendars.svg?branch=dev)](https://travis-ci.org/soroush/libcalendars) 9 | [![Build status](https://ci.appveyor.com/api/projects/status/kroimbg1ous41iak?svg=true)](https://ci.appveyor.com/project/soroush/libcalendars) 10 | 11 | A precise C library to provide arithmentic for the most common calendar 12 | systems. Currently Gregorian, Julian, Milankovic, Solar Hijri (also known as 13 | Shamsi or Jalali), Islamic Civilm Jewish (also know as Hebrew), Egyptian and 14 | Babylonian calendar systems are provided. 15 | 16 | ## Contents 17 | - [Installation](#installation) 18 | - [API Design Philosophy](#api-design-philosophy) 19 | - [Usage](#usage) 20 | - [Documentation](#documentation) 21 | - [Contribution](#contribution) 22 | - [Algorithms](#algorithms) 23 | - [Calendars](#calendars) 24 | - [Gregorian](#gregorian) 25 | - [Julian](#julian) 26 | - [Milanković](#milanković) 27 | - [Solar Hijri (Jalali, or Shamsi)](#solar-hijri) 28 | - [Islamic Civil](#islamic-civil) 29 | - [Egyptian](#egyptian) 30 | - [Babylonian](#babylonian) 31 | - [License](#license) 32 | - [Commercial Use Exception](#commercial-use-exception) 33 | 34 | ## Installation 35 | 36 | The easiest way to install `libcalendars` is to use PPA: 37 | 38 | ```bash 39 | sudo add-apt-repository ppa:soroush-r/solap 40 | sudo apt update 41 | sudo apt install libcalendars1 # installs the library 42 | sudo apt install libcalendars-dev # Installs development headers 43 | ``` 44 | 45 | Alternatively you can compile the package from source: 46 | ```bash 47 | mkdir build && cd build 48 | cmake .. 49 | cmake --build . --config Release 50 | sudo cmake --install . --config Release 51 | ``` 52 | 53 | In case you also want to build and run unit tests: 54 | ```bash 55 | cmake -DBUILD_TESTING=ON .. 56 | cmake --build . --config Release 57 | ctest . --config Release 58 | ``` 59 | 60 | If nothing goes wrong, you should see all test passed: 61 | ``` 62 | Start 1: Gregorian 63 | 1/6 Test #1: Gregorian ........................ Passed 0.31 sec 64 | Start 2: Julian 65 | 2/6 Test #2: Julian ........................... Passed 0.59 sec 66 | Start 3: Milankovic 67 | 3/6 Test #3: Milankovic ....................... Passed 0.78 sec 68 | Start 4: SolarHijri 69 | 4/6 Test #4: SolarHijri ....................... Passed 0.89 sec 70 | Start 5: Jewish 71 | 5/6 Test #5: Jewish ........................... Passed 17.46 sec 72 | Start 6: IslamicCivil 73 | 6/6 Test #6: IslamicCivil ..................... Passed 0.53 sec 74 | 75 | 100% tests passed, 0 tests failed out of 6 76 | ``` 77 | 78 | ## API Design Philosophy 79 | 80 | This library is API-less by design. This means the library is not intended to be 81 | used directly as a standalone API. Instead, it only provides arithmetic 82 | implementations meant to be integrated into existing date-time APIs or utilized 83 | by developers when creating their own APIs for handling date-time 84 | operations. 85 | 86 | Key Principles: 87 | 88 | * No Custom Data Structures: The library 89 | deliberately avoids introducing structures for representing calendar components 90 | (e.g., `year`, `month`, `day`). Instead, it operates exclusively on Plain Data 91 | Types (PDTs), such as integers or other native types, ensuring simplicity and 92 | compatibility with diverse applications. 93 | 94 | * Flexibility: By focusing solely on 95 | the arithmetic logic, the library provides a robust foundation for building or 96 | extending higher-level APIs. This design empowers developers to adapt the 97 | library seamlessly to their specific domain requirements without being 98 | constrained by predefined abstractions. 99 | 100 | * Integration-Ready: The library 101 | complements existing date-time APIs by adding precise calendar arithmetic 102 | capabilities without altering their design philosophy. It can also be used to 103 | prototype and implement custom solutions efficiently. 104 | 105 | ## Usage 106 | 107 | Almost all algorithms in libcalendar are implemented using Julian Day 108 | calculations. You can convert any date on supported calendars to JDN and vice 109 | versa. For example: 110 | 111 | ```c 112 | uint32_t jdn = 0; 113 | sh_to_jdn(&jdn, 1392, 04, 15); 114 | printf("Julian Day for 1392/04/15 AP is: %l\n", jdn); 115 | ``` 116 | Which prints: 117 | 118 | ```$ Julian Day for 1392/04/15 AP is: 2456480``` 119 | 120 | You can also use non-jdn broken-down date API. For example you 121 | can check if a year in Solar Hijri calendar is leap or not: 122 | 123 | ```c 124 | if(sh_is_leap(1395)) /* returns 0 for regular years and 1 for leap years */ 125 | printf("Yep\n"); 126 | ``` 127 | You can also convert calendar dates to/from Gregorian calendar: 128 | ```c 129 | int16_t y; 130 | uint8_t m; 131 | uint16_t d; 132 | sh_to_gr(1396, 06, 20, &y, &m, &d); 133 | printf("1396/06/20 AP is %04d-%02d-%02d\n", y, m, d); 134 | gr_to_sh(2017, 09, 11, &y, &m, &d); 135 | printf("2017-09-11 is %04d-%02d-%02d AP\n", y, m, d); 136 | ``` 137 | which will print: 138 | 139 | ``` 140 | 1396/06/20 AP is 2017-09-11 141 | 2017-09-11 is 1396/06/18 AP 142 | ``` 143 | 144 | ## Documentation 145 | 146 | Reference of API is available at 147 | [https://soroush.github.io/libcalendars](https://soroush.github.io/libcalendars/). 148 | 149 | ## Contribution 150 | 151 | This library is written in the hope that it will be useful. With your help, 152 | `libcalendars` can be better (: You can help `libcalendars` in several ways: 153 | 154 | 1. Build the library and thest its output. You can [raise an 155 | issue](https://github.com/soroush/libcalendars/issues) if you've found any 156 | problem. 157 | 158 | 2. Contribute to improve code quality, fix bugs and add new features. Please 159 | read our [Code of Conduct](CODE_OF_CONDUCT.md) for more details. 160 | 161 | ## Algorithms 162 | 163 | This library is implemented in C programming language, using no external 164 | dependecies. The C standard library used in libcalendar is C11. Though it should 165 | be possible to compile this library with a C99 compiler. 166 | 167 | Most of the conversion algorithms for JDN to calendar and vice versa are 168 | implemented based 169 | [Dr Louis Strous](http://orcid.org/0000-0003-2110-7248)'s work (available online 170 | on [Astronomy](http://aa.quae.nl/en/reken/juliaansedag.html) page). Namely 171 | Gregorian, Julian, Milankovic and Islamic Civil calendars and their JDN 172 | calculations are adopted from above page. Solar Hijri (Shamsi) and Jalali 173 | calendar calculations are implemented based on 174 | [Dr. Mousa Akrami](http://m-akrami.teacher.srbiau.ac.ir/)'s work on median year 175 | length for Persian calendar. (See notes on Solar Hijri calendar). 176 | 177 | ## Calendars 178 | 179 | Following is a list of supported calendars, and a short description (mostly from 180 | wikipedia) about them. 181 | 182 | ### Gregorian 183 | 184 | The Gregorian calendar is internationally the most widely used civil calendar. 185 | It is named after Pope Gregory XIII, who introduced it in October 1582. 186 | 187 | The calendar was a refinement to the Julian calendar[3] involving a 0.002% 188 | correction in the length of the year. The motivation for the reform was to stop 189 | the drift of the calendar with respect to the equinoxes and 190 | solstices—particularly the northern vernal equinox, which helps set the date 191 | for Easter. Transition to the Gregorian calendar would restore the holiday to 192 | the time of the year in which it was celebrated when introduced by the early 193 | Church. The reform was adopted initially by the Catholic countries of Europe. 194 | Protestants and Eastern Orthodox countries continued to use the traditional 195 | Julian calendar and adopted the Gregorian reform after a time, at least for 196 | civil purposes and for the sake of convenience in international trade. The last 197 | European country to adopt the reform was Greece, in 1923. Many (but not all) 198 | countries that have traditionally used the Islamic and other religious calendars 199 | have come to adopt this calendar for civil purposes. 200 | 201 | ### Julian 202 | 203 | The Julian calendar, proposed by Julius Caesar in 46 BC (708 AUC), was a reform 204 | of the Roman calendar. It took effect on 1 January 45 BC (AUC 709), by edict. 205 | It was the predominant calendar in the Roman world, most of Europe, and in 206 | European settlements in the Americas and elsewhere, until it was refined and 207 | gradually replaced by the Gregorian calendar, promulgated in 1582 by Pope 208 | Gregory XIII. The Julian calendar gains against the mean tropical year at the 209 | rate of one day in 128 years. For the Gregorian the figure is one day in 3,030 210 | years. The difference in the average length of the year between Julian (365.25 211 | days) and Gregorian (365.2425 days) is 0.002%. 212 | 213 | ### Milanković 214 | 215 | The Revised Julian calendar, also known as the Milanković calendar, or, less 216 | formally, new calendar, is a calendar, developed and proposed by the Serbian 217 | scientist Milutin Milanković in 1923, which effectively discontinued the 340 218 | years of divergence between the naming of dates sanctioned by those Eastern 219 | Orthodox churches adopting it and the Gregorian calendar that has come to 220 | predominate worldwide. This calendar was intended to replace the ecclesiastical 221 | calendar based on the Julian calendar hitherto in use by all of the Eastern 222 | Orthodox Church. The Revised Julian calendar temporarily aligned its dates with 223 | the Gregorian calendar proclaimed in 1582 by Pope Gregory XIII for adoption by 224 | the Christian world. The calendar has been adopted by the Orthodox churches of 225 | Constantinople, Albania, Alexandria, Antioch, Bulgaria, Cyprus, Greece, Poland, 226 | and Romania. 227 | 228 | ### Solar Hijri 229 | 230 | The Solar Hijri calendar, also called the Solar Hejri calendar or Shamsi Hijri 231 | calendar, and abbreviated as SH, is the official calendar of Iran and 232 | Afghanistan. It begins on the vernal equinox (Nowruz) as determined by 233 | astronomical calculation for the Iran Standard Time meridian 234 | (52.5°E or GMT+3.5h). This determination of starting moment is more accurate 235 | than the Gregorian calendar for predicting the date of the vernal equinox, 236 | because it uses astronomical observations rather than mathematical rules. 237 | 238 | Each of the twelve months corresponds with a zodiac sign. 239 | The first six months have 31 days, the next five have 30 days, and the last 240 | month has 29 days in usual years but 30 days in leap years. The New Year's Day 241 | always falls on the March equinox. 242 | 243 | #### A note on Solar Hijri 244 | 245 | My implementation of Solar Hijri (Shamsi) calendar is based on median year 246 | calculation obtained from Muousa Akrami's work: 247 | [The development of Iranian calendar: historical and astronomical foundations - 248 | 2014](https://arxiv.org/pdf/1111.4926.pdf). 249 | This method is more accurate than 33-year algorithm and supports a wider range 250 | of dates, both in Solar Hijri <-> Gregorian comversions, and in JDN 251 | calculations. 252 | 253 | ### Islamic Civil 254 | 255 | The Islamic, Muslim, or Hijri calendar is a lunar calendar consisting of 12 256 | months in a year of 354 or 355 days. It is used (often alongside the Gregorian 257 | calendar) to date events in many Muslim countries. It is also used by Muslims to 258 | determine the proper days of Islamic holidays and rituals, such as the annual 259 | period of fasting and the proper time for the pilgrimage to Mecca. 260 | 261 | The Islamic calendar employs the Hijri era whose epoch was retrospectively 262 | established as the Islamic New Year of AD 622. During that year, Muhammad and 263 | his followers migrated from Mecca to Yathrib (now Medina) and established the 264 | first Muslim community (ummah), an event commemorated as the Hijra. In the West, 265 | dates in this era are usually denoted AH (Latin: Anno Hegirae, "in the year of 266 | the Hijra") in parallel with the Christian (AD) and Jewish eras (AM). In Muslim 267 | countries, it is also sometimes denoted as H from its Arabic form. In English, 268 | years prior to the Hijra are reckoned as BH ("Before the Hijra"). 269 | 270 | ### Egyptian 271 | 272 | The Egyptian calendar is one of the earliest known timekeeping systems, 273 | developed in ancient Egypt to align with the Nile's annual flood cycles. It 274 | played a vital role in organizing agricultural activities and religious 275 | festivals. This calendar is notable for its remarkable simplicity and its 276 | influence on later timekeeping systems, including the Julian and Gregorian 277 | calendars. 278 | 279 | The Egyptian calendar was based on a solar year divided into three 280 | seasons of four months each, reflecting the natural cycles of the Nile: 281 | 282 | * Akhet (Inundation): The flood season, when the Nile overflowed, 283 | replenishing the 284 | soil. 285 | * Peret (Emergence): The growing season, when crops were planted and 286 | cultivated. 287 | * Shemu (Harvest): The dry season, when crops were harvested. 288 | 289 | Each of the twelve months contained 30 days, making up a total of 360 days in 290 | the year. To reconcile this structure with the solar year of approximately 291 | 365.25 days, the Egyptians added five additional days, known as the "epagomenal 292 | days," at the end of the year. These days were considered outside the normal 293 | calendar and were dedicated to the birthdays of key deities, including Osiris, 294 | Isis, and Horus. 295 | 296 | The calendar was not leap-adjusted, meaning it gradually drifted out 297 | of sync with the solar year over centuries. However, its consistency made it 298 | highly practical for everyday use and administrative tasks. This robust 299 | simplicity, combined with its cultural significance, helped the Egyptian 300 | calendar endure for millennia and leave a lasting legacy on the history of 301 | timekeeping. 302 | 303 | ### Babylonian 304 | 305 | The Babylonian calendar, developed in ancient Mesopotamia, is one of the 306 | earliest recorded lunar calendars. It played a crucial role in the 307 | administrative, agricultural, and religious life of the Babylonians. Rooted in 308 | astronomical observations, this calendar reflects the sophisticated 309 | understanding of celestial movements by Babylonian scholars. 310 | 311 | The calendar was a lunisolar system, aligning months with the lunar cycle and 312 | years with the solar cycle. It relied on the Metonic Cycle, which states that 313 | 235 synodical months are equal to 19 tropical years. These 19 years alternated 314 | between 12 and 13 months, with long years (13 months) occurring in the 1st, 315 | 4th, 7th, 9th, 12th, 15th, and 18th years of the cycle. This structure included 316 | 125 months of 30 days and 110 months of 29 days, adding up to 6,940 days in 317 | total. In most long years, the 12th month was doubled, but in the 18th year, 318 | the 6th month was doubled instead. Day 1 of month 1 (Nisannu) of year 1 in the 319 | Era of Seleukos corresponded to 3 April 310 BCE in the Julian Calendar (CJDN 320 | 1607558). 321 | 322 | The Babylonians determined the beginning of each month by observing the phases 323 | of the moon. Since the calendar was partly based on direct observations, the 324 | length of months and years was not entirely fixed. Factors such as weather 325 | conditions could delay the official start of a month if the moon was obscured 326 | by clouds. This variability meant the distribution of months into years 327 | operated independently from the distribution of days into months. 328 | 329 | To reconstruct the Babylonian calendar predictably, we can use a mathematically 330 | derived version that closely approximates the historical system. Such a version 331 | would differ from the original calendar by at most one day, capturing its 332 | structure while avoiding the unpredictability of direct lunar observations. 333 | 334 | ## License 335 | 336 | This project is licensed under the **GNU General Public License v3.0 (GPL-3.0)**. 337 | This means that you are free to use, modify, and distribute the software under the terms of the GPL-3.0 license. 338 | 339 | ### Commercial Use Exception 340 | While this project is licensed under GPL-3.0, **commercial use is permitted only with prior written permission**. 341 | However, this permission is **granted free of charge**. The requirement is in place to ensure that commercial usage aligns with the project's goals and values. 342 | 343 | If you wish to use this project in a commercial product or service, please contact us to obtain the necessary permissions. 344 | 345 | For details about the GPL-3.0 license, see the [official license text](https://www.gnu.org/licenses/gpl-3.0.html). 346 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------