/src/libreoffice/i18npool/source/calendar/calendar_jewish.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | |
21 | | #include <calendar_jewish.hxx> |
22 | | #include <nativenumbersupplier.hxx> |
23 | | |
24 | | #include <com/sun/star/i18n/CalendarDisplayCode.hpp> |
25 | | #include <com/sun/star/i18n/NativeNumberMode.hpp> |
26 | | |
27 | | using namespace ::com::sun::star::i18n; |
28 | | |
29 | | namespace i18npool { |
30 | | |
31 | | // not used |
32 | | //static UErrorCode status; // status is shared in all calls to Calendar, it has to be reset for each call. |
33 | | |
34 | | Calendar_jewish::Calendar_jewish() |
35 | 1.57k | { |
36 | 1.57k | cCalendar = u"com.sun.star.i18n.Calendar_jewish"_ustr; |
37 | 1.57k | } |
38 | | |
39 | | // The following C++ code is translated from the Lisp code in |
40 | | // ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold, |
41 | | // Software---Practice & Experience, vol. 20, no. 9 (September, 1990), |
42 | | // pp. 899--928. |
43 | | |
44 | | // This code is in the public domain, but any use of it |
45 | | // should acknowledge its source. |
46 | | // https://web.archive.org/web/20010222054702/http://www.ntu.edu.sg/home/ayxyan/date1.txt |
47 | | |
48 | | // Hebrew dates |
49 | | |
50 | | const int HebrewEpoch = -1373429; // Absolute date of start of Hebrew calendar |
51 | | |
52 | | // True if year is an Hebrew leap year |
53 | 40.2M | static bool HebrewLeapYear(sal_Int32 year) { |
54 | 40.2M | return ((((7 * year) + 1) % 19) < 7); |
55 | 40.2M | } |
56 | | |
57 | | // Last month of Hebrew year. |
58 | 759k | static sal_Int32 LastMonthOfHebrewYear(sal_Int32 year) { |
59 | 759k | return (HebrewLeapYear(year)) ? 13 : 12; |
60 | 759k | } |
61 | | |
62 | | // Number of days elapsed from the Sunday prior to the start of the |
63 | | // Hebrew calendar to the mean conjunction of Tishri of Hebrew year. |
64 | 752M | static sal_Int32 HebrewCalendarElapsedDays(sal_Int32 year) { |
65 | 752M | sal_Int32 MonthsElapsed = |
66 | 752M | (235 * ((year - 1) / 19)) // Months in complete cycles so far. |
67 | 752M | + (12 * ((year - 1) % 19)) // Regular months in this cycle. |
68 | 752M | + (7 * ((year - 1) % 19) + 1) / 19; // Leap months this cycle |
69 | 752M | sal_Int32 PartsElapsed = 204 + 793 * (MonthsElapsed % 1080); |
70 | 752M | int HoursElapsed = |
71 | 752M | 5 + 12 * MonthsElapsed + 793 * (MonthsElapsed / 1080) |
72 | 752M | + PartsElapsed / 1080; |
73 | 752M | sal_Int32 ConjunctionDay = 1 + 29 * MonthsElapsed + HoursElapsed / 24; |
74 | 752M | sal_Int32 ConjunctionParts = 1080 * (HoursElapsed % 24) + PartsElapsed % 1080; |
75 | 752M | sal_Int32 AlternativeDay; |
76 | | |
77 | 752M | if ((ConjunctionParts >= 19440) // If new moon is at or after midday, |
78 | 609M | || (((ConjunctionDay % 7) == 2) // ...or is on a Tuesday... |
79 | 62.0M | && (ConjunctionParts >= 9924) // at 9 hours, 204 parts or later... |
80 | 30.8M | && !(HebrewLeapYear(year))) // ...of a common year, |
81 | 590M | || (((ConjunctionDay % 7) == 1) // ...or is on a Monday at... |
82 | 62.0M | && (ConjunctionParts >= 16789) // 15 hours, 589 parts or later... |
83 | 8.43M | && (HebrewLeapYear(year - 1))))// at the end of a leap year |
84 | | // Then postpone Rosh HaShanah one day |
85 | 164M | AlternativeDay = ConjunctionDay + 1; |
86 | 587M | else |
87 | 587M | AlternativeDay = ConjunctionDay; |
88 | | |
89 | 752M | if (((AlternativeDay % 7) == 0)// If Rosh HaShanah would occur on Sunday, |
90 | 645M | || ((AlternativeDay % 7) == 3) // or Wednesday, |
91 | 544M | || ((AlternativeDay % 7) == 5)) // or Friday |
92 | | // Then postpone it one (more) day |
93 | 289M | return (1+ AlternativeDay); |
94 | 463M | else |
95 | 463M | return AlternativeDay; |
96 | 752M | } |
97 | | |
98 | | // Number of days in Hebrew year. |
99 | 1.03M | static sal_Int32 DaysInHebrewYear(sal_Int32 year) { |
100 | 1.03M | return ((HebrewCalendarElapsedDays(year + 1)) - |
101 | 1.03M | (HebrewCalendarElapsedDays(year))); |
102 | 1.03M | } |
103 | | |
104 | | // True if Heshvan is long in Hebrew year. |
105 | 566k | static bool LongHeshvan(sal_Int32 year) { |
106 | 566k | return ((DaysInHebrewYear(year) % 10) == 5); |
107 | 566k | } |
108 | | |
109 | | // True if Kislev is short in Hebrew year. |
110 | 467k | static bool ShortKislev(sal_Int32 year) { |
111 | 467k | return ((DaysInHebrewYear(year) % 10) == 3); |
112 | 467k | } |
113 | | |
114 | | // Last day of month in Hebrew year. |
115 | 2.43M | static sal_Int32 LastDayOfHebrewMonth(sal_Int32 month, sal_Int32 year) { |
116 | 2.43M | if ((month == 2) |
117 | 2.43M | || (month == 4) |
118 | 2.43M | || (month == 6) |
119 | 2.43M | || ((month == 8) && !(LongHeshvan(year))) |
120 | 1.89M | || ((month == 9) && ShortKislev(year)) |
121 | 1.89M | || (month == 10) |
122 | 1.59M | || ((month == 12) && !(HebrewLeapYear(year))) |
123 | 1.53M | || (month == 13)) |
124 | 969k | return 29; |
125 | 1.46M | else |
126 | 1.46M | return 30; |
127 | 2.43M | } |
128 | | |
129 | | namespace { |
130 | | |
131 | | class HebrewDate { |
132 | | private: |
133 | | sal_Int32 year; // 1... |
134 | | sal_Int32 month; // 1..LastMonthOfHebrewYear(year) |
135 | | sal_Int32 day; // 1..LastDayOfHebrewMonth(month, year) |
136 | | |
137 | | public: |
138 | 750M | HebrewDate(sal_Int32 m, sal_Int32 d, sal_Int32 y) : year(y), month(m), day(d) { } |
139 | | |
140 | 99.7k | explicit HebrewDate(sal_Int32 d) { // Computes the Hebrew date from the absolute date. |
141 | | |
142 | 99.7k | if (d == 730119) // 1999-12-31 : a very common date used in calc |
143 | 0 | { |
144 | 0 | year = 5760; |
145 | 0 | month = 10; |
146 | 0 | day = 22; |
147 | 0 | return; |
148 | 0 | } |
149 | | |
150 | 99.7k | year = (d + HebrewEpoch) / 366; // Approximation from below. |
151 | | // Search forward for year from the approximation. |
152 | 749M | while (d >= HebrewDate(7,1,year + 1).GetAbsoluteDate()) |
153 | 749M | year++; |
154 | | // Search forward for month from either Tishri or Nisan. |
155 | 99.7k | if (d < HebrewDate(1, 1, year).GetAbsoluteDate()) |
156 | 99.2k | month = 7; // Start at Tishri |
157 | 488 | else |
158 | 488 | month = 1; // Start at Nisan |
159 | 468k | while (d > HebrewDate(month, (LastDayOfHebrewMonth(month,year)), year).GetAbsoluteDate()) |
160 | 368k | month++; |
161 | | // Calculate the day by subtraction. |
162 | 99.7k | day = d - HebrewDate(month, 1, year).GetAbsoluteDate() + 1; |
163 | 99.7k | } |
164 | | |
165 | 750M | int GetAbsoluteDate() const { // Computes the absolute date of Hebrew date. |
166 | 750M | sal_Int32 DayInYear = day; // Days so far this month. |
167 | 750M | if (month < 7) { // Before Tishri, so add days in prior months |
168 | | // this year before and after Nisan. |
169 | 101k | sal_Int32 m = 7; |
170 | 759k | while (m <= (LastMonthOfHebrewYear(year))) { |
171 | 657k | DayInYear = DayInYear + LastDayOfHebrewMonth(m, year); |
172 | 657k | m++; |
173 | 657k | }; |
174 | 101k | m = 1; |
175 | 104k | while (m < month) { |
176 | 2.89k | DayInYear = DayInYear + LastDayOfHebrewMonth(m, year); |
177 | 2.89k | m++; |
178 | 2.89k | } |
179 | 101k | } |
180 | 750M | else { // Add days in prior months this year |
181 | 750M | sal_Int32 m = 7; |
182 | 751M | while (m < month) { |
183 | 1.30M | DayInYear = DayInYear + LastDayOfHebrewMonth(m, year); |
184 | 1.30M | m++; |
185 | 1.30M | } |
186 | 750M | } |
187 | 750M | return (DayInYear + |
188 | 750M | (HebrewCalendarElapsedDays(year)// Days in prior years. |
189 | 750M | + HebrewEpoch)); // Days elapsed before absolute date 1. |
190 | 750M | } |
191 | | |
192 | 99.7k | sal_Int32 GetMonth() const { return month; } |
193 | 99.7k | sal_Int32 GetDay() const { return day; } |
194 | 299k | sal_Int32 GetYear() const { return year; } |
195 | | |
196 | | }; |
197 | | |
198 | | } |
199 | | |
200 | | // Gregorian dates |
201 | | |
202 | 747k | static int LastDayOfGregorianMonth(int month, int year) { |
203 | | // Compute the last date of the month for the Gregorian calendar. |
204 | | |
205 | 747k | switch (month) { |
206 | 85.8k | case 2: |
207 | 85.8k | if ((((year % 4) == 0) && ((year % 100) != 0)) |
208 | 84.3k | || ((year % 400) == 0)) |
209 | 1.55k | return 29; |
210 | 84.3k | else |
211 | 84.3k | return 28; |
212 | 64.5k | case 4: |
213 | 128k | case 6: |
214 | 193k | case 9: |
215 | 255k | case 11: return 30; |
216 | 406k | default: return 31; |
217 | 747k | } |
218 | 747k | } |
219 | | |
220 | | namespace { |
221 | | |
222 | | class GregorianDate { |
223 | | private: |
224 | | int year; // 1... |
225 | | int month; // 1 == January, ..., 12 == December |
226 | | int day; // 1..LastDayOfGregorianMonth(month, year) |
227 | | |
228 | | public: |
229 | 99.7k | GregorianDate(int m, int d, int y) { month = m; day = d; year = y; } |
230 | | |
231 | 0 | explicit GregorianDate(int d) { // Computes the Gregorian date from the absolute date. |
232 | | // Search forward year by year from approximate year |
233 | 0 | year = d/366; |
234 | 0 | while (d >= GregorianDate(1,1,year+1).GetAbsoluteDate()) |
235 | 0 | year++; |
236 | | // Search forward month by month from January |
237 | 0 | month = 1; |
238 | 0 | while (d > GregorianDate(month, LastDayOfGregorianMonth(month,year), year).GetAbsoluteDate()) |
239 | 0 | month++; |
240 | 0 | day = d - GregorianDate(month,1,year).GetAbsoluteDate() + 1; |
241 | 0 | } |
242 | | |
243 | 99.7k | int GetAbsoluteDate() const { // Computes the absolute date from the Gregorian date. |
244 | 99.7k | int N = day; // days this month |
245 | 847k | for (int m = month - 1; m > 0; m--) // days in prior months this year |
246 | 747k | N = N + LastDayOfGregorianMonth(m, year); |
247 | 99.7k | return |
248 | 99.7k | (N // days this year |
249 | 99.7k | + 365 * (year - 1) // days in previous years ignoring leap days |
250 | 99.7k | + (year - 1)/4 // Julian leap days before this year... |
251 | 99.7k | - (year - 1)/100 // ...minus prior century years... |
252 | 99.7k | + (year - 1)/400); // ...plus prior years divisible by 400 |
253 | 99.7k | } |
254 | | |
255 | 0 | int GetMonth() const { return month; } |
256 | 0 | int GetDay() const { return day; } |
257 | 0 | int GetYear() const { return year; } |
258 | | |
259 | | }; |
260 | | |
261 | | } |
262 | | |
263 | | // map field value from gregorian calendar to other calendar, it can be overwritten by derived class. |
264 | | void Calendar_jewish::mapFromGregorian() |
265 | 99.7k | { |
266 | 99.7k | int y = fieldValue[CalendarFieldIndex::YEAR]; |
267 | 99.7k | if (fieldValue[CalendarFieldIndex::ERA] == 0) |
268 | 0 | y = 1 - y; |
269 | 99.7k | GregorianDate Temp(fieldValue[CalendarFieldIndex::MONTH] + 1, fieldValue[CalendarFieldIndex::DAY_OF_MONTH], y); |
270 | 99.7k | HebrewDate hd(Temp.GetAbsoluteDate()); |
271 | | |
272 | 99.7k | fieldValue[CalendarFieldIndex::ERA] = hd.GetYear() <= 0 ? 0 : 1; |
273 | 99.7k | fieldValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>( hd.GetMonth() - 1 ); |
274 | 99.7k | fieldValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(hd.GetDay()); |
275 | 99.7k | fieldValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(hd.GetYear() <= 0 ? 1 - hd.GetYear() : hd.GetYear()); |
276 | 99.7k | } |
277 | | |
278 | 0 | #define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR) | (1 << CalendarFieldIndex::MONTH) | (1 << CalendarFieldIndex::DAY_OF_MONTH)) |
279 | | // map field value from other calendar to gregorian calendar, it should be implemented. |
280 | | void Calendar_jewish::mapToGregorian() |
281 | 0 | { |
282 | 0 | if (!(fieldSet & FIELDS)) |
283 | 0 | return; |
284 | | |
285 | 0 | sal_Int16 y = fieldSetValue[CalendarFieldIndex::YEAR]; |
286 | 0 | if (fieldSetValue[CalendarFieldIndex::ERA] == 0) |
287 | 0 | y = 1 - y; |
288 | 0 | HebrewDate Temp(fieldSetValue[CalendarFieldIndex::MONTH] + 1, fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH], y); |
289 | 0 | GregorianDate gd(Temp.GetAbsoluteDate()); |
290 | |
|
291 | 0 | fieldSetValue[CalendarFieldIndex::ERA] = gd.GetYear() <= 0 ? 0 : 1; |
292 | 0 | fieldSetValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>( gd.GetMonth() - 1 ); |
293 | 0 | fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(gd.GetDay()); |
294 | 0 | fieldSetValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(gd.GetYear() <= 0 ? 1 - gd.GetYear() : gd.GetYear()); |
295 | 0 | fieldSet |= FIELDS; |
296 | 0 | } |
297 | | |
298 | | // Methods in XExtendedCalendar |
299 | | OUString SAL_CALL |
300 | | Calendar_jewish::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 /*nNativeNumberMode*/ ) |
301 | 96.1k | { |
302 | 96.1k | const sal_Int16 nNativeNumberMode = NativeNumberMode::NATNUM2; // make Hebrew number for Jewish calendar |
303 | | |
304 | 96.1k | if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR) { |
305 | 4.18k | sal_Int32 value = getValue(CalendarFieldIndex::YEAR) % 1000; // take last 3 digits |
306 | 4.18k | return mxNatNum->getNativeNumberString(OUString::number(value), aLocale, nNativeNumberMode ); |
307 | 4.18k | } |
308 | 91.9k | else |
309 | 91.9k | return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode ); |
310 | 96.1k | } |
311 | | |
312 | | } |
313 | | |
314 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |