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::uno;
28 using namespace ::com::sun::star::i18n;
29 using namespace ::com::sun::star::lang;
30 
31 namespace i18npool {
32 
33 // not used
34 //static UErrorCode status; // status is shared in all calls to Calendar, it has to be reset for each call.
35 
Calendar_jewish()36 Calendar_jewish::Calendar_jewish()
37 {
38     cCalendar = "com.sun.star.i18n.Calendar_jewish";
39 }
40 
41 // The following C++ code is translated from the Lisp code in
42 // ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
43 // Software---Practice & Experience, vol. 20, no. 9 (September, 1990),
44 // pp. 899--928.
45 
46 // This code is in the public domain, but any use of it
47 // should acknowledge its source.
48 // https://web.archive.org/web/20010222054702/http://www.ntu.edu.sg/home/ayxyan/date1.txt
49 
50 // Hebrew dates
51 
52 const int HebrewEpoch = -1373429; // Absolute date of start of Hebrew calendar
53 
54 // True if year is an Hebrew leap year
HebrewLeapYear(sal_Int32 year)55 static bool HebrewLeapYear(sal_Int32 year) {
56     return ((((7 * year) + 1) % 19) < 7);
57 }
58 
59 // Last month of Hebrew year.
LastMonthOfHebrewYear(sal_Int32 year)60 static sal_Int32 LastMonthOfHebrewYear(sal_Int32 year) {
61     return  (HebrewLeapYear(year)) ? 13 : 12;
62 }
63 
64 // Number of days elapsed from the Sunday prior to the start of the
65 // Hebrew calendar to the mean conjunction of Tishri of Hebrew year.
HebrewCalendarElapsedDays(sal_Int32 year)66 static sal_Int32 HebrewCalendarElapsedDays(sal_Int32 year) {
67     sal_Int32 MonthsElapsed =
68         (235 * ((year - 1) / 19))           // Months in complete cycles so far.
69         + (12 * ((year - 1) % 19))          // Regular months in this cycle.
70         + (7 * ((year - 1) % 19) + 1) / 19; // Leap months this cycle
71     sal_Int32 PartsElapsed = 204 + 793 * (MonthsElapsed % 1080);
72     int HoursElapsed =
73         5 + 12 * MonthsElapsed + 793 * (MonthsElapsed  / 1080)
74         + PartsElapsed / 1080;
75     sal_Int32 ConjunctionDay = 1 + 29 * MonthsElapsed + HoursElapsed / 24;
76     sal_Int32 ConjunctionParts = 1080 * (HoursElapsed % 24) + PartsElapsed % 1080;
77     sal_Int32 AlternativeDay;
78 
79     if ((ConjunctionParts >= 19440)        // If new moon is at or after midday,
80           || (((ConjunctionDay % 7) == 2)    // ...or is on a Tuesday...
81           && (ConjunctionParts >= 9924)  // at 9 hours, 204 parts or later...
82           && !(HebrewLeapYear(year)))   // ...of a common year,
83           || (((ConjunctionDay % 7) == 1)    // ...or is on a Monday at...
84           && (ConjunctionParts >= 16789) // 15 hours, 589 parts or later...
85           && (HebrewLeapYear(year - 1))))// at the end of a leap year
86         // Then postpone Rosh HaShanah one day
87         AlternativeDay = ConjunctionDay + 1;
88     else
89         AlternativeDay = ConjunctionDay;
90 
91     if (((AlternativeDay % 7) == 0)// If Rosh HaShanah would occur on Sunday,
92           || ((AlternativeDay % 7) == 3)     // or Wednesday,
93           || ((AlternativeDay % 7) == 5))    // or Friday
94         // Then postpone it one (more) day
95         return (1+ AlternativeDay);
96     else
97         return AlternativeDay;
98 }
99 
100 // Number of days in Hebrew year.
DaysInHebrewYear(sal_Int32 year)101 static sal_Int32 DaysInHebrewYear(sal_Int32 year) {
102     return ((HebrewCalendarElapsedDays(year + 1)) -
103           (HebrewCalendarElapsedDays(year)));
104 }
105 
106 // True if Heshvan is long in Hebrew year.
LongHeshvan(sal_Int32 year)107 static bool LongHeshvan(sal_Int32 year) {
108     return ((DaysInHebrewYear(year) % 10) == 5);
109 }
110 
111 // True if Kislev is short in Hebrew year.
ShortKislev(sal_Int32 year)112 static bool ShortKislev(sal_Int32 year) {
113     return ((DaysInHebrewYear(year) % 10) == 3);
114 }
115 
116 // Last day of month in Hebrew year.
LastDayOfHebrewMonth(sal_Int32 month,sal_Int32 year)117 static sal_Int32 LastDayOfHebrewMonth(sal_Int32 month, sal_Int32 year) {
118     if ((month == 2)
119         || (month == 4)
120         || (month == 6)
121         || ((month == 8) && !(LongHeshvan(year)))
122         || ((month == 9) && ShortKislev(year))
123         || (month == 10)
124         || ((month == 12) && !(HebrewLeapYear(year)))
125         || (month == 13))
126         return 29;
127     else
128         return 30;
129 }
130 
131 namespace {
132 
133 class HebrewDate {
134 private:
135     sal_Int32 year;   // 1...
136     sal_Int32 month;  // 1..LastMonthOfHebrewYear(year)
137     sal_Int32 day;    // 1..LastDayOfHebrewMonth(month, year)
138 
139 public:
HebrewDate(sal_Int32 m,sal_Int32 d,sal_Int32 y)140     HebrewDate(sal_Int32 m, sal_Int32 d, sal_Int32 y) : year(y), month(m), day(d) { }
141 
HebrewDate(sal_Int32 d)142     explicit HebrewDate(sal_Int32 d) { // Computes the Hebrew date from the absolute date.
143     year = (d + HebrewEpoch) / 366; // Approximation from below.
144     // Search forward for year from the approximation.
145     while (d >= HebrewDate(7,1,year + 1).GetAbsoluteDate())
146       year++;
147     // Search forward for month from either Tishri or Nisan.
148     if (d < HebrewDate(1, 1, year).GetAbsoluteDate())
149       month = 7;  //  Start at Tishri
150     else
151       month = 1;  //  Start at Nisan
152     while (d > HebrewDate(month, (LastDayOfHebrewMonth(month,year)), year).GetAbsoluteDate())
153       month++;
154     // Calculate the day by subtraction.
155     day = d - HebrewDate(month, 1, year).GetAbsoluteDate() + 1;
156     }
157 
GetAbsoluteDate() const158     int GetAbsoluteDate() const { // Computes the absolute date of Hebrew date.
159     sal_Int32 DayInYear = day; // Days so far this month.
160     if (month < 7) { // Before Tishri, so add days in prior months
161              // this year before and after Nisan.
162       sal_Int32 m = 7;
163       while (m <= (LastMonthOfHebrewYear(year))) {
164         DayInYear = DayInYear + LastDayOfHebrewMonth(m, year);
165         m++;
166       };
167       m = 1;
168       while (m < month) {
169         DayInYear = DayInYear + LastDayOfHebrewMonth(m, year);
170         m++;
171       }
172     }
173     else { // Add days in prior months this year
174       sal_Int32 m = 7;
175       while (m < month) {
176         DayInYear = DayInYear + LastDayOfHebrewMonth(m, year);
177         m++;
178       }
179     }
180     return (DayInYear +
181         (HebrewCalendarElapsedDays(year)// Days in prior years.
182          + HebrewEpoch));         // Days elapsed before absolute date 1.
183     }
184 
GetMonth() const185     sal_Int32 GetMonth() const { return month; }
GetDay() const186     sal_Int32 GetDay() const { return day; }
GetYear() const187     sal_Int32 GetYear() const { return year; }
188 
189 };
190 
191 }
192 
193 //  Gregorian dates
194 
LastDayOfGregorianMonth(int month,int year)195 static int LastDayOfGregorianMonth(int month, int year) {
196 // Compute the last date of the month for the Gregorian calendar.
197 
198     switch (month) {
199     case 2:
200     if ((((year % 4) == 0) && ((year % 100) != 0))
201         || ((year % 400) == 0))
202       return 29;
203     else
204       return 28;
205     case 4:
206     case 6:
207     case 9:
208     case 11: return 30;
209     default: return 31;
210     }
211 }
212 
213 namespace {
214 
215 class GregorianDate {
216 private:
217     int year;   // 1...
218     int month;  // 1 == January, ..., 12 == December
219     int day;    // 1..LastDayOfGregorianMonth(month, year)
220 
221 public:
GregorianDate(int m,int d,int y)222     GregorianDate(int m, int d, int y) { month = m; day = d; year = y; }
223 
GregorianDate(int d)224     explicit GregorianDate(int d) { // Computes the Gregorian date from the absolute date.
225         // Search forward year by year from approximate year
226         year = d/366;
227         while (d >= GregorianDate(1,1,year+1).GetAbsoluteDate())
228           year++;
229         // Search forward month by month from January
230         month = 1;
231         while (d > GregorianDate(month, LastDayOfGregorianMonth(month,year), year).GetAbsoluteDate())
232           month++;
233         day = d - GregorianDate(month,1,year).GetAbsoluteDate() + 1;
234     }
235 
GetAbsoluteDate() const236     int GetAbsoluteDate() const { // Computes the absolute date from the Gregorian date.
237         int N = day;           // days this month
238         for (int m = month - 1;  m > 0; m--) // days in prior months this year
239           N = N + LastDayOfGregorianMonth(m, year);
240         return
241           (N                    // days this year
242            + 365 * (year - 1)   // days in previous years ignoring leap days
243            + (year - 1)/4       // Julian leap days before this year...
244            - (year - 1)/100     // ...minus prior century years...
245            + (year - 1)/400);   // ...plus prior years divisible by 400
246     }
247 
GetMonth() const248     int GetMonth() const { return month; }
GetDay() const249     int GetDay() const { return day; }
GetYear() const250     int GetYear() const { return year; }
251 
252 };
253 
254 }
255 
256 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
mapFromGregorian()257 void Calendar_jewish::mapFromGregorian()
258 {
259     int y = fieldValue[CalendarFieldIndex::YEAR];
260     if (fieldValue[CalendarFieldIndex::ERA] == 0)
261         y = 1 - y;
262     GregorianDate Temp(fieldValue[CalendarFieldIndex::MONTH] + 1, fieldValue[CalendarFieldIndex::DAY_OF_MONTH], y);
263     HebrewDate hd(Temp.GetAbsoluteDate());
264 
265     fieldValue[CalendarFieldIndex::ERA] = hd.GetYear() <= 0 ? 0 : 1;
266     fieldValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>( hd.GetMonth() - 1 );
267     fieldValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(hd.GetDay());
268     fieldValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(hd.GetYear() <= 0 ? 1 - hd.GetYear() : hd.GetYear());
269 }
270 
271 #define FIELDS  ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR) | (1 << CalendarFieldIndex::MONTH) | (1 << CalendarFieldIndex::DAY_OF_MONTH))
272 // map field value from other calendar to gregorian calendar, it should be implemented.
mapToGregorian()273 void Calendar_jewish::mapToGregorian()
274 {
275     if (!(fieldSet & FIELDS))
276         return;
277 
278     sal_Int16 y = fieldSetValue[CalendarFieldIndex::YEAR];
279     if (fieldSetValue[CalendarFieldIndex::ERA] == 0)
280         y = 1 - y;
281     HebrewDate Temp(fieldSetValue[CalendarFieldIndex::MONTH] + 1, fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH], y);
282     GregorianDate gd(Temp.GetAbsoluteDate());
283 
284     fieldSetValue[CalendarFieldIndex::ERA] = gd.GetYear() <= 0 ? 0 : 1;
285     fieldSetValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>( gd.GetMonth() - 1 );
286     fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(gd.GetDay());
287     fieldSetValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(gd.GetYear() <= 0 ? 1 - gd.GetYear() : gd.GetYear());
288     fieldSet |= FIELDS;
289 }
290 
291 // Methods in XExtendedCalendar
292 OUString SAL_CALL
getDisplayString(sal_Int32 nCalendarDisplayCode,sal_Int16)293 Calendar_jewish::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 /*nNativeNumberMode*/ )
294 {
295     const sal_Int16 nNativeNumberMode = NativeNumberMode::NATNUM2;  // make Hebrew number for Jewish calendar
296 
297     if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR) {
298         sal_Int32 value = getValue(CalendarFieldIndex::YEAR) % 1000; // take last 3 digits
299         return mxNatNum->getNativeNumberString(OUString::number(value), aLocale, nNativeNumberMode );
300     }
301     else
302         return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode );
303 }
304 
305 }
306 
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
308