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