xref: /PHP-7.0/ext/calendar/calendar.c (revision 6550f3ad)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Shane Caraveo             <shane@caraveo.com>               |
16    |          Colin Viebrock            <colin@easydns.com>               |
17    |          Hartmut Holzgraefe        <hholzgra@php.net>                |
18    |          Wez Furlong               <wez@thebrainroom.com>            |
19    +----------------------------------------------------------------------+
20  */
21 /* $Id$ */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #ifdef PHP_WIN32
28 #define _WINNLS_
29 #endif
30 
31 #include "php.h"
32 #include "ext/standard/info.h"
33 #include "php_calendar.h"
34 #include "sdncal.h"
35 
36 #include <stdio.h>
37 
38 /* {{{ arginfo */
39 ZEND_BEGIN_ARG_INFO_EX(arginfo_unixtojd, 0, 0, 0)
40 	ZEND_ARG_INFO(0, timestamp)
41 ZEND_END_ARG_INFO()
42 
43 ZEND_BEGIN_ARG_INFO(arginfo_jdtounix, 0)
44 	ZEND_ARG_INFO(0, jday)
45 ZEND_END_ARG_INFO()
46 
47 ZEND_BEGIN_ARG_INFO_EX(arginfo_cal_info, 0, 0, 0)
48 	ZEND_ARG_INFO(0, calendar)
49 ZEND_END_ARG_INFO()
50 
51 ZEND_BEGIN_ARG_INFO(arginfo_cal_days_in_month, 0)
52 	ZEND_ARG_INFO(0, calendar)
53 	ZEND_ARG_INFO(0, month)
54 	ZEND_ARG_INFO(0, year)
55 ZEND_END_ARG_INFO()
56 
57 ZEND_BEGIN_ARG_INFO(arginfo_cal_to_jd, 0)
58 	ZEND_ARG_INFO(0, calendar)
59 	ZEND_ARG_INFO(0, month)
60 	ZEND_ARG_INFO(0, day)
61 	ZEND_ARG_INFO(0, year)
62 ZEND_END_ARG_INFO()
63 
64 ZEND_BEGIN_ARG_INFO(arginfo_cal_from_jd, 0)
65 	ZEND_ARG_INFO(0, jd)
66 	ZEND_ARG_INFO(0, calendar)
67 ZEND_END_ARG_INFO()
68 
69 ZEND_BEGIN_ARG_INFO(arginfo_jdtogregorian, 0)
70 	ZEND_ARG_INFO(0, juliandaycount)
71 ZEND_END_ARG_INFO()
72 
73 ZEND_BEGIN_ARG_INFO(arginfo_gregoriantojd, 0)
74 	ZEND_ARG_INFO(0, month)
75 	ZEND_ARG_INFO(0, day)
76 	ZEND_ARG_INFO(0, year)
77 ZEND_END_ARG_INFO()
78 
79 ZEND_BEGIN_ARG_INFO(arginfo_jdtojulian, 0)
80 	ZEND_ARG_INFO(0, juliandaycount)
81 ZEND_END_ARG_INFO()
82 
83 ZEND_BEGIN_ARG_INFO(arginfo_juliantojd, 0)
84 	ZEND_ARG_INFO(0, month)
85 	ZEND_ARG_INFO(0, day)
86 	ZEND_ARG_INFO(0, year)
87 ZEND_END_ARG_INFO()
88 
89 ZEND_BEGIN_ARG_INFO_EX(arginfo_jdtojewish, 0, 0, 1)
90 	ZEND_ARG_INFO(0, juliandaycount)
91 	ZEND_ARG_INFO(0, hebrew)
92 	ZEND_ARG_INFO(0, fl)
93 ZEND_END_ARG_INFO()
94 
95 ZEND_BEGIN_ARG_INFO(arginfo_jewishtojd, 0)
96 	ZEND_ARG_INFO(0, month)
97 	ZEND_ARG_INFO(0, day)
98 	ZEND_ARG_INFO(0, year)
99 ZEND_END_ARG_INFO()
100 
101 ZEND_BEGIN_ARG_INFO(arginfo_jdtofrench, 0)
102 	ZEND_ARG_INFO(0, juliandaycount)
103 ZEND_END_ARG_INFO()
104 
105 ZEND_BEGIN_ARG_INFO(arginfo_frenchtojd, 0)
106 	ZEND_ARG_INFO(0, month)
107 	ZEND_ARG_INFO(0, day)
108 	ZEND_ARG_INFO(0, year)
109 ZEND_END_ARG_INFO()
110 
111 ZEND_BEGIN_ARG_INFO_EX(arginfo_jddayofweek, 0, 0, 1)
112 	ZEND_ARG_INFO(0, juliandaycount)
113 	ZEND_ARG_INFO(0, mode)
114 ZEND_END_ARG_INFO()
115 
116 ZEND_BEGIN_ARG_INFO(arginfo_jdmonthname, 0)
117 	ZEND_ARG_INFO(0, juliandaycount)
118 	ZEND_ARG_INFO(0, mode)
119 ZEND_END_ARG_INFO()
120 
121 ZEND_BEGIN_ARG_INFO_EX(arginfo_easter_date, 0, 0, 0)
122 	ZEND_ARG_INFO(0, year)
123 ZEND_END_ARG_INFO()
124 
125 ZEND_BEGIN_ARG_INFO_EX(arginfo_easter_days, 0, 0, 0)
126 	ZEND_ARG_INFO(0, year)
127 	ZEND_ARG_INFO(0, method)
128 ZEND_END_ARG_INFO()
129 
130 /* }}} */
131 
132 const zend_function_entry calendar_functions[] = {
133 	PHP_FE(jdtogregorian, arginfo_jdtogregorian)
134 	PHP_FE(gregoriantojd, arginfo_gregoriantojd)
135 	PHP_FE(jdtojulian, arginfo_jdtojulian)
136 	PHP_FE(juliantojd, arginfo_juliantojd)
137 	PHP_FE(jdtojewish, arginfo_jdtojewish)
138 	PHP_FE(jewishtojd, arginfo_jewishtojd)
139 	PHP_FE(jdtofrench, arginfo_jdtofrench)
140 	PHP_FE(frenchtojd, arginfo_frenchtojd)
141 	PHP_FE(jddayofweek, arginfo_jddayofweek)
142 	PHP_FE(jdmonthname, arginfo_jdmonthname)
143 	PHP_FE(easter_date, arginfo_easter_date)
144 	PHP_FE(easter_days, arginfo_easter_days)
145 	PHP_FE(unixtojd, arginfo_unixtojd)
146 	PHP_FE(jdtounix, arginfo_jdtounix)
147 	PHP_FE(cal_to_jd, arginfo_cal_to_jd)
148 	PHP_FE(cal_from_jd, arginfo_cal_from_jd)
149 	PHP_FE(cal_days_in_month, arginfo_cal_days_in_month)
150 	PHP_FE(cal_info, arginfo_cal_info)
151 	PHP_FE_END
152 };
153 
154 
155 zend_module_entry calendar_module_entry = {
156 	STANDARD_MODULE_HEADER,
157 	"calendar",
158 	calendar_functions,
159 	PHP_MINIT(calendar),
160 	NULL,
161 	NULL,
162 	NULL,
163 	PHP_MINFO(calendar),
164 	PHP_CALENDAR_VERSION,
165 	STANDARD_MODULE_PROPERTIES,
166 };
167 
168 #ifdef COMPILE_DL_CALENDAR
169 ZEND_GET_MODULE(calendar)
170 #endif
171 
172 /* this order must match the conversion table below */
173 enum cal_name_type_t {
174 	CAL_GREGORIAN = 0,
175 	CAL_JULIAN,
176 	CAL_JEWISH,
177 	CAL_FRENCH,
178 	CAL_NUM_CALS
179 };
180 
181 typedef zend_long (*cal_to_jd_func_t) (int month, int day, int year);
182 typedef void (*cal_from_jd_func_t) (zend_long jd, int *year, int *month, int *day);
183 typedef char *(*cal_as_string_func_t) (int year, int month, int day);
184 
185 struct cal_entry_t {
186 	char *name;
187 	char *symbol;
188 	cal_to_jd_func_t to_jd;
189 	cal_from_jd_func_t from_jd;
190 	int num_months;
191 	int max_days_in_month;
192 	char **month_name_short;
193 	char **month_name_long;
194 };
195 
196 static struct cal_entry_t cal_conversion_table[CAL_NUM_CALS] = {
197 	{"Gregorian", "CAL_GREGORIAN", GregorianToSdn, SdnToGregorian, 12, 31,
198 	 MonthNameShort, MonthNameLong},
199 	{"Julian", "CAL_JULIAN", JulianToSdn, SdnToJulian, 12, 31,
200 	 MonthNameShort, MonthNameLong},
201 	{"Jewish", "CAL_JEWISH", JewishToSdn, SdnToJewish, 13, 30,
202 	 JewishMonthNameLeap, JewishMonthNameLeap},
203 	{"French", "CAL_FRENCH", FrenchToSdn, SdnToFrench, 13, 30,
204 	 FrenchMonthName, FrenchMonthName}
205 };
206 
207 #define JEWISH_MONTH_NAME(year) 	((monthsPerYear[((year)-1) % 19] == 13)?JewishMonthNameLeap:JewishMonthName)
208 #define JEWISH_HEB_MONTH_NAME(year) ((monthsPerYear[((year)-1) % 19] == 13)?JewishMonthHebNameLeap:JewishMonthHebName)
209 
210 /* For jddayofweek */
211 enum { CAL_DOW_DAYNO, CAL_DOW_LONG, CAL_DOW_SHORT };
212 
213 /* For jdmonthname */
214 enum { CAL_MONTH_GREGORIAN_SHORT, CAL_MONTH_GREGORIAN_LONG,
215 	CAL_MONTH_JULIAN_SHORT, CAL_MONTH_JULIAN_LONG, CAL_MONTH_JEWISH,
216 	CAL_MONTH_FRENCH
217 };
218 
219 /* for heb_number_to_chars */
220 static char alef_bet[25] = "0����������������������";
221 
222 #define CAL_JEWISH_ADD_ALAFIM_GERESH 0x2
223 #define CAL_JEWISH_ADD_ALAFIM 0x4
224 #define CAL_JEWISH_ADD_GERESHAYIM 0x8
225 
PHP_MINIT_FUNCTION(calendar)226 PHP_MINIT_FUNCTION(calendar)
227 {
228 	REGISTER_LONG_CONSTANT("CAL_GREGORIAN", CAL_GREGORIAN, CONST_CS | CONST_PERSISTENT);
229 	REGISTER_LONG_CONSTANT("CAL_JULIAN", CAL_JULIAN, CONST_CS | CONST_PERSISTENT);
230 	REGISTER_LONG_CONSTANT("CAL_JEWISH", CAL_JEWISH, CONST_CS | CONST_PERSISTENT);
231 	REGISTER_LONG_CONSTANT("CAL_FRENCH", CAL_FRENCH, CONST_CS | CONST_PERSISTENT);
232 	REGISTER_LONG_CONSTANT("CAL_NUM_CALS", CAL_NUM_CALS, CONST_CS | CONST_PERSISTENT);
233 /* constants for jddayofweek */
234 	REGISTER_LONG_CONSTANT("CAL_DOW_DAYNO", CAL_DOW_DAYNO, CONST_CS | CONST_PERSISTENT);
235 	REGISTER_LONG_CONSTANT("CAL_DOW_SHORT", CAL_DOW_SHORT, CONST_CS | CONST_PERSISTENT);
236 	REGISTER_LONG_CONSTANT("CAL_DOW_LONG", CAL_DOW_LONG, CONST_CS | CONST_PERSISTENT);
237 /* constants for jdmonthname */
238 	REGISTER_LONG_CONSTANT("CAL_MONTH_GREGORIAN_SHORT", CAL_MONTH_GREGORIAN_SHORT, CONST_CS | CONST_PERSISTENT);
239 	REGISTER_LONG_CONSTANT("CAL_MONTH_GREGORIAN_LONG", CAL_MONTH_GREGORIAN_LONG, CONST_CS | CONST_PERSISTENT);
240 	REGISTER_LONG_CONSTANT("CAL_MONTH_JULIAN_SHORT", CAL_MONTH_JULIAN_SHORT, CONST_CS | CONST_PERSISTENT);
241 	REGISTER_LONG_CONSTANT("CAL_MONTH_JULIAN_LONG", CAL_MONTH_JULIAN_LONG, CONST_CS | CONST_PERSISTENT);
242 	REGISTER_LONG_CONSTANT("CAL_MONTH_JEWISH", CAL_MONTH_JEWISH, CONST_CS | CONST_PERSISTENT);
243 	REGISTER_LONG_CONSTANT("CAL_MONTH_FRENCH", CAL_MONTH_FRENCH, CONST_CS | CONST_PERSISTENT);
244 /* constants for easter calculation */
245 	REGISTER_LONG_CONSTANT("CAL_EASTER_DEFAULT", CAL_EASTER_DEFAULT, CONST_CS | CONST_PERSISTENT);
246 	REGISTER_LONG_CONSTANT("CAL_EASTER_ROMAN", CAL_EASTER_ROMAN, CONST_CS | CONST_PERSISTENT);
247 	REGISTER_LONG_CONSTANT("CAL_EASTER_ALWAYS_GREGORIAN", CAL_EASTER_ALWAYS_GREGORIAN, CONST_CS | CONST_PERSISTENT);
248 	REGISTER_LONG_CONSTANT("CAL_EASTER_ALWAYS_JULIAN", CAL_EASTER_ALWAYS_JULIAN, CONST_CS | CONST_PERSISTENT);
249 /* constants for Jewish date formatting */
250 	REGISTER_LONG_CONSTANT("CAL_JEWISH_ADD_ALAFIM_GERESH", CAL_JEWISH_ADD_ALAFIM_GERESH, CONST_CS | CONST_PERSISTENT);
251 	REGISTER_LONG_CONSTANT("CAL_JEWISH_ADD_ALAFIM", CAL_JEWISH_ADD_ALAFIM, CONST_CS | CONST_PERSISTENT);
252 	REGISTER_LONG_CONSTANT("CAL_JEWISH_ADD_GERESHAYIM", CAL_JEWISH_ADD_GERESHAYIM, CONST_CS | CONST_PERSISTENT);
253 	return SUCCESS;
254 }
255 
PHP_MINFO_FUNCTION(calendar)256 PHP_MINFO_FUNCTION(calendar)
257 {
258 	php_info_print_table_start();
259 	php_info_print_table_row(2, "Calendar support", "enabled");
260 	php_info_print_table_end();
261 }
262 
_php_cal_info(int cal,zval * ret)263 static void _php_cal_info(int cal, zval *ret)
264 {
265 	zval months, smonths;
266 	int i;
267 	struct cal_entry_t *calendar;
268 
269 	calendar = &cal_conversion_table[cal];
270 	array_init(ret);
271 
272 	array_init(&months);
273 	array_init(&smonths);
274 
275 	for (i = 1; i <= calendar->num_months; i++) {
276 		add_index_string(&months, i, calendar->month_name_long[i]);
277 		add_index_string(&smonths, i, calendar->month_name_short[i]);
278 	}
279 
280 	add_assoc_zval(ret, "months", &months);
281 	add_assoc_zval(ret, "abbrevmonths", &smonths);
282 	add_assoc_long(ret, "maxdaysinmonth", calendar->max_days_in_month);
283 	add_assoc_string(ret, "calname", calendar->name);
284 	add_assoc_string(ret, "calsymbol", calendar->symbol);
285 
286 }
287 
288 /* {{{ proto array cal_info([int calendar])
289    Returns information about a particular calendar */
PHP_FUNCTION(cal_info)290 PHP_FUNCTION(cal_info)
291 {
292 	zend_long cal = -1;
293 
294 
295 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &cal) == FAILURE) {
296 		RETURN_FALSE;
297 	}
298 
299 	if (cal == -1) {
300 		int i;
301 		zval val;
302 
303 		array_init(return_value);
304 
305 		for (i = 0; i < CAL_NUM_CALS; i++) {
306 			_php_cal_info(i, &val);
307 			add_index_zval(return_value, i, &val);
308 		}
309 		return;
310 	}
311 
312 
313 	if (cal != -1 && (cal < 0 || cal >= CAL_NUM_CALS)) {
314 		php_error_docref(NULL, E_WARNING, "invalid calendar ID %pd.", cal);
315 		RETURN_FALSE;
316 	}
317 
318 	_php_cal_info(cal, return_value);
319 
320 }
321 /* }}} */
322 
323 /* {{{ proto int cal_days_in_month(int calendar, int month, int year)
324    Returns the number of days in a month for a given year and calendar */
PHP_FUNCTION(cal_days_in_month)325 PHP_FUNCTION(cal_days_in_month)
326 {
327 	zend_long cal, month, year;
328 	struct cal_entry_t *calendar;
329 	zend_long sdn_start, sdn_next;
330 
331 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &cal, &month, &year) == FAILURE) {
332 		RETURN_FALSE;
333 	}
334 
335 	if (cal < 0 || cal >= CAL_NUM_CALS) {
336 		php_error_docref(NULL, E_WARNING, "invalid calendar ID %pd.", cal);
337 		RETURN_FALSE;
338 	}
339 
340 	calendar = &cal_conversion_table[cal];
341 
342 	sdn_start = calendar->to_jd(year, month, 1);
343 
344 	if (sdn_start == 0) {
345 		php_error_docref(NULL, E_WARNING, "invalid date.");
346 		RETURN_FALSE;
347 	}
348 
349 	sdn_next = calendar->to_jd(year, 1 + month, 1);
350 
351 	if (sdn_next == 0) {
352 		/* If the next month is invalid, then we need to try the first month of
353 		 * the next year, bearing in mind that the next year after 1 BCE is
354 		 * actually 1 AD and not 0. */
355 		if (year == -1) {
356 			sdn_next = calendar->to_jd(1, 1, 1);
357 		}
358 		else {
359 			sdn_next = calendar->to_jd(year + 1, 1, 1);
360 			if (cal == CAL_FRENCH && sdn_next == 0) {
361 				/* The French calendar ends on 0014-13-05. */
362 				sdn_next = 2380953;
363 			}
364 		}
365 	}
366 
367 	RETURN_LONG(sdn_next - sdn_start);
368 }
369 /* }}} */
370 
371 /* {{{ proto int cal_to_jd(int calendar, int month, int day, int year)
372    Converts from a supported calendar to Julian Day Count */
PHP_FUNCTION(cal_to_jd)373 PHP_FUNCTION(cal_to_jd)
374 {
375 	zend_long cal, month, day, year;
376 
377 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "llll", &cal, &month, &day, &year) != SUCCESS) {
378 		RETURN_FALSE;
379 	}
380 
381 	if (cal < 0 || cal >= CAL_NUM_CALS) {
382 		php_error_docref(NULL, E_WARNING, "invalid calendar ID %pd.", cal);
383 		RETURN_FALSE;
384 	}
385 
386 	RETURN_LONG(cal_conversion_table[cal].to_jd(year, month, day));
387 }
388 /* }}} */
389 
390 /* {{{ proto array cal_from_jd(int jd, int calendar)
391    Converts from Julian Day Count to a supported calendar and return extended information */
PHP_FUNCTION(cal_from_jd)392 PHP_FUNCTION(cal_from_jd)
393 {
394 	zend_long jd, cal;
395 	int month, day, year, dow;
396 	char date[16];
397 	struct cal_entry_t *calendar;
398 
399 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &jd, &cal) == FAILURE) {
400 		RETURN_FALSE;
401 	}
402 
403 	if (cal < 0 || cal >= CAL_NUM_CALS) {
404 		php_error_docref(NULL, E_WARNING, "invalid calendar ID %pd", cal);
405 		RETURN_FALSE;
406 	}
407 	calendar = &cal_conversion_table[cal];
408 
409 	array_init(return_value);
410 
411 	calendar->from_jd(jd, &year, &month, &day);
412 
413 	snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
414 	add_assoc_string(return_value, "date", date);
415 
416 	add_assoc_long(return_value, "month", month);
417 	add_assoc_long(return_value, "day", day);
418 	add_assoc_long(return_value, "year", year);
419 
420 /* day of week */
421 	if (cal != CAL_JEWISH || year > 0) {
422 		dow = DayOfWeek(jd);
423 		add_assoc_long(return_value, "dow", dow);
424 		add_assoc_string(return_value, "abbrevdayname", DayNameShort[dow]);
425 		add_assoc_string(return_value, "dayname", DayNameLong[dow]);
426 	} else {
427 		add_assoc_null(return_value, "dow");
428 		add_assoc_string(return_value, "abbrevdayname", "");
429 		add_assoc_string(return_value, "dayname", "");
430 	}
431 /* month name */
432 	if(cal == CAL_JEWISH) {
433 		/* special case for Jewish calendar */
434 		add_assoc_string(return_value, "abbrevmonth", (year > 0 ? JEWISH_MONTH_NAME(year)[month] : ""));
435 		add_assoc_string(return_value, "monthname", (year > 0 ? JEWISH_MONTH_NAME(year)[month] : ""));
436 	} else {
437 		add_assoc_string(return_value, "abbrevmonth", calendar->month_name_short[month]);
438 		add_assoc_string(return_value, "monthname", calendar->month_name_long[month]);
439 	}
440 }
441 /* }}} */
442 
443 /* {{{ proto string jdtogregorian(int juliandaycount)
444    Converts a julian day count to a gregorian calendar date */
PHP_FUNCTION(jdtogregorian)445 PHP_FUNCTION(jdtogregorian)
446 {
447 	zend_long julday;
448 	int year, month, day;
449 	char date[16];
450 
451 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &julday) == FAILURE) {
452 		RETURN_FALSE;
453 	}
454 
455 	SdnToGregorian(julday, &year, &month, &day);
456 	snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
457 
458 	RETURN_STRING(date);
459 }
460 /* }}} */
461 
462 /* {{{ proto int gregoriantojd(int month, int day, int year)
463    Converts a gregorian calendar date to julian day count */
PHP_FUNCTION(gregoriantojd)464 PHP_FUNCTION(gregoriantojd)
465 {
466 	zend_long year, month, day;
467 
468 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &month, &day, &year) == FAILURE) {
469 		RETURN_FALSE;
470 	}
471 
472 	RETURN_LONG(GregorianToSdn(year, month, day));
473 }
474 /* }}} */
475 
476 /* {{{ proto string jdtojulian(int juliandaycount)
477    Convert a julian day count to a julian calendar date */
PHP_FUNCTION(jdtojulian)478 PHP_FUNCTION(jdtojulian)
479 {
480 	zend_long julday;
481 	int year, month, day;
482 	char date[16];
483 
484 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &julday) == FAILURE) {
485 		RETURN_FALSE;
486 	}
487 
488 	SdnToJulian(julday, &year, &month, &day);
489 	snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
490 
491 	RETURN_STRING(date);
492 }
493 /* }}} */
494 
495 /* {{{ proto int juliantojd(int month, int day, int year)
496    Converts a julian calendar date to julian day count */
PHP_FUNCTION(juliantojd)497 PHP_FUNCTION(juliantojd)
498 {
499 	zend_long year, month, day;
500 
501 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &month, &day, &year) == FAILURE) {
502 		RETURN_FALSE;
503 	}
504 
505 	RETURN_LONG(JulianToSdn(year, month, day));
506 }
507 /* }}} */
508 
509 /* {{{ heb_number_to_chars*/
510 /*
511 caution: the Hebrew format produces non unique result.
512 for example both: year '5' and year '5000' produce '�'.
513 use the numeric one for calculations.
514  */
heb_number_to_chars(int n,int fl,char ** ret)515 static char *heb_number_to_chars(int n, int fl, char **ret)
516 {
517 	char *p, old[18], *endofalafim;
518 
519 	p = endofalafim = old;
520 /*
521    prevents the option breaking the jewish beliefs, and some other
522    critical resources ;)
523  */
524 	if (n > 9999 || n < 1) {
525 		*ret = NULL;
526 		return NULL;
527 	}
528 
529 /* alafim (thousands) case */
530 	if (n / 1000) {
531 		*p = alef_bet[n / 1000];
532 		p++;
533 
534 		if (CAL_JEWISH_ADD_ALAFIM_GERESH & fl) {
535 			*p = '\'';
536 			p++;
537 		}
538 		if (CAL_JEWISH_ADD_ALAFIM & fl) {
539 			strcpy(p, " ����� ");
540 			p += 7;
541 		}
542 
543 		endofalafim = p;
544 		n = n % 1000;
545 	}
546 
547 /* tav-tav (tav=400) case */
548 	while (n >= 400) {
549 		*p = alef_bet[22];
550 		p++;
551 		n -= 400;
552 	}
553 
554 /* meot (hundreads) case */
555 	if (n >= 100) {
556 		*p = alef_bet[18 + n / 100];
557 		p++;
558 		n = n % 100;
559 	}
560 
561 /* tet-vav & tet-zain case (special case for 15 and 16) */
562 	if (n == 15 || n == 16) {
563 		*p = alef_bet[9];
564 		p++;
565 		*p = alef_bet[n - 9];
566 		p++;
567 	} else {
568 /* asarot (tens) case */
569 		if (n >= 10) {
570 			*p = alef_bet[9 + n / 10];
571 			p++;
572 			n = n % 10;
573 		}
574 
575 /* yehidot (ones) case */
576 		if (n > 0) {
577 			*p = alef_bet[n];
578 			p++;
579 		}
580 	}
581 
582 	if (CAL_JEWISH_ADD_GERESHAYIM & fl) {
583 		switch (p - endofalafim) {
584 		case 0:
585 			break;
586 		case 1:
587 			*p = '\'';
588 			p++;
589 			break;
590 		default:
591 			*(p) = *(p - 1);
592 			*(p - 1) = '"';
593 			p++;
594 		}
595 	}
596 
597 	*p = '\0';
598 	*ret = estrndup(old, (p - old) + 1);
599 	p = *ret;
600 	return p;
601 }
602 /* }}} */
603 
604 /* {{{ proto string jdtojewish(int juliandaycount [, bool hebrew [, int fl]])
605    Converts a julian day count to a jewish calendar date */
PHP_FUNCTION(jdtojewish)606 PHP_FUNCTION(jdtojewish)
607 {
608 	zend_long julday, fl = 0;
609 	zend_bool heb   = 0;
610 	int year, month, day;
611 	char date[16], hebdate[32];
612 	char *dayp, *yearp;
613 
614 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|bl", &julday, &heb, &fl) == FAILURE) {
615 		RETURN_FALSE;
616 	}
617 
618 	SdnToJewish(julday, &year, &month, &day);
619 	if (!heb) {
620 		snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
621 		RETURN_STRING(date);
622 	} else {
623 		if (year <= 0 || year > 9999) {
624 			php_error_docref(NULL, E_WARNING, "Year out of range (0-9999).");
625 			RETURN_FALSE;
626 		}
627 
628 		snprintf(hebdate, sizeof(hebdate), "%s %s %s", heb_number_to_chars(day, fl, &dayp), JEWISH_HEB_MONTH_NAME(year)[month], heb_number_to_chars(year, fl, &yearp));
629 
630 		if (dayp) {
631 			efree(dayp);
632 		}
633 		if (yearp) {
634 			efree(yearp);
635 		}
636 
637 		RETURN_STRING(hebdate);
638 
639 	}
640 }
641 /* }}} */
642 
643 /* {{{ proto int jewishtojd(int month, int day, int year)
644    Converts a jewish calendar date to a julian day count */
PHP_FUNCTION(jewishtojd)645 PHP_FUNCTION(jewishtojd)
646 {
647 	zend_long year, month, day;
648 
649 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &month, &day, &year) == FAILURE) {
650 		RETURN_FALSE;
651 	}
652 
653 	RETURN_LONG(JewishToSdn(year, month, day));
654 }
655 /* }}} */
656 
657 /* {{{ proto string jdtofrench(int juliandaycount)
658    Converts a julian day count to a french republic calendar date */
PHP_FUNCTION(jdtofrench)659 PHP_FUNCTION(jdtofrench)
660 {
661 	zend_long julday;
662 	int year, month, day;
663 	char date[16];
664 
665 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &julday) == FAILURE) {
666 		RETURN_FALSE;
667 	}
668 
669 	SdnToFrench(julday, &year, &month, &day);
670 	snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
671 
672 	RETURN_STRING(date);
673 }
674 /* }}} */
675 
676 /* {{{ proto int frenchtojd(int month, int day, int year)
677    Converts a french republic calendar date to julian day count */
PHP_FUNCTION(frenchtojd)678 PHP_FUNCTION(frenchtojd)
679 {
680 	zend_long year, month, day;
681 
682 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &month, &day, &year) == FAILURE) {
683 		RETURN_FALSE;
684 	}
685 
686 	RETURN_LONG(FrenchToSdn(year, month, day));
687 }
688 /* }}} */
689 
690 /* {{{ proto mixed jddayofweek(int juliandaycount [, int mode])
691    Returns name or number of day of week from julian day count */
PHP_FUNCTION(jddayofweek)692 PHP_FUNCTION(jddayofweek)
693 {
694 	zend_long julday, mode = CAL_DOW_DAYNO;
695 	int day;
696 	char *daynamel, *daynames;
697 
698 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &julday, &mode) == FAILURE) {
699 		RETURN_FALSE;
700 	}
701 
702 	day = DayOfWeek(julday);
703 	daynamel = DayNameLong[day];
704 	daynames = DayNameShort[day];
705 
706 	switch (mode) {
707 	case CAL_DOW_LONG:
708 		RETURN_STRING(daynamel);
709 		break;
710 	case CAL_DOW_SHORT:
711 		RETURN_STRING(daynames);
712 		break;
713 	case CAL_DOW_DAYNO:
714 	default:
715 		RETURN_LONG(day);
716 		break;
717 	}
718 }
719 /* }}} */
720 
721 /* {{{ proto string jdmonthname(int juliandaycount, int mode)
722    Returns name of month for julian day count */
PHP_FUNCTION(jdmonthname)723 PHP_FUNCTION(jdmonthname)
724 {
725 	zend_long julday, mode;
726 	char *monthname = NULL;
727 	int month, day, year;
728 
729 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &julday, &mode) == FAILURE) {
730 		RETURN_FALSE;
731 	}
732 
733 	switch (mode) {
734 	case CAL_MONTH_GREGORIAN_LONG:	/* gregorian or julian month */
735 		SdnToGregorian(julday, &year, &month, &day);
736 		monthname = MonthNameLong[month];
737 		break;
738 	case CAL_MONTH_JULIAN_SHORT:	/* gregorian or julian month */
739 		SdnToJulian(julday, &year, &month, &day);
740 		monthname = MonthNameShort[month];
741 		break;
742 	case CAL_MONTH_JULIAN_LONG:	/* gregorian or julian month */
743 		SdnToJulian(julday, &year, &month, &day);
744 		monthname = MonthNameLong[month];
745 		break;
746 	case CAL_MONTH_JEWISH:		/* jewish month */
747 		SdnToJewish(julday, &year, &month, &day);
748 		monthname = (year > 0 ? JEWISH_MONTH_NAME(year)[month] : "");
749 		break;
750 	case CAL_MONTH_FRENCH:		/* french month */
751 		SdnToFrench(julday, &year, &month, &day);
752 		monthname = FrenchMonthName[month];
753 		break;
754 	default:					/* default gregorian */
755 	case CAL_MONTH_GREGORIAN_SHORT:	/* gregorian or julian month */
756 		SdnToGregorian(julday, &year, &month, &day);
757 		monthname = MonthNameShort[month];
758 		break;
759 	}
760 
761 	RETURN_STRING(monthname);
762 }
763 /* }}} */
764 
765 /*
766  * Local variables:
767  * tab-width: 4
768  * c-basic-offset: 4
769  * End:
770  * vim600: sw=4 ts=4 fdm=marker
771  * vim<600: sw=4 ts=4
772  */
773