1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: calendar_jewish.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
37 #include "calendar_jewish.hxx"
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::lang
;
41 using namespace ::com::sun::star::i18n
;
42 using namespace ::rtl
;
44 #define ERROR RuntimeException()
47 //static UErrorCode status; // status is shared in all calls to Calendar, it has to be reset for each call.
49 Calendar_jewish::Calendar_jewish()
51 cCalendar
= "com.sun.star.i18n.Calendar_jewish";
54 // The following C++ code is translated from the Lisp code in
55 // ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
56 // Software---Practice & Experience, vol. 20, no. 9 (September, 1990),
59 // This code is in the public domain, but any use of it
60 // should acknowledge its source.
61 // http://www.ntu.edu.sg/home/ayxyan/date1.txt
65 const int HebrewEpoch
= -1373429; // Absolute date of start of Hebrew calendar
67 // True if year is an Hebrew leap year
68 sal_Bool
HebrewLeapYear(sal_Int32 year
) {
69 return ((((7 * year
) + 1) % 19) < 7);
72 // Last month of Hebrew year.
73 sal_Int32
LastMonthOfHebrewYear(sal_Int32 year
) {
74 return (HebrewLeapYear(year
)) ? 13 : 12;
77 // Number of days elapsed from the Sunday prior to the start of the
78 // Hebrew calendar to the mean conjunction of Tishri of Hebrew year.
79 sal_Int32
HebrewCalendarElapsedDays(sal_Int32 year
) {
80 sal_Int32 MonthsElapsed
=
81 (235 * ((year
- 1) / 19)) // Months in complete cycles so far.
82 + (12 * ((year
- 1) % 19)) // Regular months in this cycle.
83 + (7 * ((year
- 1) % 19) + 1) / 19; // Leap months this cycle
84 sal_Int32 PartsElapsed
= 204 + 793 * (MonthsElapsed
% 1080);
86 5 + 12 * MonthsElapsed
+ 793 * (MonthsElapsed
/ 1080)
87 + PartsElapsed
/ 1080;
88 sal_Int32 ConjunctionDay
= 1 + 29 * MonthsElapsed
+ HoursElapsed
/ 24;
89 sal_Int32 ConjunctionParts
= 1080 * (HoursElapsed
% 24) + PartsElapsed
% 1080;
90 sal_Int32 AlternativeDay
;
92 if ((ConjunctionParts
>= 19440) // If new moon is at or after midday,
93 || (((ConjunctionDay
% 7) == 2) // ...or is on a Tuesday...
94 && (ConjunctionParts
>= 9924) // at 9 hours, 204 parts or later...
95 && !(HebrewLeapYear(year
))) // ...of a common year,
96 || (((ConjunctionDay
% 7) == 1) // ...or is on a Monday at...
97 && (ConjunctionParts
>= 16789) // 15 hours, 589 parts or later...
98 && (HebrewLeapYear(year
- 1))))// at the end of a leap year
99 // Then postpone Rosh HaShanah one day
100 AlternativeDay
= ConjunctionDay
+ 1;
102 AlternativeDay
= ConjunctionDay
;
104 if (((AlternativeDay
% 7) == 0)// If Rosh HaShanah would occur on Sunday,
105 || ((AlternativeDay
% 7) == 3) // or Wednesday,
106 || ((AlternativeDay
% 7) == 5)) // or Friday
107 // Then postpone it one (more) day
108 return (1+ AlternativeDay
);
110 return AlternativeDay
;
113 // Number of days in Hebrew year.
114 sal_Int32
DaysInHebrewYear(sal_Int32 year
) {
115 return ((HebrewCalendarElapsedDays(year
+ 1)) -
116 (HebrewCalendarElapsedDays(year
)));
119 // True if Heshvan is long in Hebrew year.
120 sal_Bool
LongHeshvan(sal_Int32 year
) {
121 return ((DaysInHebrewYear(year
) % 10) == 5);
124 // True if Kislev is short in Hebrew year.
125 sal_Bool
ShortKislev(sal_Int32 year
) {
126 return ((DaysInHebrewYear(year
) % 10) == 3);
129 // Last day of month in Hebrew year.
130 sal_Int32
LastDayOfHebrewMonth(sal_Int32 month
, sal_Int32 year
) {
134 || ((month
== 8) && !(LongHeshvan(year
)))
135 || ((month
== 9) && ShortKislev(year
))
137 || ((month
== 12) && !(HebrewLeapYear(year
)))
147 sal_Int32 year
; // 1...
148 sal_Int32 month
; // 1..LastMonthOfHebrewYear(year)
149 sal_Int32 day
; // 1..LastDayOfHebrewMonth(month, year)
152 HebrewDate(sal_Int32 m
, sal_Int32 d
, sal_Int32 y
) { month
= m
; day
= d
; year
= y
; }
154 HebrewDate(sal_Int32 d
) { // Computes the Hebrew date from the absolute date.
155 year
= (d
+ HebrewEpoch
) / 366; // Approximation from below.
156 // Search forward for year from the approximation.
157 while (d
>= HebrewDate(7,1,year
+ 1))
159 // Search forward for month from either Tishri or Nisan.
160 if (d
< HebrewDate(1, 1, year
))
161 month
= 7; // Start at Tishri
163 month
= 1; // Start at Nisan
164 while (d
> HebrewDate(month
, (LastDayOfHebrewMonth(month
,year
)), year
))
166 // Calculate the day by subtraction.
167 day
= d
- HebrewDate(month
, 1, year
) + 1;
170 operator int() { // Computes the absolute date of Hebrew date.
171 sal_Int32 DayInYear
= day
; // Days so far this month.
172 if (month
< 7) { // Before Tishri, so add days in prior months
173 // this year before and after Nisan.
175 while (m
<= (LastMonthOfHebrewYear(year
))) {
176 DayInYear
= DayInYear
+ LastDayOfHebrewMonth(m
, year
);
181 DayInYear
= DayInYear
+ LastDayOfHebrewMonth(m
, year
);
185 else { // Add days in prior months this year
188 DayInYear
= DayInYear
+ LastDayOfHebrewMonth(m
, year
);
193 (HebrewCalendarElapsedDays(year
)// Days in prior years.
194 + HebrewEpoch
)); // Days elapsed before absolute date 1.
197 sal_Int32
GetMonth() { return month
; }
198 sal_Int32
GetDay() { return day
; }
199 sal_Int32
GetYear() { return year
; }
205 int LastDayOfGregorianMonth(int month
, int year
) {
206 // Compute the last date of the month for the Gregorian calendar.
210 if ((((year
% 4) == 0) && ((year
% 100) != 0))
211 || ((year
% 400) == 0))
223 class GregorianDate
{
226 int month
; // 1 == January, ..., 12 == December
227 int day
; // 1..LastDayOfGregorianMonth(month, year)
230 GregorianDate(int m
, int d
, int y
) { month
= m
; day
= d
; year
= y
; }
232 GregorianDate(int d
) { // Computes the Gregorian date from the absolute date.
233 // Search forward year by year from approximate year
235 while (d
>= GregorianDate(1,1,year
+1))
237 // Search forward month by month from January
239 while (d
> GregorianDate(month
, LastDayOfGregorianMonth(month
,year
), year
))
241 day
= d
- GregorianDate(month
,1,year
) + 1;
244 operator int() { // Computes the absolute date from the Gregorian date.
245 int N
= day
; // days this month
246 for (int m
= month
- 1; m
> 0; m
--) // days in prior months this year
247 N
= N
+ LastDayOfGregorianMonth(m
, year
);
250 + 365 * (year
- 1) // days in previous years ignoring leap days
251 + (year
- 1)/4 // Julian leap days before this year...
252 - (year
- 1)/100 // ...minus prior century years...
253 + (year
- 1)/400); // ...plus prior years divisible by 400
256 int GetMonth() { return month
; }
257 int GetDay() { return day
; }
258 int GetYear() { return year
; }
262 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
263 void Calendar_jewish::mapFromGregorian() throw(RuntimeException
)
265 int y
= fieldValue
[CalendarFieldIndex::YEAR
];
266 if (fieldValue
[CalendarFieldIndex::ERA
] == 0)
268 GregorianDate
Temp(fieldValue
[CalendarFieldIndex::MONTH
] + 1, fieldValue
[CalendarFieldIndex::DAY_OF_MONTH
], y
);
271 fieldValue
[CalendarFieldIndex::ERA
] = hd
.GetYear() <= 0 ? 0 : 1;
272 fieldValue
[CalendarFieldIndex::MONTH
] = sal::static_int_cast
<sal_Int16
>( hd
.GetMonth() - 1 );
273 fieldValue
[CalendarFieldIndex::DAY_OF_MONTH
] = (sal_Int16
)hd
.GetDay();
274 fieldValue
[CalendarFieldIndex::YEAR
] = (sal_Int16
)(hd
.GetYear() <= 0 ? 1 - hd
.GetYear() : hd
.GetYear());
277 #define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR) | (1 << CalendarFieldIndex::MONTH) | (1 << CalendarFieldIndex::DAY_OF_MONTH))
278 // map field value from other calendar to gregorian calendar, it should be implemented.
279 void Calendar_jewish::mapToGregorian() throw(RuntimeException
)
281 if (fieldSet
& FIELDS
) {
282 sal_Int16 y
= fieldSetValue
[CalendarFieldIndex::YEAR
];
283 if (fieldSetValue
[CalendarFieldIndex::ERA
] == 0)
285 HebrewDate
Temp(fieldSetValue
[CalendarFieldIndex::MONTH
] + 1, fieldSetValue
[CalendarFieldIndex::DAY_OF_MONTH
], y
);
286 GregorianDate
gd(Temp
);
288 fieldSetValue
[CalendarFieldIndex::ERA
] = gd
.GetYear() <= 0 ? 0 : 1;
289 fieldSetValue
[CalendarFieldIndex::MONTH
] = sal::static_int_cast
<sal_Int16
>( gd
.GetMonth() - 1 );
290 fieldSetValue
[CalendarFieldIndex::DAY_OF_MONTH
] = (sal_Int16
)gd
.GetDay();
291 fieldSetValue
[CalendarFieldIndex::YEAR
] = (sal_Int16
)(gd
.GetYear() <= 0 ? 1 - gd
.GetYear() : gd
.GetYear());
296 // Methods in XExtendedCalendar
298 Calendar_jewish::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
299 throw (RuntimeException
)
301 nNativeNumberMode
= NativeNumberMode::NATNUM2
; // make Hebrew number for Jewish calendar
303 if (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
) {
304 sal_Int32 value
= getValue(CalendarFieldIndex::YEAR
) % 1000; // take last 3 digits
305 return aNatNum
.getNativeNumberString(OUString::valueOf(value
), aLocale
, nNativeNumberMode
);
308 return Calendar_gregorian::getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);