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