27#include "kcalendarsystemhebrew.h"
29static int hebrewDaysElapsed(
int y);
30static TQString num2heb(
int num,
bool includeMillenium);
45static class h_date * hebrewToGregorian(
int y,
int m,
int d)
47 static class h_date h;
51 s = hebrewDaysElapsed(y);
53 s = hebrewDaysElapsed(y + 1) - s;
55 if (s > 365 && m > 6 )
60 d += (59 * (m - 1) + 1) / 2;
62 if (s % 10 > 4 && m > 2)
64 if (s % 10 < 4 && m > 3)
71 y = (d + 36525) * 4 / 146097 - 1;
72 d -= y / 4 * 146097 + (y % 4) * 36524;
76 s = (d + 366)*4/1461-1;
77 d -= s/4*1461 + (s % 4)*365;
80 m = (d + 245)*12/367-7;
95static class h_date * gregorianToHebrew(
int y,
int m,
int d)
97 static class h_date h;
105 d += 365*y + y/4 + 367*m/12 + 5968;
108 h.hd_dw = (d + 1) % 7;
112 s = hebrewDaysElapsed(y);
113 m = hebrewDaysElapsed(y + 1);
117 m = hebrewDaysElapsed(y + 1);
131 if (s > 365 && m <= 5)
137 d -= (m * s + 3) / 4;
146static TQString num2heb(
int num,
bool includeMillenium)
148 const TQChar decade[] = {0x05D8, 0x05D9, 0x05DB, 0x05DC, 0x05DE,
149 0x05E0, 0x05E1, 0x05E2, 0x05E4, 0x05E6};
152 if (num < 1 || num > 9999)
153 return TQString::number(num);
156 if (includeMillenium || num % 1000 == 0)
157 result += TQChar(0x05D0 - 1 + num / 1000);
162 result += TQChar(0x05EA);
165 result += TQChar(0x05E7 - 1 + num / 100);
169 if (num == 15 || num == 16)
171 result += decade[num / 10];
175 result += TQChar(0x05D0 - 1 + num);
177 if (result.length() == 1)
180 result.insert(result.length() - 1,
'\"');
186static const int HOUR = 1080;
187static const int DAY = 24*HOUR;
188static const int WEEK = 7*DAY;
189#define M(h,p) ((h)*HOUR+p)
190#define MONTH (DAY+M(12,793))
196static int hebrewDaysElapsed(
int y)
203 nm = m*MONTH+M(1+6,779);
211 if (l < 12 && dw == 3 && nm >= M(9 + 6,204) ||
212 l < 7 && dw == 2 && nm>=M(15+6,589))
215 if (dw == 1 || dw == 4 || dw == 6)
224static int long_cheshvan(
int year)
229 gd = hebrewToGregorian(year, 1, 1);
230 first.setYMD(gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1);
232 gd = hebrewToGregorian(year + 1, 1, 1);
233 last.setYMD(gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1);
235 return (first.daysTo(last) % 10 == 5);
242static int short_kislev(
int year)
247 gd = hebrewToGregorian(year, 1, 1);
248 first.setYMD(gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1);
250 gd = hebrewToGregorian(year + 1, 1, 1);
251 last.setYMD(gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1);
253 return (first.daysTo(last) % 10 == 3);
256static bool is_leap_year(
int year)
258 return ((((7 * year) + 1) % 19) < 7);
262KCalendarSystemHebrew::KCalendarSystemHebrew(
const TDELocale * locale)
268KCalendarSystemHebrew::~KCalendarSystemHebrew()
273static class h_date * toHebrew(
const TQDate & date)
276 sd = gregorianToHebrew(date.year(), date.month(), date.day());
283int KCalendarSystemHebrew::year(
const TQDate& date)
const
285 class h_date *sd = toHebrew(date);
290int KCalendarSystemHebrew::monthsInYear(
const TQDate & date )
const
292 if ( is_leap_year( year(date) ) )
299int KCalendarSystemHebrew::weeksInYear(
int year)
const
302 setYMD(temp, year, 1, 1);
304 setYMD(temp, year, monthsInYear(temp), hndays(monthsInYear(temp), year) );
306 int nWeekNumber = weekNumber(temp);
309 temp = temp.addDays(-7);
310 nWeekNumber = weekNumber(temp);
316int KCalendarSystemHebrew::weekNumber(
const TQDate& date,
int * yearNum)
const
318 TQDate firstDayWeek1, lastDayOfYear;
321 int weekDay1, dayOfWeek1InYear;
324 setYMD(firstDayWeek1, y, 1, 1);
325 weekDay1 = dayOfWeek(firstDayWeek1);
330 firstDayWeek1 = addDays(firstDayWeek1 , 7 - weekDay1 + 1);
332 dayOfWeek1InYear = dayOfYear(firstDayWeek1);
334 if ( dayOfYear(date) < dayOfWeek1InYear )
338 return weeksInYear(y - 1);
342 setYMD(lastDayOfYear, y + 1, 1, 1);
343 lastDayOfYear = addDays(lastDayOfYear, -1);
344 if ( (dayOfYear(date) >= daysInYear(date) - dayOfWeek(lastDayOfYear) + 1)
346 && dayOfWeek(lastDayOfYear) < 4)
356 firstDayWeek1 = addDays( firstDayWeek1, -( weekDay1 - 1));
358 week = firstDayWeek1.daysTo(date) / 7 + 1;
365TQString KCalendarSystemHebrew::monthName(
const TQDate& date,
366 bool shortName)
const
368 return monthName(month(date), year(date), shortName);
372TQString KCalendarSystemHebrew::monthNamePossessive(
const TQDate& date,
373 bool shortName)
const
375 return monthNamePossessive(month(date), year(date), shortName);
379TQString KCalendarSystemHebrew::monthName(
int month,
int year,
bool )
const
382 return TQString::null;
383 if ( is_leap_year(year) )
386 return TQString::null;
388 else if ( month > 12 )
389 return TQString::null;
392 if( month == 6 && is_leap_year(year) )
394 else if ( month == 7 && is_leap_year(year) )
396 else if ( month > 7 && is_leap_year(year) )
402 return locale()->translate(
"Tishrey");
404 return locale()->translate(
"Heshvan");
406 return locale()->translate(
"Kislev");
408 return locale()->translate(
"Tevet");
410 return locale()->translate(
"Shvat");
412 return locale()->translate(
"Adar");
414 return locale()->translate(
"Nisan");
416 return locale()->translate(
"Iyar");
418 return locale()->translate(
"Sivan");
420 return locale()->translate(
"Tamuz");
422 return locale()->translate(
"Av");
424 return locale()->translate(
"Elul");
426 return locale()->translate(
"Adar I");
428 return locale()->translate(
"Adar II");
433 return TQString::null;
437TQString KCalendarSystemHebrew::monthNamePossessive(
int month,
int year,
438 bool shortName)
const
440 return "of " + monthName(month, year, shortName);
443bool KCalendarSystemHebrew::setYMD(TQDate & date,
int y,
int m,
int d)
const
445 if( y < minValidYear() || y > maxValidYear() )
447 if( m < 1 || m > (is_leap_year(y) ? 13 : 12) )
449 if( d < 1 || d > hndays(m,y) )
452 class h_date * gd = hebrewToGregorian( y, m, d );
454 return date.setYMD(gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1);
457TQString KCalendarSystemHebrew::weekDayName(
int day,
bool shortName)
const
463TQString KCalendarSystemHebrew::weekDayName(
const TQDate& date,
464 bool shortName)
const
466 return weekDayName(dayOfWeek(date), shortName);
470int KCalendarSystemHebrew::dayOfWeek(
const TQDate& date)
const
472 class h_date *sd = toHebrew(date);
473 if ( sd->hd_dw == 0 )
480int KCalendarSystemHebrew::dayOfYear(
const TQDate & date)
const
483 setYMD(first, year(date), 1, 1);
485 return first.daysTo(date) + 1;
488int KCalendarSystemHebrew::daysInMonth(
const TQDate& date)
const
490 return hndays(month(date), year(date));
493int KCalendarSystemHebrew::hndays(
int mon,
int year)
const
495 if ( mon == 6 && is_leap_year(year) )
497 else if ( mon == 7 && is_leap_year(year) )
499 else if ( mon > 7 && is_leap_year(year) )
502 if( mon == 8 || mon == 10 ||
503 mon == 12 || mon == 4 ||
505 ( mon == 6 && !is_leap_year(year)) ||
506 (mon == 2 && !long_cheshvan(year)) ||
507 (mon == 3 && short_kislev(year)))
515int KCalendarSystemHebrew::minValidYear()
const
517 TQDate date(1753, 1, 1);
524int KCalendarSystemHebrew::maxValidYear()
const
526 TQDate date(8000, 1, 1);
532int KCalendarSystemHebrew::day(
const TQDate& date)
const
534 class h_date *sd = toHebrew(date);
540int KCalendarSystemHebrew::month(
const TQDate& date)
const
542 class h_date *sd = toHebrew(date);
544 int month = sd->hd_mon;
545 if ( is_leap_year( sd->hd_year ) )
549 else if( month == 14 )
551 else if ( month > 6 && month < 13 )
559int KCalendarSystemHebrew::daysInYear(
const TQDate & date)
const
562 setYMD(first, year(date), 1, 1);
563 setYMD(last, year(date) + 1, 1, 1);
565 return first.daysTo(last);
569int KCalendarSystemHebrew::weekDayOfPray()
const
575TQDate KCalendarSystemHebrew::addDays(
const TQDate & date,
int ndays )
const
577 return date.addDays( ndays );
581TQDate KCalendarSystemHebrew::addMonths(
const TQDate & date,
int nmonths )
const
583 TQDate result = date;
585 while ( nmonths > 0 )
587 result = addDays(result, daysInMonth(result));
591 while ( nmonths < 0 )
595 int nDaysInMonth = daysInMonth(addDays(result, -day(result)));
596 result = addDays(result, -nDaysInMonth);
604TQDate KCalendarSystemHebrew::addYears(
const TQDate & date,
int nyears )
const
606 TQDate result = date;
607 int y = year(date) + nyears;
609 setYMD( result, y, month(date), day(date) );
615TQString KCalendarSystemHebrew::calendarName()
const
617 return TQString::fromLatin1(
"hebrew");
621bool KCalendarSystemHebrew::isLunar()
const
627bool KCalendarSystemHebrew::isLunisolar()
const
633bool KCalendarSystemHebrew::isSolar()
const
638TQString KCalendarSystemHebrew::dayString(
const TQDate & pDate,
bool bShort)
const
643 if (locale()->language() == TQString::fromLatin1(
"he"))
644 sResult = num2heb(day(pDate),
false);
651TQString KCalendarSystemHebrew::yearString(
const TQDate & pDate,
bool bShort)
const
656 if (locale()->language() == TQString::fromLatin1(
"he"))
657 sResult = num2heb(year(pDate), !bShort);
664static int heb2num(
const TQString& str,
int & iLength) {
669 int decadeValues[14] = {10, 20, 20, 30, 40, 40, 50,
670 50, 60, 70, 80, 80, 90, 90};
673 for (pos = 0 ; pos < s.length() ; pos++)
676 if (s.length() > pos && (s[pos + 1] == TQChar(
'\'') ||
677 s[pos + 1] == TQChar(
'\"')))
680 s.remove(pos + 1, 1);
683 if (c >= TQChar(0x05D0) && c <= TQChar(0x05D7))
685 if (s.length() > pos && s[pos + 1] >= TQChar(0x05D0) &&
686 s[pos + 1] <= TQChar(0x05EA))
687 result += (c.unicode() - 0x05D0 + 1) * 1000;
689 result += c.unicode() - 0x05D0 + 1;
691 else if (c == TQChar(0x05D8))
693 if (s.length() > pos && s[pos + 1] >= TQChar(0x05D0) &&
694 s[pos + 1] <= TQChar(0x05EA) && s[pos + 1] != TQChar(0x05D5) &&
695 s[pos + 1] != TQChar(0x05D6))
700 else if (c >= TQChar(0x05D9) && c <= TQChar(0x05E6))
702 if (s.length() > pos && s[pos + 1] >= TQChar(0x05D9))
705 result += decadeValues[c.unicode() - 0x05D9];
707 else if (c >= TQChar(0x05E7) && c <= TQChar(0x05EA))
709 result += (c.unicode() - 0x05E7 + 1) * 100;
722int KCalendarSystemHebrew::dayStringToInteger(
const TQString & sNum,
int & iLength)
const
725 if (locale()->language() ==
"he")
726 iResult= heb2num(sNum, iLength);
733int KCalendarSystemHebrew::yearStringToInteger(
const TQString & sNum,
int & iLength)
const
736 if (locale()->language() ==
"he")
737 iResult = heb2num(sNum, iLength);
CalendarSystem abstract class, default derived kde gregorian class and factory class.
virtual TQString weekDayName(int weekDay, bool shortName=false) const =0
Gets specific calendar type week day name If an invalid week day is specified, TQString::null is retu...
virtual int yearStringToInteger(const TQString &sNum, int &iLength) const
Converts a year literal of a part of a string into a integer starting at the beginning of the string.
virtual TQString dayString(const TQDate &pDate, bool bShort) const
Converts a date into a day literal.
virtual TQString yearString(const TQDate &pDate, bool bShort) const
Converts a date into a year literal.
TDELocale provides support for country specific stuff like the national language.