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