calendar.c 21 KB

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