calendar_methods.cpp 35 KB

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