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