11 #include "converter.h"
12 #include <tdelocale.h>
14 Converter::Converter()
19 Converter::~Converter()
23 long Converter::absolute_from_gregorian(
int year,
int month,
int day)
25 int xyear, day_number;
28 day_number = day + 31 * (month - 1);
31 day_number -= (23 + (4 * month)) / 10;
32 if (gregorian_leap_year_p(year))
45 long Converter::absolute_from_hebrew(
int year,
int month,
int day)
47 long sum = day + hebrew_elapsed_days(year) - 1373429L;
52 int months = hebrew_months_in_year(year);
54 for (i = 7; i <= months; ++i)
55 sum += hebrew_month_length(year, i);
56 for (i = 1; i < month; ++i)
57 sum += hebrew_month_length(year, i);
61 for (i = 7; i < month; ++i)
62 sum += hebrew_month_length(year, i);
69 Converter::gregorian_from_absolute(
long date,
int *yearp,
70 int *monthp,
int *dayp)
74 for (year = date / 366;
75 date >= absolute_from_gregorian(year + 1, 1, 1); ++year) ;
78 && (date >= absolute_from_gregorian(year, 1 + month, 1));
80 day = 1 + date - absolute_from_gregorian(year, month, 1);
88 Converter::hebrew_from_absolute(
long date,
int *yearp,
int *monthp,
91 int year, month, day, gyear, gmonth, gday, months;
93 gregorian_from_absolute(date, &gyear, &gmonth, &gday);
95 while (date >= absolute_from_hebrew(1 + year, 7, 1))
97 months = hebrew_months_in_year(year);
99 date > absolute_from_hebrew(year, month,
100 hebrew_month_length(year, month));
101 month = 1 + (month % months)) ;
102 day = 1 + date - absolute_from_hebrew(year, month, 1);
109 int Converter::hebrew_months_in_year(
int year)
111 if (hebrew_leap_year_p(year))
119 1, Iyar, Sivan, Tamuz, Ab, Elul, Tishrei, Cheshvan, Kislev, Tevet,
120 Shvat, Adar, AdarII, AdarI = 12
125 1, February, March, April, May, June, July, August, September,
126 October, November, December
130 int Converter::hebrew_month_length(
int year,
int month)
150 if ((hebrew_year_length(year) % 10) == 5)
157 if ((hebrew_year_length(year) % 10) == 3)
164 if (hebrew_leap_year_p(year))
176 Converter::secular_month_length(
int year,
195 if (gregorian_leap_year_p(year))
205 bool Converter::gregorian_leap_year_p(
int year)
209 if ((year % 400) == 0)
211 if ((year % 100) == 0)
217 bool Converter::hebrew_leap_year_p(
int year)
239 long Converter::hebrew_elapsed_days(
int year)
241 static int saved_year[MEMORY] = { -1, -1, -1, -1, -1 };
242 static long saved_value[MEMORY];
245 for (i = 0; i < MEMORY; ++i)
246 if (year == saved_year[i])
247 return saved_value[i];
248 for (i = 0; i < MEMORY-1; ++i) {
249 saved_year[i] = saved_year[1 + i];
250 saved_value[i] = saved_value[1 + i];
252 saved_year[MEMORY - 1] = year;
253 saved_value[MEMORY - 1] = hebrew_elapsed_days2(year);
254 return saved_value[MEMORY - 1];
257 long Converter::hebrew_elapsed_days2(
int year)
259 long prev_year = year - 1;
260 long months_elapsed = 235L * (prev_year / 19)
261 + 12L * (prev_year % 19)
262 + (((prev_year % 19) * 7 + 1) / 19);
263 long parts_elapsed = 5604 + 13753 * months_elapsed;
264 long day = 1 + 29 * months_elapsed + parts_elapsed / 25920;
265 long parts = parts_elapsed % 25920;
266 int weekday = (day % 7);
267 long alt_day = ((parts >= 19440)
268 || (weekday == 2 && (parts >= 9924)
269 && !hebrew_leap_year_p(year)) || (weekday == 1
288 int Converter::hebrew_year_length(
int year)
290 return hebrew_elapsed_days(1 + year) - hebrew_elapsed_days(year);
295 Converter::SecularToHebrewConversion(
int syear,
int smonth,
297 struct DateResult *result)
299 int hyear, hmonth, hday;
302 absolute = absolute_from_gregorian(syear, smonth, sday);
304 hebrew_from_absolute(absolute, &hyear, &hmonth, &hday);
306 result->year = hyear;
307 result->month = hmonth;
309 finish_up(absolute, hyear, hmonth, syear, smonth, result);
314 Converter::HebrewToSecularConversion(
int hyear,
int hmonth,
316 struct DateResult *result)
318 int syear, smonth, sday;
321 absolute = absolute_from_hebrew(hyear, hmonth, hday);
322 gregorian_from_absolute(absolute, &syear, &smonth, &sday);
323 result->year = hyear;
324 result->month = hmonth;
326 finish_up(absolute, hyear, hmonth, syear, smonth, result);
331 Converter::finish_up(
long absolute,
int hyear,
int hmonth,
332 int syear,
int smonth,
333 struct DateResult *result)
335 result->hebrew_month_length = hebrew_month_length(hyear, hmonth);
336 result->secular_month_length = secular_month_length(syear, smonth);
337 result->hebrew_leap_year_p = hebrew_leap_year_p(hyear);
338 result->secular_leap_year_p = gregorian_leap_year_p(syear);
339 result->kvia = (hebrew_year_length(hyear) % 10) - 3;
341 result->day_of_week = (7 + absolute) % 7;
342 result->hebrew_day_number =
343 absolute - absolute_from_hebrew(hyear, 7, 1) + 1;