calendar_methods.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. +----------------------------------------------------------------------+
  3. | This source file is subject to version 3.01 of the PHP license, |
  4. | that is bundled with this package in the file LICENSE, and is |
  5. | available through the world-wide-web at the following url: |
  6. | https://www.php.net/license/3_01.txt |
  7. | If you did not receive a copy of the PHP license and are unable to |
  8. | obtain it through the world-wide-web, please send a note to |
  9. | license@php.net so we can mail you a copy immediately. |
  10. +----------------------------------------------------------------------+
  11. | Authors: Gustavo Lopes <cataphract@php.net> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include <inttypes.h>
  18. #include "../intl_cppshims.h"
  19. #include <unicode/locid.h>
  20. #include <unicode/calendar.h>
  21. #include <unicode/ustring.h>
  22. #include "../intl_convertcpp.h"
  23. #include "../common/common_date.h"
  24. extern "C" {
  25. #include "../php_intl.h"
  26. #define USE_TIMEZONE_POINTER 1
  27. #include "../timezone/timezone_class.h"
  28. #define USE_CALENDAR_POINTER 1
  29. #include "calendar_class.h"
  30. #include "../intl_convert.h"
  31. #include <zend_exceptions.h>
  32. #include <zend_interfaces.h>
  33. #include <ext/date/php_date.h>
  34. }
  35. #include "../common/common_enum.h"
  36. using icu::Locale;
  37. #define ZEND_VALUE_ERROR_INVALID_FIELD(argument, zpp_arg_position) \
  38. if (argument < 0 || argument >= UCAL_FIELD_COUNT) { \
  39. zend_argument_value_error(getThis() ? ((zpp_arg_position)-1) : zpp_arg_position, \
  40. "must be a valid field"); \
  41. RETURN_THROWS(); \
  42. }
  43. #define ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(argument, zpp_arg_position) \
  44. if (argument < INT32_MIN || argument > INT32_MAX) { \
  45. zend_argument_value_error(getThis() ? ((zpp_arg_position)-1) : zpp_arg_position, \
  46. "must be between %d and %d", INT32_MIN, INT32_MAX); \
  47. RETURN_THROWS(); \
  48. }
  49. #define ZEND_VALUE_ERROR_INVALID_DAY_OF_WEEK(argument, zpp_arg_position) \
  50. if (argument < UCAL_SUNDAY || argument > UCAL_SATURDAY) { \
  51. zend_argument_value_error(getThis() ? ((zpp_arg_position)-1) : zpp_arg_position, \
  52. "must be a valid day of the week"); \
  53. RETURN_THROWS(); \
  54. }
  55. U_CFUNC PHP_METHOD(IntlCalendar, __construct)
  56. {
  57. zend_throw_exception( NULL,
  58. "An object of this type cannot be created with the new operator",
  59. 0 );
  60. }
  61. U_CFUNC PHP_FUNCTION(intlcal_create_instance)
  62. {
  63. zval *zv_timezone = NULL;
  64. const char *locale_str = NULL;
  65. size_t dummy;
  66. TimeZone *timeZone;
  67. UErrorCode status = U_ZERO_ERROR;
  68. intl_error_reset(NULL);
  69. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|zs!",
  70. &zv_timezone, &locale_str, &dummy) == FAILURE) {
  71. RETURN_THROWS();
  72. }
  73. timeZone = timezone_process_timezone_argument(zv_timezone, NULL,
  74. "intlcal_create_instance");
  75. if (timeZone == NULL) {
  76. RETURN_NULL();
  77. }
  78. if (!locale_str) {
  79. locale_str = intl_locale_get_default();
  80. }
  81. Calendar *cal = Calendar::createInstance(timeZone,
  82. Locale::createFromName(locale_str), status);
  83. if (cal == NULL) {
  84. delete timeZone;
  85. intl_error_set(NULL, status, "Error creating ICU Calendar object", 0);
  86. RETURN_NULL();
  87. }
  88. calendar_object_create(return_value, cal);
  89. }
  90. class BugStringCharEnumeration : public StringEnumeration
  91. {
  92. public:
  93. explicit BugStringCharEnumeration(UEnumeration* _uenum) : uenum(_uenum) {}
  94. ~BugStringCharEnumeration()
  95. {
  96. uenum_close(uenum);
  97. }
  98. int32_t count(UErrorCode& status) const {
  99. return uenum_count(uenum, &status);
  100. }
  101. virtual const UnicodeString* snext(UErrorCode& status)
  102. {
  103. int32_t length;
  104. const UChar* str = uenum_unext(uenum, &length, &status);
  105. if (str == 0 || U_FAILURE(status)) {
  106. return 0;
  107. }
  108. return &unistr.setTo(str, length);
  109. }
  110. virtual const char* next(int32_t *resultLength, UErrorCode &status)
  111. {
  112. int32_t length = -1;
  113. const char* str = uenum_next(uenum, &length, &status);
  114. if (str == 0 || U_FAILURE(status)) {
  115. return 0;
  116. }
  117. if (resultLength) {
  118. //the bug is that uenum_next doesn't set the length
  119. *resultLength = (length == -1) ? (int32_t)strlen(str) : length;
  120. }
  121. return str;
  122. }
  123. void reset(UErrorCode& status)
  124. {
  125. uenum_reset(uenum, &status);
  126. }
  127. virtual UClassID getDynamicClassID() const;
  128. static UClassID U_EXPORT2 getStaticClassID();
  129. private:
  130. UEnumeration *uenum;
  131. };
  132. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BugStringCharEnumeration)
  133. U_CFUNC PHP_FUNCTION(intlcal_get_keyword_values_for_locale)
  134. {
  135. UErrorCode status = U_ZERO_ERROR;
  136. char *key,
  137. *locale;
  138. size_t key_len,
  139. locale_len;
  140. bool commonly_used;
  141. intl_error_reset(NULL);
  142. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssb",
  143. &key, &key_len, &locale, &locale_len, &commonly_used) == FAILURE) {
  144. RETURN_THROWS();
  145. }
  146. StringEnumeration *se = Calendar::getKeywordValuesForLocale(key,
  147. Locale::createFromName(locale), (UBool)commonly_used,
  148. status);
  149. if (se == NULL) {
  150. intl_error_set(NULL, status, "intlcal_get_keyword_values_for_locale: "
  151. "error calling underlying method", 0);
  152. RETURN_FALSE;
  153. }
  154. IntlIterator_from_StringEnumeration(se, return_value);
  155. }
  156. U_CFUNC PHP_FUNCTION(intlcal_get_now)
  157. {
  158. intl_error_reset(NULL);
  159. if (zend_parse_parameters_none() == FAILURE) {
  160. RETURN_THROWS();
  161. }
  162. RETURN_DOUBLE((double)Calendar::getNow());
  163. }
  164. U_CFUNC PHP_FUNCTION(intlcal_get_available_locales)
  165. {
  166. intl_error_reset(NULL);
  167. if (zend_parse_parameters_none() == FAILURE) {
  168. RETURN_THROWS();
  169. }
  170. int32_t count;
  171. const Locale *availLocales = Calendar::getAvailableLocales(count);
  172. array_init(return_value);
  173. for (int i = 0; i < count; i++) {
  174. Locale locale = availLocales[i];
  175. add_next_index_string(return_value, locale.getName());
  176. }
  177. }
  178. static void _php_intlcal_field_uec_ret_in32t_method(
  179. int32_t (Calendar::*func)(UCalendarDateFields, UErrorCode&) const,
  180. INTERNAL_FUNCTION_PARAMETERS)
  181. {
  182. zend_long field;
  183. CALENDAR_METHOD_INIT_VARS;
  184. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  185. "Ol", &object, Calendar_ce_ptr, &field) == FAILURE) {
  186. RETURN_THROWS();
  187. }
  188. ZEND_VALUE_ERROR_INVALID_FIELD(field, 2);
  189. CALENDAR_METHOD_FETCH_OBJECT;
  190. int32_t result = (co->ucal->*func)(
  191. (UCalendarDateFields)field, CALENDAR_ERROR_CODE(co));
  192. INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed");
  193. RETURN_LONG((zend_long)result);
  194. }
  195. U_CFUNC PHP_FUNCTION(intlcal_get)
  196. {
  197. _php_intlcal_field_uec_ret_in32t_method(&Calendar::get,
  198. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  199. }
  200. U_CFUNC PHP_FUNCTION(intlcal_get_time)
  201. {
  202. CALENDAR_METHOD_INIT_VARS;
  203. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  204. &object, Calendar_ce_ptr) == FAILURE) {
  205. RETURN_THROWS();
  206. }
  207. CALENDAR_METHOD_FETCH_OBJECT;
  208. UDate result = co->ucal->getTime(CALENDAR_ERROR_CODE(co));
  209. INTL_METHOD_CHECK_STATUS(co,
  210. "intlcal_get_time: error calling ICU Calendar::getTime");
  211. RETURN_DOUBLE((double)result);
  212. }
  213. U_CFUNC PHP_FUNCTION(intlcal_set_time)
  214. {
  215. double time_arg;
  216. CALENDAR_METHOD_INIT_VARS;
  217. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Od",
  218. &object, Calendar_ce_ptr, &time_arg) == FAILURE) {
  219. RETURN_THROWS();
  220. }
  221. CALENDAR_METHOD_FETCH_OBJECT;
  222. co->ucal->setTime((UDate)time_arg, CALENDAR_ERROR_CODE(co));
  223. INTL_METHOD_CHECK_STATUS(co, "Call to underlying method failed");
  224. RETURN_TRUE;
  225. }
  226. U_CFUNC PHP_FUNCTION(intlcal_add)
  227. {
  228. zend_long field,
  229. amount;
  230. CALENDAR_METHOD_INIT_VARS;
  231. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  232. "Oll", &object, Calendar_ce_ptr, &field, &amount) == FAILURE) {
  233. RETURN_THROWS();
  234. }
  235. ZEND_VALUE_ERROR_INVALID_FIELD(field, 2);
  236. ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(amount, 3);
  237. CALENDAR_METHOD_FETCH_OBJECT;
  238. co->ucal->add((UCalendarDateFields)field, (int32_t)amount, CALENDAR_ERROR_CODE(co));
  239. INTL_METHOD_CHECK_STATUS(co, "intlcal_add: Call to underlying method failed");
  240. RETURN_TRUE;
  241. }
  242. U_CFUNC PHP_FUNCTION(intlcal_set_time_zone)
  243. {
  244. zval *zv_timezone;
  245. TimeZone *timeZone;
  246. CALENDAR_METHOD_INIT_VARS;
  247. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  248. "Oz!", &object, Calendar_ce_ptr, &zv_timezone) == FAILURE) {
  249. RETURN_THROWS();
  250. }
  251. CALENDAR_METHOD_FETCH_OBJECT;
  252. if (zv_timezone == NULL) {
  253. RETURN_TRUE; /* the method does nothing if passed null */
  254. }
  255. timeZone = timezone_process_timezone_argument(zv_timezone,
  256. CALENDAR_ERROR_P(co), "intlcal_set_time_zone");
  257. if (timeZone == NULL) {
  258. RETURN_FALSE;
  259. }
  260. co->ucal->adoptTimeZone(timeZone);
  261. RETURN_TRUE;
  262. }
  263. static void _php_intlcal_before_after(
  264. UBool (Calendar::*func)(const Calendar&, UErrorCode&) const,
  265. INTERNAL_FUNCTION_PARAMETERS)
  266. {
  267. zval *when_object;
  268. Calendar_object *when_co;
  269. CALENDAR_METHOD_INIT_VARS;
  270. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  271. "OO", &object, Calendar_ce_ptr, &when_object, Calendar_ce_ptr)
  272. == FAILURE) {
  273. RETURN_THROWS();
  274. }
  275. CALENDAR_METHOD_FETCH_OBJECT;
  276. when_co = Z_INTL_CALENDAR_P(when_object);
  277. if (when_co->ucal == NULL) {
  278. zend_argument_error(NULL, 2, "is uninitialized");
  279. RETURN_THROWS();
  280. }
  281. UBool res = (co->ucal->*func)(*when_co->ucal, CALENDAR_ERROR_CODE(co));
  282. INTL_METHOD_CHECK_STATUS(co, "intlcal_before/after: Error calling ICU method");
  283. RETURN_BOOL((int)res);
  284. }
  285. U_CFUNC PHP_FUNCTION(intlcal_after)
  286. {
  287. _php_intlcal_before_after(&Calendar::after, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  288. }
  289. U_CFUNC PHP_FUNCTION(intlcal_before)
  290. {
  291. _php_intlcal_before_after(&Calendar::before, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  292. }
  293. U_CFUNC PHP_FUNCTION(intlcal_set)
  294. {
  295. zend_long args[6];
  296. CALENDAR_METHOD_INIT_VARS;
  297. object = getThis();
  298. int arg_num = ZEND_NUM_ARGS() - (object ? 0 : 1);
  299. if (zend_parse_method_parameters(
  300. ZEND_NUM_ARGS(), object, "Oll|llll",
  301. &object, Calendar_ce_ptr, &args[0], &args[1], &args[2], &args[3], &args[4], &args[5]
  302. ) == FAILURE) {
  303. RETURN_THROWS();
  304. }
  305. for (int i = 0; i < arg_num; i++) {
  306. /* Arguments start at 1 */
  307. ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(args[i], i + 1);
  308. }
  309. CALENDAR_METHOD_FETCH_OBJECT;
  310. if (arg_num == 2) {
  311. ZEND_VALUE_ERROR_INVALID_FIELD(args[0], 2);
  312. co->ucal->set((UCalendarDateFields)args[0], (int32_t)args[1]);
  313. } else if (arg_num == 3) {
  314. co->ucal->set((int32_t)args[0], (int32_t)args[1], (int32_t)args[2]);
  315. } else if (arg_num == 4) {
  316. zend_argument_count_error("IntlCalendar::set() has no variant with exactly 4 parameters");
  317. RETURN_THROWS();
  318. } else if (arg_num == 5) {
  319. co->ucal->set((int32_t)args[0], (int32_t)args[1], (int32_t)args[2], (int32_t)args[3], (int32_t)args[4]);
  320. } else {
  321. co->ucal->set((int32_t)args[0], (int32_t)args[1], (int32_t)args[2], (int32_t)args[3], (int32_t)args[4], (int32_t)args[5]);
  322. }
  323. RETURN_TRUE;
  324. }
  325. U_CFUNC PHP_FUNCTION(intlcal_roll)
  326. {
  327. zval *zvalue;
  328. zend_long field, value;
  329. CALENDAR_METHOD_INIT_VARS;
  330. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olz", &object, Calendar_ce_ptr, &field, &zvalue) == FAILURE) {
  331. RETURN_THROWS();
  332. }
  333. CALENDAR_METHOD_FETCH_OBJECT;
  334. ZEND_VALUE_ERROR_INVALID_FIELD(field, 2);
  335. if (Z_TYPE_P(zvalue) == IS_FALSE || Z_TYPE_P(zvalue) == IS_TRUE) {
  336. value = Z_TYPE_P(zvalue) == IS_TRUE ? 1 : -1;
  337. php_error_docref(NULL, E_DEPRECATED, "Passing bool is deprecated, use 1 or -1 instead");
  338. } else {
  339. value = zval_get_long(zvalue);
  340. ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(value, 3);
  341. }
  342. co->ucal->roll((UCalendarDateFields)field, (int32_t)value, CALENDAR_ERROR_CODE(co));
  343. INTL_METHOD_CHECK_STATUS(co, "intlcal_roll: Error calling ICU Calendar::roll");
  344. RETURN_TRUE;
  345. }
  346. U_CFUNC PHP_FUNCTION(intlcal_clear)
  347. {
  348. zend_long field;
  349. bool field_is_null = 1;
  350. CALENDAR_METHOD_INIT_VARS;
  351. if (zend_parse_method_parameters(ZEND_NUM_ARGS(),
  352. getThis(), "O|l!", &object, Calendar_ce_ptr, &field, &field_is_null) == FAILURE) {
  353. RETURN_THROWS();
  354. }
  355. CALENDAR_METHOD_FETCH_OBJECT;
  356. if (field_is_null) {
  357. co->ucal->clear();
  358. } else {
  359. ZEND_VALUE_ERROR_INVALID_FIELD(field, 2);
  360. co->ucal->clear((UCalendarDateFields)field);
  361. }
  362. RETURN_TRUE;
  363. }
  364. U_CFUNC PHP_FUNCTION(intlcal_field_difference)
  365. {
  366. zend_long field;
  367. double when;
  368. CALENDAR_METHOD_INIT_VARS;
  369. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  370. "Odl", &object, Calendar_ce_ptr, &when, &field) == FAILURE) {
  371. RETURN_THROWS();
  372. }
  373. ZEND_VALUE_ERROR_INVALID_FIELD(field, 3);
  374. CALENDAR_METHOD_FETCH_OBJECT;
  375. int32_t result = co->ucal->fieldDifference((UDate)when,
  376. (UCalendarDateFields)field, CALENDAR_ERROR_CODE(co));
  377. INTL_METHOD_CHECK_STATUS(co,
  378. "intlcal_field_difference: Call to ICU method has failed");
  379. RETURN_LONG((zend_long)result);
  380. }
  381. U_CFUNC PHP_FUNCTION(intlcal_get_actual_maximum)
  382. {
  383. _php_intlcal_field_uec_ret_in32t_method(&Calendar::getActualMaximum,
  384. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  385. }
  386. U_CFUNC PHP_FUNCTION(intlcal_get_actual_minimum)
  387. {
  388. _php_intlcal_field_uec_ret_in32t_method(&Calendar::getActualMinimum,
  389. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  390. }
  391. U_CFUNC PHP_FUNCTION(intlcal_get_day_of_week_type)
  392. {
  393. zend_long dow;
  394. CALENDAR_METHOD_INIT_VARS;
  395. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  396. "Ol", &object, Calendar_ce_ptr, &dow) == FAILURE) {
  397. RETURN_THROWS();
  398. }
  399. ZEND_VALUE_ERROR_INVALID_DAY_OF_WEEK(dow, 2);
  400. CALENDAR_METHOD_FETCH_OBJECT;
  401. int32_t result = co->ucal->getDayOfWeekType(
  402. (UCalendarDaysOfWeek)dow, CALENDAR_ERROR_CODE(co));
  403. INTL_METHOD_CHECK_STATUS(co,
  404. "intlcal_get_day_of_week_type: Call to ICU method has failed");
  405. RETURN_LONG((zend_long)result);
  406. }
  407. U_CFUNC PHP_FUNCTION(intlcal_get_first_day_of_week)
  408. {
  409. CALENDAR_METHOD_INIT_VARS;
  410. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  411. "O", &object, Calendar_ce_ptr) == FAILURE) {
  412. RETURN_THROWS();
  413. }
  414. CALENDAR_METHOD_FETCH_OBJECT;
  415. int32_t result = co->ucal->getFirstDayOfWeek(CALENDAR_ERROR_CODE(co));
  416. INTL_METHOD_CHECK_STATUS(co,
  417. "intlcal_get_first_day_of_week: Call to ICU method has failed");
  418. RETURN_LONG((zend_long)result);
  419. }
  420. static void _php_intlcal_field_ret_in32t_method(
  421. int32_t (Calendar::*func)(UCalendarDateFields) const,
  422. INTERNAL_FUNCTION_PARAMETERS)
  423. {
  424. zend_long field;
  425. CALENDAR_METHOD_INIT_VARS;
  426. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  427. "Ol", &object, Calendar_ce_ptr, &field) == FAILURE) {
  428. RETURN_THROWS();
  429. }
  430. ZEND_VALUE_ERROR_INVALID_FIELD(field, 2);
  431. CALENDAR_METHOD_FETCH_OBJECT;
  432. int32_t result = (co->ucal->*func)((UCalendarDateFields)field);
  433. INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed");
  434. RETURN_LONG((zend_long)result);
  435. }
  436. U_CFUNC PHP_FUNCTION(intlcal_get_greatest_minimum)
  437. {
  438. _php_intlcal_field_ret_in32t_method(&Calendar::getGreatestMinimum,
  439. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  440. }
  441. U_CFUNC PHP_FUNCTION(intlcal_get_least_maximum)
  442. {
  443. _php_intlcal_field_ret_in32t_method(&Calendar::getLeastMaximum,
  444. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  445. }
  446. U_CFUNC PHP_FUNCTION(intlcal_get_locale)
  447. {
  448. zend_long locale_type;
  449. CALENDAR_METHOD_INIT_VARS;
  450. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  451. "Ol", &object, Calendar_ce_ptr, &locale_type) == FAILURE) {
  452. RETURN_THROWS();
  453. }
  454. if (locale_type != ULOC_ACTUAL_LOCALE && locale_type != ULOC_VALID_LOCALE) {
  455. zend_argument_value_error(getThis() ? 1 : 2, "must be either Locale::ACTUAL_LOCALE or Locale::VALID_LOCALE");
  456. RETURN_THROWS();
  457. }
  458. CALENDAR_METHOD_FETCH_OBJECT;
  459. Locale locale = co->ucal->getLocale((ULocDataLocaleType)locale_type,
  460. CALENDAR_ERROR_CODE(co));
  461. INTL_METHOD_CHECK_STATUS(co,
  462. "intlcal_get_locale: Call to ICU method has failed");
  463. RETURN_STRING(locale.getName());
  464. }
  465. U_CFUNC PHP_FUNCTION(intlcal_get_maximum)
  466. {
  467. _php_intlcal_field_ret_in32t_method(&Calendar::getMaximum,
  468. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  469. }
  470. U_CFUNC PHP_FUNCTION(intlcal_get_minimal_days_in_first_week)
  471. {
  472. CALENDAR_METHOD_INIT_VARS;
  473. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  474. "O", &object, Calendar_ce_ptr) == FAILURE) {
  475. RETURN_THROWS();
  476. }
  477. CALENDAR_METHOD_FETCH_OBJECT;
  478. uint8_t result = co->ucal->getMinimalDaysInFirstWeek();
  479. INTL_METHOD_CHECK_STATUS(co,
  480. "intlcal_get_first_day_of_week: Call to ICU method has failed"); /* TODO Is it really a failure? */
  481. RETURN_LONG((zend_long)result);
  482. }
  483. U_CFUNC PHP_FUNCTION(intlcal_get_minimum)
  484. {
  485. _php_intlcal_field_ret_in32t_method(&Calendar::getMinimum,
  486. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  487. }
  488. U_CFUNC PHP_FUNCTION(intlcal_get_time_zone)
  489. {
  490. CALENDAR_METHOD_INIT_VARS;
  491. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  492. "O", &object, Calendar_ce_ptr) == FAILURE) {
  493. RETURN_THROWS();
  494. }
  495. CALENDAR_METHOD_FETCH_OBJECT;
  496. TimeZone *tz = co->ucal->getTimeZone().clone();
  497. if (tz == NULL) {
  498. intl_errors_set(CALENDAR_ERROR_P(co), U_MEMORY_ALLOCATION_ERROR,
  499. "intlcal_get_time_zone: could not clone TimeZone", 0);
  500. RETURN_FALSE;
  501. }
  502. timezone_object_construct(tz, return_value, 1);
  503. }
  504. U_CFUNC PHP_FUNCTION(intlcal_get_type)
  505. {
  506. CALENDAR_METHOD_INIT_VARS;
  507. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  508. "O", &object, Calendar_ce_ptr) == FAILURE) {
  509. RETURN_THROWS();
  510. }
  511. CALENDAR_METHOD_FETCH_OBJECT;
  512. RETURN_STRING(co->ucal->getType());
  513. }
  514. U_CFUNC PHP_FUNCTION(intlcal_get_weekend_transition)
  515. {
  516. zend_long dow;
  517. CALENDAR_METHOD_INIT_VARS;
  518. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  519. "Ol", &object, Calendar_ce_ptr, &dow) == FAILURE) {
  520. RETURN_THROWS();
  521. }
  522. ZEND_VALUE_ERROR_INVALID_DAY_OF_WEEK(dow, 2);
  523. CALENDAR_METHOD_FETCH_OBJECT;
  524. int32_t res = co->ucal->getWeekendTransition((UCalendarDaysOfWeek)dow,
  525. CALENDAR_ERROR_CODE(co));
  526. INTL_METHOD_CHECK_STATUS(co, "intlcal_get_weekend_transition: "
  527. "Error calling ICU method");
  528. RETURN_LONG((zend_long)res);
  529. }
  530. U_CFUNC PHP_FUNCTION(intlcal_in_daylight_time)
  531. {
  532. CALENDAR_METHOD_INIT_VARS;
  533. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  534. "O", &object, Calendar_ce_ptr) == FAILURE) {
  535. RETURN_THROWS();
  536. }
  537. CALENDAR_METHOD_FETCH_OBJECT;
  538. UBool ret = co->ucal->inDaylightTime(CALENDAR_ERROR_CODE(co));
  539. INTL_METHOD_CHECK_STATUS(co, "intlcal_in_daylight_time: "
  540. "Error calling ICU method");
  541. RETURN_BOOL((int)ret);
  542. }
  543. U_CFUNC PHP_FUNCTION(intlcal_is_equivalent_to)
  544. {
  545. zval *other_object;
  546. Calendar_object *other_co;
  547. CALENDAR_METHOD_INIT_VARS;
  548. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  549. "OO", &object, Calendar_ce_ptr, &other_object, Calendar_ce_ptr)
  550. == FAILURE) {
  551. RETURN_THROWS();
  552. }
  553. other_co = Z_INTL_CALENDAR_P(other_object);
  554. if (other_co->ucal == NULL) {
  555. zend_argument_error(NULL, 2, "is uninitialized");
  556. RETURN_THROWS();
  557. }
  558. CALENDAR_METHOD_FETCH_OBJECT;
  559. RETURN_BOOL((int)co->ucal->isEquivalentTo(*other_co->ucal));
  560. }
  561. U_CFUNC PHP_FUNCTION(intlcal_is_lenient)
  562. {
  563. CALENDAR_METHOD_INIT_VARS;
  564. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  565. "O", &object, Calendar_ce_ptr) == FAILURE) {
  566. RETURN_THROWS();
  567. }
  568. CALENDAR_METHOD_FETCH_OBJECT;
  569. RETURN_BOOL((int)co->ucal->isLenient());
  570. }
  571. U_CFUNC PHP_FUNCTION(intlcal_is_set)
  572. {
  573. zend_long field;
  574. CALENDAR_METHOD_INIT_VARS;
  575. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  576. "Ol", &object, Calendar_ce_ptr, &field) == FAILURE) {
  577. RETURN_THROWS();
  578. }
  579. ZEND_VALUE_ERROR_INVALID_FIELD(field, 2);
  580. CALENDAR_METHOD_FETCH_OBJECT;
  581. RETURN_BOOL((int)co->ucal->isSet((UCalendarDateFields)field));
  582. }
  583. U_CFUNC PHP_FUNCTION(intlcal_is_weekend)
  584. {
  585. double date;
  586. bool date_is_null = 1;
  587. CALENDAR_METHOD_INIT_VARS;
  588. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  589. "O|d!", &object, Calendar_ce_ptr, &date, &date_is_null) == FAILURE) {
  590. RETURN_THROWS();
  591. }
  592. CALENDAR_METHOD_FETCH_OBJECT;
  593. if (date_is_null) {
  594. RETURN_BOOL((int)co->ucal->isWeekend());
  595. } else {
  596. UBool ret = co->ucal->isWeekend((UDate)date, CALENDAR_ERROR_CODE(co));
  597. INTL_METHOD_CHECK_STATUS(co, "intlcal_is_weekend: "
  598. "Error calling ICU method");
  599. RETURN_BOOL((int)ret);
  600. }
  601. }
  602. U_CFUNC PHP_FUNCTION(intlcal_set_first_day_of_week)
  603. {
  604. zend_long dow;
  605. CALENDAR_METHOD_INIT_VARS;
  606. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  607. "Ol", &object, Calendar_ce_ptr, &dow) == FAILURE) {
  608. RETURN_THROWS();
  609. }
  610. ZEND_VALUE_ERROR_INVALID_DAY_OF_WEEK(dow, 2);
  611. CALENDAR_METHOD_FETCH_OBJECT;
  612. co->ucal->setFirstDayOfWeek((UCalendarDaysOfWeek)dow);
  613. RETURN_TRUE;
  614. }
  615. U_CFUNC PHP_FUNCTION(intlcal_set_lenient)
  616. {
  617. bool is_lenient;
  618. CALENDAR_METHOD_INIT_VARS;
  619. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  620. "Ob", &object, Calendar_ce_ptr, &is_lenient) == FAILURE) {
  621. RETURN_THROWS();
  622. }
  623. CALENDAR_METHOD_FETCH_OBJECT;
  624. co->ucal->setLenient((UBool) is_lenient);
  625. RETURN_TRUE;
  626. }
  627. U_CFUNC PHP_FUNCTION(intlcal_set_minimal_days_in_first_week)
  628. {
  629. zend_long num_days;
  630. CALENDAR_METHOD_INIT_VARS;
  631. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  632. "Ol", &object, Calendar_ce_ptr, &num_days) == FAILURE) {
  633. RETURN_THROWS();
  634. }
  635. // Use ZEND_VALUE_ERROR_INVALID_DAY_OF_WEEK ?
  636. if (num_days < 1 || num_days > 7) {
  637. zend_argument_value_error(getThis() ? 1 : 2, "must be between 1 and 7");
  638. RETURN_THROWS();
  639. }
  640. CALENDAR_METHOD_FETCH_OBJECT;
  641. co->ucal->setMinimalDaysInFirstWeek((uint8_t)num_days);
  642. RETURN_TRUE;
  643. }
  644. U_CFUNC PHP_FUNCTION(intlcal_equals)
  645. {
  646. zval *other_object;
  647. Calendar_object *other_co;
  648. CALENDAR_METHOD_INIT_VARS;
  649. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  650. "OO", &object, Calendar_ce_ptr, &other_object, Calendar_ce_ptr)
  651. == FAILURE) {
  652. RETURN_THROWS();
  653. }
  654. CALENDAR_METHOD_FETCH_OBJECT;
  655. other_co = Z_INTL_CALENDAR_P(other_object);
  656. if (other_co->ucal == NULL) {
  657. zend_argument_error(NULL, 2, "is uninitialized");
  658. RETURN_THROWS();
  659. }
  660. UBool result = co->ucal->equals(*other_co->ucal, CALENDAR_ERROR_CODE(co));
  661. INTL_METHOD_CHECK_STATUS(co, "intlcal_equals: error calling ICU Calendar::equals");
  662. RETURN_BOOL((int)result);
  663. }
  664. U_CFUNC PHP_FUNCTION(intlcal_get_repeated_wall_time_option)
  665. {
  666. CALENDAR_METHOD_INIT_VARS;
  667. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  668. "O", &object, Calendar_ce_ptr) == FAILURE) {
  669. RETURN_THROWS();
  670. }
  671. CALENDAR_METHOD_FETCH_OBJECT;
  672. RETURN_LONG(co->ucal->getRepeatedWallTimeOption());
  673. }
  674. U_CFUNC PHP_FUNCTION(intlcal_get_skipped_wall_time_option)
  675. {
  676. CALENDAR_METHOD_INIT_VARS;
  677. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  678. "O", &object, Calendar_ce_ptr) == FAILURE) {
  679. RETURN_THROWS();
  680. }
  681. CALENDAR_METHOD_FETCH_OBJECT;
  682. RETURN_LONG(co->ucal->getSkippedWallTimeOption());
  683. }
  684. U_CFUNC PHP_FUNCTION(intlcal_set_repeated_wall_time_option)
  685. {
  686. zend_long option;
  687. CALENDAR_METHOD_INIT_VARS;
  688. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  689. "Ol", &object, Calendar_ce_ptr, &option) == FAILURE) {
  690. RETURN_THROWS();
  691. }
  692. if (option != UCAL_WALLTIME_FIRST && option != UCAL_WALLTIME_LAST) {
  693. zend_argument_value_error(getThis() ? 1 : 2, "must be either IntlCalendar::WALLTIME_FIRST or "
  694. "IntlCalendar::WALLTIME_LAST");
  695. RETURN_THROWS();
  696. }
  697. CALENDAR_METHOD_FETCH_OBJECT;
  698. co->ucal->setRepeatedWallTimeOption((UCalendarWallTimeOption)option);
  699. RETURN_TRUE;
  700. }
  701. U_CFUNC PHP_FUNCTION(intlcal_set_skipped_wall_time_option)
  702. {
  703. zend_long option;
  704. CALENDAR_METHOD_INIT_VARS;
  705. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  706. "Ol", &object, Calendar_ce_ptr, &option) == FAILURE) {
  707. RETURN_THROWS();
  708. }
  709. if (option != UCAL_WALLTIME_FIRST && option != UCAL_WALLTIME_LAST
  710. && option != UCAL_WALLTIME_NEXT_VALID) {
  711. zend_argument_value_error(getThis() ? 1 : 2, "must be one of IntlCalendar::WALLTIME_FIRST, "
  712. "IntlCalendar::WALLTIME_LAST, or IntlCalendar::WALLTIME_NEXT_VALID");
  713. RETURN_THROWS();
  714. }
  715. CALENDAR_METHOD_FETCH_OBJECT;
  716. co->ucal->setSkippedWallTimeOption((UCalendarWallTimeOption)option);
  717. RETURN_TRUE;
  718. }
  719. U_CFUNC PHP_FUNCTION(intlcal_from_date_time)
  720. {
  721. zend_object *date_obj;
  722. zend_string *date_str;
  723. zval zv_tmp, zv_arg, zv_timestamp;
  724. php_date_obj *datetime;
  725. char *locale_str = NULL;
  726. size_t locale_str_len;
  727. TimeZone *timeZone;
  728. UErrorCode status = U_ZERO_ERROR;
  729. Calendar *cal;
  730. intl_error_reset(NULL);
  731. ZEND_PARSE_PARAMETERS_START(1, 2)
  732. Z_PARAM_OBJ_OF_CLASS_OR_STR(date_obj, php_date_get_date_ce(), date_str)
  733. Z_PARAM_OPTIONAL
  734. Z_PARAM_STRING_OR_NULL(locale_str, locale_str_len)
  735. ZEND_PARSE_PARAMETERS_END();
  736. if (date_str) {
  737. object_init_ex(&zv_tmp, php_date_get_date_ce());
  738. ZVAL_STR(&zv_arg, date_str);
  739. zend_call_known_instance_method_with_1_params(Z_OBJCE(zv_tmp)->constructor, Z_OBJ(zv_tmp), NULL, &zv_arg);
  740. date_obj = Z_OBJ(zv_tmp);
  741. if (EG(exception)) {
  742. zend_object_store_ctor_failed(Z_OBJ(zv_tmp));
  743. goto error;
  744. }
  745. }
  746. datetime = php_date_obj_from_obj(date_obj);
  747. if (!datetime->time) {
  748. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  749. "intlcal_from_date_time: DateTime object is unconstructed",
  750. 0);
  751. goto error;
  752. }
  753. zend_call_method_with_0_params(date_obj, php_date_get_date_ce(), NULL, "gettimestamp", &zv_timestamp);
  754. if (Z_TYPE(zv_timestamp) != IS_LONG) {
  755. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  756. "intlcal_from_date_time: bad DateTime; call to "
  757. "DateTime::getTimestamp() failed", 0);
  758. zval_ptr_dtor(&zv_timestamp);
  759. goto error;
  760. }
  761. if (!datetime->time->is_localtime) {
  762. timeZone = TimeZone::getGMT()->clone();
  763. } else {
  764. timeZone = timezone_convert_datetimezone(datetime->time->zone_type,
  765. datetime, 1, NULL, "intlcal_from_date_time");
  766. if (timeZone == NULL) {
  767. goto error;
  768. }
  769. }
  770. if (!locale_str) {
  771. locale_str = const_cast<char*>(intl_locale_get_default());
  772. }
  773. cal = Calendar::createInstance(timeZone,
  774. Locale::createFromName(locale_str), status);
  775. if (cal == NULL) {
  776. delete timeZone;
  777. intl_error_set(NULL, status, "intlcal_from_date_time: "
  778. "error creating ICU Calendar object", 0);
  779. goto error;
  780. }
  781. cal->setTime(((UDate)Z_LVAL(zv_timestamp)) * 1000., status);
  782. if (U_FAILURE(status)) {
  783. /* time zone was adopted by cal; should not be deleted here */
  784. delete cal;
  785. intl_error_set(NULL, status, "intlcal_from_date_time: "
  786. "error creating ICU Calendar::setTime()", 0);
  787. goto error;
  788. }
  789. calendar_object_create(return_value, cal);
  790. error:
  791. if (date_str) {
  792. OBJ_RELEASE(date_obj);
  793. }
  794. }
  795. U_CFUNC PHP_FUNCTION(intlcal_to_date_time)
  796. {
  797. zval retval;
  798. CALENDAR_METHOD_INIT_VARS;
  799. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  800. &object, Calendar_ce_ptr) == FAILURE) {
  801. RETURN_THROWS();
  802. }
  803. CALENDAR_METHOD_FETCH_OBJECT;
  804. /* There are no exported functions in ext/date to this
  805. * in a more native fashion */
  806. double date = co->ucal->getTime(CALENDAR_ERROR_CODE(co)) / 1000.;
  807. int64_t ts;
  808. char ts_str[sizeof("@-9223372036854775808")];
  809. int ts_str_len;
  810. zval ts_zval, tmp;
  811. INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed");
  812. if (date > (double)U_INT64_MAX || date < (double)U_INT64_MIN) {
  813. intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR,
  814. "intlcal_to_date_time: The calendar date is out of the "
  815. "range for a 64-bit integer", 0);
  816. RETURN_FALSE;
  817. }
  818. ZVAL_UNDEF(&retval);
  819. ts = (int64_t)date;
  820. ts_str_len = slprintf(ts_str, sizeof(ts_str), "@%" PRIi64, ts);
  821. ZVAL_STRINGL(&ts_zval, ts_str, ts_str_len);
  822. /* Now get the time zone */
  823. const TimeZone& tz = co->ucal->getTimeZone();
  824. zval *timezone_zval = timezone_convert_to_datetimezone(
  825. &tz, CALENDAR_ERROR_P(co), "intlcal_to_date_time", &tmp);
  826. if (timezone_zval == NULL) {
  827. zval_ptr_dtor(&ts_zval);
  828. RETURN_FALSE;
  829. }
  830. /* resources allocated from now on */
  831. /* Finally, instantiate object and call constructor */
  832. object_init_ex(return_value, php_date_get_date_ce());
  833. zend_call_known_instance_method_with_2_params(
  834. Z_OBJCE_P(return_value)->constructor, Z_OBJ_P(return_value), NULL, &ts_zval, timezone_zval);
  835. if (EG(exception)) {
  836. zend_object_store_ctor_failed(Z_OBJ_P(return_value));
  837. zval_ptr_dtor(return_value);
  838. zval_ptr_dtor(&ts_zval);
  839. RETVAL_FALSE;
  840. goto error;
  841. }
  842. zval_ptr_dtor(&ts_zval);
  843. /* due to bug #40743, we have to set the time zone again */
  844. zend_call_method_with_1_params(Z_OBJ_P(return_value), NULL, NULL, "settimezone",
  845. &retval, timezone_zval);
  846. if (Z_ISUNDEF(retval) || Z_TYPE(retval) == IS_FALSE) {
  847. intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR,
  848. "intlcal_to_date_time: call to DateTime::setTimeZone has failed",
  849. 1);
  850. zval_ptr_dtor(return_value);
  851. RETVAL_FALSE;
  852. goto error;
  853. }
  854. error:
  855. zval_ptr_dtor(timezone_zval);
  856. zval_ptr_dtor(&retval);
  857. }
  858. U_CFUNC PHP_FUNCTION(intlcal_get_error_code)
  859. {
  860. CALENDAR_METHOD_INIT_VARS;
  861. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  862. &object, Calendar_ce_ptr) == FAILURE) {
  863. RETURN_THROWS();
  864. }
  865. /* Fetch the object (without resetting its last error code ). */
  866. co = Z_INTL_CALENDAR_P(object);
  867. if (co == NULL)
  868. RETURN_FALSE;
  869. RETURN_LONG((zend_long)CALENDAR_ERROR_CODE(co));
  870. }
  871. U_CFUNC PHP_FUNCTION(intlcal_get_error_message)
  872. {
  873. zend_string* message = NULL;
  874. CALENDAR_METHOD_INIT_VARS;
  875. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  876. &object, Calendar_ce_ptr) == FAILURE) {
  877. RETURN_THROWS();
  878. }
  879. /* Fetch the object (without resetting its last error code ). */
  880. co = Z_INTL_CALENDAR_P(object);
  881. if (co == NULL)
  882. RETURN_FALSE;
  883. /* Return last error message. */
  884. message = intl_error_get_message(CALENDAR_ERROR_P(co));
  885. RETURN_STR(message);
  886. }