File indexing completed on 2025-01-19 04:46:46

0001 /*
0002   This file is part of KOrganizer.
0003 
0004   SPDX-FileCopyrightText: 2003 Jonathan Singer <jsinger@leeta.net>
0005   SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com>
0006   Calendar routines from Hebrew Calendar by Frank Yellin.
0007   SPDX-FileCopyrightText: 1994-2006 Danny Sadinoff <danny@sadinoff.com>.
0008 
0009   SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 #pragma once
0012 
0013 // needed for [[nodiscard]]
0014 
0015 struct DateResult {
0016     int year;
0017     int month;
0018     int day;
0019     int day_of_week;
0020 
0021     int hebrew_month_length, secular_month_length;
0022     bool hebrew_leap_year_p, secular_leap_year_p;
0023     int kvia;
0024     int hebrew_day_number;
0025 };
0026 
0027 /**
0028   This class converts dates between the Hebrew and Gregorian (secular)
0029   calendars.
0030 
0031   @author Loïc Corbasson
0032  */
0033 class HebrewDate
0034 {
0035 public:
0036     explicit HebrewDate(const DateResult &);
0037     ~HebrewDate();
0038 
0039     static HebrewDate fromSecular(int year, int month, int day);
0040     static HebrewDate fromHebrew(int year, int month, int day);
0041 
0042     [[nodiscard]] int year() const;
0043     [[nodiscard]] int month() const;
0044     [[nodiscard]] int day() const;
0045     [[nodiscard]] int dayOfWeek() const;
0046 
0047     [[nodiscard]] int hebrewMonthLength() const;
0048     [[nodiscard]] int secularMonthLength() const;
0049     [[nodiscard]] bool isOnHebrewLeapYear() const;
0050     [[nodiscard]] bool isOnSecularLeapYear() const;
0051     [[nodiscard]] int kvia() const;
0052     [[nodiscard]] int hebrewDayNumber() const;
0053 
0054 private:
0055     int mYear, mMonth, mDay, mDayOfWeek;
0056     int mHebrewMonthLength, mSecularMonthLength;
0057     bool mOnHebrewLeapYear, mOnSecularLeapYear;
0058     int mKvia, mHebrewDayNumber;
0059 };
0060 
0061 /**
0062   This class is used internally to convert dates between the Hebrew and
0063   Gregorian (secular) calendars.
0064 
0065   Calendar routines from Hebrew Calendar by Frank Yellin.
0066 
0067   For more information, see “The Comprehensive Hebrew Calendar” by Arthur Spier
0068   and “Calendrical Calculations” by E. M. Reingold and Nachum Dershowitz,
0069   or the documentation of Remind by Roaring Penguin Software Inc.
0070 
0071   @author Jonathan Singer
0072 */
0073 class Converter
0074 {
0075     friend class HebrewDate;
0076 
0077 public:
0078     enum HebrewMonths {
0079         Nissan = 1,
0080         Iyar,
0081         Sivan,
0082         Tamuz,
0083         Ab,
0084         Elul,
0085         Tishrei,
0086         Cheshvan,
0087         Kislev,
0088         Tevet,
0089         Shvat,
0090         Adar,
0091         AdarII,
0092         AdarI = 12,
0093     };
0094 
0095     enum SecularMonths {
0096         January = 1,
0097         February,
0098         March,
0099         April,
0100         May,
0101         June,
0102         July,
0103         August,
0104         September,
0105         October,
0106         November,
0107         December,
0108     };
0109 
0110 private:
0111     static bool hebrew_leap_year_p(int year);
0112     static bool gregorian_leap_year_p(int year);
0113 
0114     static long absolute_from_gregorian(int year, int month, int day);
0115     static long absolute_from_hebrew(int year, int month, int day);
0116 
0117     static void gregorian_from_absolute(long date, int *yearp, int *monthp, int *dayp);
0118     static void hebrew_from_absolute(long date, int *yearp, int *monthp, int *dayp);
0119 
0120     static int hebrew_months_in_year(int year);
0121     static int hebrew_month_length(int year, int month);
0122     static int secular_month_length(int year, int month);
0123 
0124     static long hebrew_elapsed_days(int year);
0125     static long hebrew_elapsed_days2(int year);
0126     static int hebrew_year_length(int year);
0127 
0128     static void finish_up(long absolute, int hyear, int hmonth, int syear, int smonth, struct DateResult *result);
0129 
0130     static void secularToHebrewConversion(int year, int month, int day, struct DateResult *result);
0131     static void hebrewToSecularConversion(int year, int month, int day, struct DateResult *result);
0132 };