1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
24 #include "calendar_jewish.hxx"
26 using namespace ::com::sun::star::uno
;
27 using namespace ::com::sun::star::lang
;
28 using ::rtl::OUString
;
30 #define ERROR RuntimeException()
32 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
35 //static UErrorCode status; // status is shared in all calls to Calendar, it has to be reset for each call.
37 Calendar_jewish::Calendar_jewish()
39 cCalendar
= "com.sun.star.i18n.Calendar_jewish";
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),
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
53 const int HebrewEpoch
= -1373429; // Absolute date of start of Hebrew calendar
55 // True if year is an Hebrew leap year
56 sal_Bool
HebrewLeapYear(sal_Int32 year
) {
57 return ((((7 * year
) + 1) % 19) < 7);
60 // Last month of Hebrew year.
61 sal_Int32
LastMonthOfHebrewYear(sal_Int32 year
) {
62 return (HebrewLeapYear(year
)) ? 13 : 12;
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 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 + (7 * ((year
- 1) % 19) + 1) / 19; // Leap months this cycle
72 sal_Int32 PartsElapsed
= 204 + 793 * (MonthsElapsed
% 1080);
74 5 + 12 * MonthsElapsed
+ 793 * (MonthsElapsed
/ 1080)
75 + PartsElapsed
/ 1080;
76 sal_Int32 ConjunctionDay
= 1 + 29 * MonthsElapsed
+ HoursElapsed
/ 24;
77 sal_Int32 ConjunctionParts
= 1080 * (HoursElapsed
% 24) + PartsElapsed
% 1080;
78 sal_Int32 AlternativeDay
;
80 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 && !(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 && (HebrewLeapYear(year
- 1))))// at the end of a leap year
87 // Then postpone Rosh HaShanah one day
88 AlternativeDay
= ConjunctionDay
+ 1;
90 AlternativeDay
= ConjunctionDay
;
92 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 return (1+ AlternativeDay
);
98 return AlternativeDay
;
101 // Number of days in Hebrew year.
102 sal_Int32
DaysInHebrewYear(sal_Int32 year
) {
103 return ((HebrewCalendarElapsedDays(year
+ 1)) -
104 (HebrewCalendarElapsedDays(year
)));
107 // True if Heshvan is long in Hebrew year.
108 sal_Bool
LongHeshvan(sal_Int32 year
) {
109 return ((DaysInHebrewYear(year
) % 10) == 5);
112 // True if Kislev is short in Hebrew year.
113 sal_Bool
ShortKislev(sal_Int32 year
) {
114 return ((DaysInHebrewYear(year
) % 10) == 3);
117 // Last day of month in Hebrew year.
118 sal_Int32
LastDayOfHebrewMonth(sal_Int32 month
, sal_Int32 year
) {
122 || ((month
== 8) && !(LongHeshvan(year
)))
123 || ((month
== 9) && ShortKislev(year
))
125 || ((month
== 12) && !(HebrewLeapYear(year
)))
135 sal_Int32 year
; // 1...
136 sal_Int32 month
; // 1..LastMonthOfHebrewYear(year)
137 sal_Int32 day
; // 1..LastDayOfHebrewMonth(month, year)
140 HebrewDate(sal_Int32 m
, sal_Int32 d
, sal_Int32 y
) { month
= m
; day
= d
; year
= y
; }
142 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))
147 // Search forward for month from either Tishri or Nisan.
148 if (d
< HebrewDate(1, 1, year
))
149 month
= 7; // Start at Tishri
151 month
= 1; // Start at Nisan
152 while (d
> HebrewDate(month
, (LastDayOfHebrewMonth(month
,year
)), year
))
154 // Calculate the day by subtraction.
155 day
= d
- HebrewDate(month
, 1, year
) + 1;
158 operator int() { // 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.
163 while (m
<= (LastMonthOfHebrewYear(year
))) {
164 DayInYear
= DayInYear
+ LastDayOfHebrewMonth(m
, year
);
169 DayInYear
= DayInYear
+ LastDayOfHebrewMonth(m
, year
);
173 else { // Add days in prior months this year
176 DayInYear
= DayInYear
+ LastDayOfHebrewMonth(m
, year
);
181 (HebrewCalendarElapsedDays(year
)// Days in prior years.
182 + HebrewEpoch
)); // Days elapsed before absolute date 1.
185 sal_Int32
GetMonth() const { return month
; }
186 sal_Int32
GetDay() const { return day
; }
187 sal_Int32
GetYear() const { return year
; }
193 int LastDayOfGregorianMonth(int month
, int year
) {
194 // Compute the last date of the month for the Gregorian calendar.
198 if ((((year
% 4) == 0) && ((year
% 100) != 0))
199 || ((year
% 400) == 0))
211 class GregorianDate
{
214 int month
; // 1 == January, ..., 12 == December
215 int day
; // 1..LastDayOfGregorianMonth(month, year)
218 GregorianDate(int m
, int d
, int y
) { month
= m
; day
= d
; year
= y
; }
220 GregorianDate(int d
) { // Computes the Gregorian date from the absolute date.
221 // Search forward year by year from approximate year
223 while (d
>= GregorianDate(1,1,year
+1))
225 // Search forward month by month from January
227 while (d
> GregorianDate(month
, LastDayOfGregorianMonth(month
,year
), year
))
229 day
= d
- GregorianDate(month
,1,year
) + 1;
232 operator int() { // Computes the absolute date from the Gregorian date.
233 int N
= day
; // days this month
234 for (int m
= month
- 1; m
> 0; m
--) // days in prior months this year
235 N
= N
+ LastDayOfGregorianMonth(m
, 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 + (year
- 1)/400); // ...plus prior years divisible by 400
244 int GetMonth() const { return month
; }
245 int GetDay() const { return day
; }
246 int GetYear() const { return year
; }
250 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
251 void Calendar_jewish::mapFromGregorian() throw(RuntimeException
)
253 int y
= fieldValue
[CalendarFieldIndex::YEAR
];
254 if (fieldValue
[CalendarFieldIndex::ERA
] == 0)
256 GregorianDate
Temp(fieldValue
[CalendarFieldIndex::MONTH
] + 1, fieldValue
[CalendarFieldIndex::DAY_OF_MONTH
], y
);
259 fieldValue
[CalendarFieldIndex::ERA
] = hd
.GetYear() <= 0 ? 0 : 1;
260 fieldValue
[CalendarFieldIndex::MONTH
] = sal::static_int_cast
<sal_Int16
>( hd
.GetMonth() - 1 );
261 fieldValue
[CalendarFieldIndex::DAY_OF_MONTH
] = (sal_Int16
)hd
.GetDay();
262 fieldValue
[CalendarFieldIndex::YEAR
] = (sal_Int16
)(hd
.GetYear() <= 0 ? 1 - hd
.GetYear() : hd
.GetYear());
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 void Calendar_jewish::mapToGregorian() throw(RuntimeException
)
269 if (fieldSet
& FIELDS
) {
270 sal_Int16 y
= fieldSetValue
[CalendarFieldIndex::YEAR
];
271 if (fieldSetValue
[CalendarFieldIndex::ERA
] == 0)
273 HebrewDate
Temp(fieldSetValue
[CalendarFieldIndex::MONTH
] + 1, fieldSetValue
[CalendarFieldIndex::DAY_OF_MONTH
], y
);
274 GregorianDate
gd(Temp
);
276 fieldSetValue
[CalendarFieldIndex::ERA
] = gd
.GetYear() <= 0 ? 0 : 1;
277 fieldSetValue
[CalendarFieldIndex::MONTH
] = sal::static_int_cast
<sal_Int16
>( gd
.GetMonth() - 1 );
278 fieldSetValue
[CalendarFieldIndex::DAY_OF_MONTH
] = (sal_Int16
)gd
.GetDay();
279 fieldSetValue
[CalendarFieldIndex::YEAR
] = (sal_Int16
)(gd
.GetYear() <= 0 ? 1 - gd
.GetYear() : gd
.GetYear());
284 // Methods in XExtendedCalendar
286 Calendar_jewish::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
287 throw (RuntimeException
)
289 nNativeNumberMode
= NativeNumberMode::NATNUM2
; // make Hebrew number for Jewish calendar
291 if (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
) {
292 sal_Int32 value
= getValue(CalendarFieldIndex::YEAR
) % 1000; // take last 3 digits
293 return aNatNum
.getNativeNumberString(OUString::valueOf(value
), aLocale
, nNativeNumberMode
);
296 return Calendar_gregorian::getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */