1 /*
2 * Copyright (C) 2020 Georg Zotti
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program 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 this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
17 */
18
19 #include "StelTranslator.hpp"
20 #include "HebrewCalendar.hpp"
21 #include "JulianCalendar.hpp"
22 #include "StelUtils.hpp"
23 #include "StelApp.hpp"
24 #include "StelCore.hpp"
25
26 const int HebrewCalendar::hebrewEpoch=JulianCalendar::fixedFromJulian({-3761, JulianCalendar::october, 7}); // RD -1373427
27
HebrewCalendar(double jd)28 HebrewCalendar::HebrewCalendar(double jd): Calendar(jd)
29 {
30 HebrewCalendar::retranslate();
31 }
32
33 QMap<int, QString> HebrewCalendar::weekDayNames;
34 QMap<int, QString> HebrewCalendar::monthNames;
35
retranslate()36 void HebrewCalendar::retranslate()
37 {
38 // fill the name lists with translated month and day names
39 weekDayNames={
40 {0, qc_("yom rishon (first day)" , "Hebrew week day name")},
41 {1, qc_("yom sheni (second day)" , "Hebrew week day name")},
42 {2, qc_("yom shelishi (third day)" , "Hebrew week day name")},
43 {3, qc_("yom revi'i (fourth day)" , "Hebrew week day name")},
44 {4, qc_("yom ḥamishi (fifth day)" , "Hebrew week day name")},
45 {5, qc_("yom shishi (sixth day)" , "Hebrew week day name")},
46 {6, qc_("yom shabbat (sabbath day)", "Hebrew week day name")}};
47 monthNames={
48 { 1, qc_("Nisan" , "Hebrew month name")},
49 { 2, qc_("Iyyar" , "Hebrew month name")},
50 { 3, qc_("Sivan" , "Hebrew month name")},
51 { 4, qc_("Tammuz" , "Hebrew month name")},
52 { 5, qc_("Av" , "Hebrew month name")},
53 { 6, qc_("Elul" , "Hebrew month name")},
54 { 7, qc_("Tishri" , "Hebrew month name")},
55 { 8, qc_("Marḥeshvan", "Hebrew month name")},
56 { 9, qc_("Kislev" , "Hebrew month name")},
57 {10, qc_("Tevet" , "Hebrew month name")},
58 {11, qc_("Shevat" , "Hebrew month name")},
59 {12, qc_("Adar" , "Hebrew month name")}};
60 }
61
62 // Set a calendar date from the Julian day number
setJD(double JD)63 void HebrewCalendar::setJD(double JD)
64 {
65 this->JD=JD;
66
67 int rd=fixedFromJD(JD, true);
68 parts=hebrewFromFixed(rd);
69
70 emit partsChanged(parts);
71 }
72
73 // get a stringlist of calendar date elements sorted from the largest to the smallest.
74 // Year, Month, MonthName, Day, DayName
75 // Again, in this plugin only, note no year zero, and AD/BC counting.
getDateStrings() const76 QStringList HebrewCalendar::getDateStrings() const
77 {
78 const int rd=fixedFromHebrew(parts);
79 const int dow=dayOfWeekFromFixed(rd);
80
81 QStringList list;
82 list << QString::number(parts.at(0)); // 0:year
83 list << QString::number(parts.at(1)); // 1:month
84 QString monthName=monthNames.value(qMin(12, parts.at(1)), "error");
85 if (isLeap(parts.at(0)))
86 {
87 static const QMap<int, QString>adarMap={{12, " I"}, {13, " II"}};
88 monthName.append(adarMap.value(parts.at(1), ""));
89 }
90 list << monthName; // 2:monthName
91 list << QString::number(parts.at(2)); // 3:day
92 list << weekDayNames.value(dow); // 4:weekday
93
94 return list;
95 }
96
97 // get a formatted complete string for a date
getFormattedDateString() const98 QString HebrewCalendar::getFormattedDateString() const
99 {
100 QStringList str=getDateStrings();
101 // TODO: Maybe use QDate with user's localisation here? Weekday has to be taken from our results, though.
102 return QString("%1, %2 - %3 (%4) - %5")
103 .arg(str.at(4)) // weekday
104 .arg(str.at(3)) // day
105 .arg(str.at(1)) // month, numerical
106 .arg(str.at(2)) // month, name
107 .arg(str.at(0));// year
108 }
109
110 // set date from a vector of calendar date elements sorted from the largest to the smallest.
111 // Year-Month[1...12]-Day[1...31]
112 // Time is not changed!
setDate(QVector<int> parts)113 void HebrewCalendar::setDate(QVector<int> parts)
114 {
115 //qDebug() << "JulianCalendar::setDate:" << parts;
116 this->parts=parts;
117 // For the Julian calendar, we really have no year 0 in this plugin.
118 Q_ASSERT(parts.at(0) != 0);
119
120 double rd=fixedFromHebrew(parts);
121 // restore time from JD!
122 double frac=StelUtils::fmodpos(JD+0.5+StelApp::getInstance().getCore()->getUTCOffset(JD)/24., 1.);
123 JD=jdFromFixed(rd+frac, true);
124
125 emit jdChanged(JD);
126 }
127
128 // returns true for leap years.
isLeap(int hYear)129 bool HebrewCalendar::isLeap(int hYear)
130 {
131 return (StelUtils::imod(7*hYear+1, 19) < 7);
132 }
133
134 // return number of months in a hebrew year
lastMonthOfHebrewYear(int hYear)135 HebrewCalendar::month HebrewCalendar::lastMonthOfHebrewYear(int hYear)
136 {
137 return (isLeap(hYear) ? HebrewCalendar::adarii : HebrewCalendar::adar);
138 }
139
140 // return molad of a hebrew month
molad(int hYear,int hMonth)141 double HebrewCalendar::molad(int hYear, int hMonth)
142 {
143 const int y = (hMonth<HebrewCalendar::tishri ? hYear+1 : hYear);
144 const int monthsElapsed = hMonth-HebrewCalendar::tishri+StelUtils::intFloorDiv(235*y-234, 19);
145 double ret=hebrewEpoch-876.0/25920.0;
146 ret+=monthsElapsed*(29.5+793.0/25920.0);
147 return ret;
148 }
149
hebrewCalendarElapsedDays(int hYear)150 int HebrewCalendar::hebrewCalendarElapsedDays(int hYear)
151 {
152 const int monthsElapsed=StelUtils::intFloorDiv(235*hYear-234, 19);
153 const int partsElapsed=12084+13753*monthsElapsed;
154 const int days = 29*monthsElapsed+StelUtils::intFloorDiv(partsElapsed, 25920);
155 if (StelUtils::imod(3*(days+1), 7)<3)
156 return days+1;
157 else
158 return days;
159 }
160
hebrewYearLengthCorrection(int hYear)161 int HebrewCalendar::hebrewYearLengthCorrection(int hYear)
162 {
163 const int ny0=hebrewCalendarElapsedDays(hYear-1);
164 const int ny1=hebrewCalendarElapsedDays(hYear);
165 const int ny2=hebrewCalendarElapsedDays(hYear+1);
166 if (ny2-ny1==356)
167 return 2;
168 else if (ny1-ny0==382)
169 return 1;
170 else
171 return 0;
172 }
173
hebrewNewYear(int hYear)174 int HebrewCalendar::hebrewNewYear(int hYear)
175 {
176 return hebrewEpoch+hebrewCalendarElapsedDays(hYear)+hebrewYearLengthCorrection(hYear);
177 }
178
daysInHebrewYear(int hYear)179 int HebrewCalendar::daysInHebrewYear(int hYear)
180 {
181 return hebrewNewYear(hYear+1)-hebrewNewYear(hYear);
182 }
183
longMarheshvan(int hYear)184 bool HebrewCalendar::longMarheshvan(int hYear)
185 {
186 return QVector<int>({355, 385}).contains(daysInHebrewYear(hYear));
187 }
188
shortKislev(int hYear)189 bool HebrewCalendar::shortKislev(int hYear)
190 {
191 return QVector<int>({353, 383}).contains(daysInHebrewYear(hYear));
192 }
193
194 // return number of days
lastDayOfHebrewMonth(int hYear,int hMonth)195 int HebrewCalendar::lastDayOfHebrewMonth(int hYear, int hMonth)
196 {
197 if (QVector<int>({iyyar, tammuz, elul, tevet, adarii}).contains(hMonth))
198 return 29;
199 if ((hMonth==HebrewCalendar::adar) && !isLeap(hYear))
200 return 29;
201 if ((hMonth==HebrewCalendar::marheshvan) && !longMarheshvan(hYear))
202 return 29;
203 if ((hMonth==HebrewCalendar::kislev) && shortKislev(hYear))
204 return 29;
205 return 30;
206 }
207
fixedFromHebrew(QVector<int> hebrew)208 int HebrewCalendar::fixedFromHebrew(QVector<int> hebrew)
209 {
210 const int year=hebrew.at(0);
211 const int month=hebrew.at(1);
212 const int day=hebrew.at(2);
213
214 int ret=hebrewNewYear(year)+day-1;
215 if (month<tishri)
216 {
217 for (int m=tishri; m<=lastMonthOfHebrewYear(year); m++)
218 ret+=lastDayOfHebrewMonth(year, m);
219 for (int m=nisan; m<month; m++)
220 ret+=lastDayOfHebrewMonth(year, m);
221 }
222 else
223 {
224 for (int m=tishri; m<month; m++)
225 ret+=lastDayOfHebrewMonth(year, m);
226 }
227 return ret;
228 }
229
hebrewFromFixed(int rd)230 QVector<int> HebrewCalendar::hebrewFromFixed(int rd)
231 {
232 const int approx = StelUtils::intFloorDiv(98496*(rd-hebrewEpoch), 35975351)+1;
233
234 int year=approx-1;
235 while (hebrewNewYear(year+1)<=rd)
236 year++;
237
238 const int start= (rd<fixedFromHebrew({year, HebrewCalendar::nisan, 1}) ? HebrewCalendar::tishri : HebrewCalendar::nisan);
239
240 int month=start;
241 while (rd>fixedFromHebrew({year, month, lastDayOfHebrewMonth(month, year)}))
242 month++;
243
244 const int day=rd-fixedFromHebrew({year, month, 1})+1;
245 return {year, month, day};
246 }
247