gregoriancalendar_methods.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "../intl_cppshims.h"
  18. #include <unicode/locid.h>
  19. #include <unicode/calendar.h>
  20. #include <unicode/gregocal.h>
  21. #include <unicode/ustring.h>
  22. extern "C" {
  23. #include "../php_intl.h"
  24. #include "../intl_common.h"
  25. #define USE_TIMEZONE_POINTER 1
  26. #include "../timezone/timezone_class.h"
  27. #define USE_CALENDAR_POINTER 1
  28. #include "calendar_class.h"
  29. #include <ext/date/php_date.h>
  30. #include "zend_exceptions.h"
  31. }
  32. using icu::GregorianCalendar;
  33. using icu::Locale;
  34. using icu::UnicodeString;
  35. using icu::StringPiece;
  36. static inline GregorianCalendar *fetch_greg(Calendar_object *co) {
  37. return (GregorianCalendar*)co->ucal;
  38. }
  39. static void _php_intlgregcal_constructor_body(
  40. INTERNAL_FUNCTION_PARAMETERS, bool is_constructor, zend_error_handling *error_handling, bool *error_handling_replaced)
  41. {
  42. zval *tz_object = NULL;
  43. zval args_a[6],
  44. *args = &args_a[0];
  45. char *locale = NULL;
  46. size_t locale_len;
  47. zend_long largs[6];
  48. UErrorCode status = U_ZERO_ERROR;
  49. int variant;
  50. intl_error_reset(NULL);
  51. // parameter number validation / variant determination
  52. if (ZEND_NUM_ARGS() > 6 ||
  53. zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
  54. zend_argument_count_error("Too many arguments");
  55. RETURN_THROWS();
  56. }
  57. for (variant = ZEND_NUM_ARGS();
  58. variant > 0 && Z_TYPE(args[variant - 1]) == IS_NULL;
  59. variant--) {}
  60. if (variant == 4) {
  61. zend_argument_count_error("No variant with 4 arguments (excluding trailing NULLs)");
  62. RETURN_THROWS();
  63. }
  64. // argument parsing
  65. if (variant <= 2) {
  66. if (zend_parse_parameters(MIN(ZEND_NUM_ARGS(), 2),
  67. "|z!s!", &tz_object, &locale, &locale_len) == FAILURE) {
  68. RETURN_THROWS();
  69. }
  70. }
  71. if (variant > 2 && zend_parse_parameters(ZEND_NUM_ARGS(),
  72. "lll|lll", &largs[0], &largs[1], &largs[2], &largs[3], &largs[4],
  73. &largs[5]) == FAILURE) {
  74. RETURN_THROWS();
  75. }
  76. if (error_handling != NULL) {
  77. zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
  78. *error_handling_replaced = 1;
  79. }
  80. // instantion of ICU object
  81. Calendar_object *co = Z_INTL_CALENDAR_P(return_value);
  82. GregorianCalendar *gcal = NULL;
  83. if (co->ucal) {
  84. zend_throw_error(NULL, "IntlGregorianCalendar object is already constructed");
  85. RETURN_THROWS();
  86. }
  87. if (variant <= 2) {
  88. // From timezone and locale (0 to 2 arguments)
  89. TimeZone *tz = timezone_process_timezone_argument(tz_object, NULL,
  90. "intlgregcal_create_instance");
  91. if (tz == NULL) {
  92. if (!EG(exception)) {
  93. zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
  94. }
  95. if (!is_constructor) {
  96. zval_ptr_dtor(return_value);
  97. RETVAL_NULL();
  98. }
  99. return;
  100. }
  101. if (!locale) {
  102. locale = const_cast<char*>(intl_locale_get_default());
  103. }
  104. gcal = new GregorianCalendar(tz, Locale::createFromName(locale),
  105. status);
  106. // Should this throw?
  107. if (U_FAILURE(status)) {
  108. intl_error_set(NULL, status, "intlgregcal_create_instance: error "
  109. "creating ICU GregorianCalendar from time zone and locale", 0);
  110. if (gcal) {
  111. delete gcal;
  112. }
  113. delete tz;
  114. if (!is_constructor) {
  115. zval_ptr_dtor(return_value);
  116. RETVAL_NULL();
  117. }
  118. return;
  119. }
  120. } else {
  121. // From date/time (3, 5 or 6 arguments)
  122. for (int i = 0; i < variant; i++) {
  123. if (largs[i] < INT32_MIN || largs[i] > INT32_MAX) {
  124. zend_argument_value_error(getThis() ? (i-1) : i,
  125. "must be between %d and %d", INT32_MIN, INT32_MAX);
  126. RETURN_THROWS();
  127. }
  128. }
  129. if (variant == 3) {
  130. gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
  131. (int32_t)largs[2], status);
  132. } else if (variant == 5) {
  133. gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
  134. (int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], status);
  135. } else if (variant == 6) {
  136. gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
  137. (int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], (int32_t)largs[5],
  138. status);
  139. }
  140. if (U_FAILURE(status)) {
  141. intl_error_set(NULL, status, "intlgregcal_create_instance: error "
  142. "creating ICU GregorianCalendar from date", 0);
  143. if (gcal) {
  144. delete gcal;
  145. }
  146. if (!is_constructor) {
  147. zval_ptr_dtor(return_value);
  148. RETVAL_NULL();
  149. }
  150. return;
  151. }
  152. timelib_tzinfo *tzinfo = get_timezone_info();
  153. UnicodeString tzstr = UnicodeString::fromUTF8(StringPiece(tzinfo->name));
  154. if (tzstr.isBogus()) {
  155. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  156. "intlgregcal_create_instance: could not create UTF-8 string "
  157. "from PHP's default timezone name (see date_default_timezone_get())",
  158. 0);
  159. delete gcal;
  160. if (!is_constructor) {
  161. zval_ptr_dtor(return_value);
  162. RETVAL_NULL();
  163. }
  164. return;
  165. }
  166. TimeZone *tz = TimeZone::createTimeZone(tzstr);
  167. gcal->adoptTimeZone(tz);
  168. }
  169. co->ucal = gcal;
  170. }
  171. U_CFUNC PHP_FUNCTION(intlgregcal_create_instance)
  172. {
  173. intl_error_reset(NULL);
  174. object_init_ex(return_value, GregorianCalendar_ce_ptr);
  175. _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU, /* is_constructor */ 0, NULL, NULL);
  176. }
  177. U_CFUNC PHP_METHOD(IntlGregorianCalendar, __construct)
  178. {
  179. zend_error_handling error_handling;
  180. bool error_handling_replaced = 0;
  181. return_value = ZEND_THIS;
  182. _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU, /* is_constructor */ 1, &error_handling, &error_handling_replaced);
  183. if (error_handling_replaced) {
  184. zend_restore_error_handling(&error_handling);
  185. }
  186. }
  187. U_CFUNC PHP_FUNCTION(intlgregcal_set_gregorian_change)
  188. {
  189. double date;
  190. CALENDAR_METHOD_INIT_VARS;
  191. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  192. "Od", &object, GregorianCalendar_ce_ptr, &date) == FAILURE) {
  193. RETURN_THROWS();
  194. }
  195. CALENDAR_METHOD_FETCH_OBJECT;
  196. fetch_greg(co)->setGregorianChange(date, CALENDAR_ERROR_CODE(co));
  197. INTL_METHOD_CHECK_STATUS(co, "intlgregcal_set_gregorian_change: error "
  198. "calling ICU method");
  199. RETURN_TRUE;
  200. }
  201. U_CFUNC PHP_FUNCTION(intlgregcal_get_gregorian_change)
  202. {
  203. CALENDAR_METHOD_INIT_VARS;
  204. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  205. "O", &object, GregorianCalendar_ce_ptr) == FAILURE) {
  206. RETURN_THROWS();
  207. }
  208. CALENDAR_METHOD_FETCH_OBJECT;
  209. RETURN_DOUBLE((double)fetch_greg(co)->getGregorianChange());
  210. }
  211. U_CFUNC PHP_FUNCTION(intlgregcal_is_leap_year)
  212. {
  213. zend_long year;
  214. CALENDAR_METHOD_INIT_VARS;
  215. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  216. "Ol", &object, GregorianCalendar_ce_ptr, &year) == FAILURE) {
  217. RETURN_THROWS();
  218. }
  219. if (year < INT32_MIN || year > INT32_MAX) {
  220. zend_argument_value_error(getThis() ? 1 : 2, "must be between %d and %d", INT32_MIN, INT32_MAX);
  221. RETURN_THROWS();
  222. }
  223. CALENDAR_METHOD_FETCH_OBJECT;
  224. RETURN_BOOL((int)fetch_greg(co)->isLeapYear((int32_t)year));
  225. }