calendar_class.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/calendar.h>
  19. #include <unicode/gregocal.h>
  20. extern "C" {
  21. #define USE_TIMEZONE_POINTER 1
  22. #include "../timezone/timezone_class.h"
  23. #define USE_CALENDAR_POINTER 1
  24. #include "calendar_class.h"
  25. #include "calendar_arginfo.h"
  26. #include <zend_exceptions.h>
  27. #include <assert.h>
  28. }
  29. using icu::GregorianCalendar;
  30. using icu::Locale;
  31. /* {{{ Global variables */
  32. zend_class_entry *Calendar_ce_ptr;
  33. zend_class_entry *GregorianCalendar_ce_ptr;
  34. zend_object_handlers Calendar_handlers;
  35. /* }}} */
  36. U_CFUNC void calendar_object_create(zval *object,
  37. Calendar *calendar)
  38. {
  39. UClassID classId = calendar->getDynamicClassID();
  40. zend_class_entry *ce;
  41. //if (dynamic_cast<GregorianCalendar*>(calendar) != NULL) {
  42. if (classId == GregorianCalendar::getStaticClassID()) {
  43. ce = GregorianCalendar_ce_ptr;
  44. } else {
  45. ce = Calendar_ce_ptr;
  46. }
  47. object_init_ex(object, ce);
  48. calendar_object_construct(object, calendar);
  49. }
  50. U_CFUNC Calendar *calendar_fetch_native_calendar(zend_object *object)
  51. {
  52. Calendar_object *co = php_intl_calendar_fetch_object(object);
  53. return co->ucal;
  54. }
  55. U_CFUNC void calendar_object_construct(zval *object,
  56. Calendar *calendar)
  57. {
  58. Calendar_object *co;
  59. CALENDAR_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
  60. assert(co->ucal == NULL);
  61. co->ucal = (Calendar*)calendar;
  62. }
  63. /* {{{ clone handler for Calendar */
  64. static zend_object *Calendar_clone_obj(zend_object *object)
  65. {
  66. Calendar_object *co_orig,
  67. *co_new;
  68. zend_object *ret_val;
  69. intl_error_reset(NULL);
  70. co_orig = php_intl_calendar_fetch_object(object);
  71. intl_error_reset(INTL_DATA_ERROR_P(co_orig));
  72. ret_val = Calendar_ce_ptr->create_object(object->ce);
  73. co_new = php_intl_calendar_fetch_object(ret_val);
  74. zend_objects_clone_members(&co_new->zo, &co_orig->zo);
  75. if (co_orig->ucal != NULL) {
  76. Calendar *newCalendar;
  77. newCalendar = co_orig->ucal->clone();
  78. if (!newCalendar) {
  79. zend_string *err_msg;
  80. intl_errors_set_code(CALENDAR_ERROR_P(co_orig),
  81. U_MEMORY_ALLOCATION_ERROR);
  82. intl_errors_set_custom_msg(CALENDAR_ERROR_P(co_orig),
  83. "Could not clone IntlCalendar", 0);
  84. err_msg = intl_error_get_message(CALENDAR_ERROR_P(co_orig));
  85. zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
  86. zend_string_free(err_msg);
  87. } else {
  88. co_new->ucal = newCalendar;
  89. }
  90. } else {
  91. zend_throw_exception(NULL, "Cannot clone unconstructed IntlCalendar", 0);
  92. }
  93. return ret_val;
  94. }
  95. /* }}} */
  96. static const struct {
  97. UCalendarDateFields field;
  98. const char *name;
  99. } debug_info_fields[] = {
  100. {UCAL_ERA, "era"},
  101. {UCAL_YEAR, "year"},
  102. {UCAL_MONTH, "month"},
  103. {UCAL_WEEK_OF_YEAR, "week of year"},
  104. {UCAL_WEEK_OF_MONTH, "week of month"},
  105. {UCAL_DAY_OF_YEAR, "day of year"},
  106. {UCAL_DAY_OF_MONTH, "day of month"},
  107. {UCAL_DAY_OF_WEEK, "day of week"},
  108. {UCAL_DAY_OF_WEEK_IN_MONTH, "day of week in month"},
  109. {UCAL_AM_PM, "AM/PM"},
  110. {UCAL_HOUR, "hour"},
  111. {UCAL_HOUR_OF_DAY, "hour of day"},
  112. {UCAL_MINUTE, "minute"},
  113. {UCAL_SECOND, "second"},
  114. {UCAL_MILLISECOND, "millisecond"},
  115. {UCAL_ZONE_OFFSET, "zone offset"},
  116. {UCAL_DST_OFFSET, "DST offset"},
  117. {UCAL_YEAR_WOY, "year for week of year"},
  118. {UCAL_DOW_LOCAL, "localized day of week"},
  119. {UCAL_EXTENDED_YEAR, "extended year"},
  120. {UCAL_JULIAN_DAY, "julian day"},
  121. {UCAL_MILLISECONDS_IN_DAY, "milliseconds in day"},
  122. {UCAL_IS_LEAP_MONTH, "is leap month"},
  123. };
  124. /* {{{ get_debug_info handler for Calendar */
  125. static HashTable *Calendar_get_debug_info(zend_object *object, int *is_temp)
  126. {
  127. zval zv,
  128. zfields;
  129. Calendar_object *co;
  130. const Calendar *cal;
  131. HashTable *debug_info;
  132. *is_temp = 1;
  133. debug_info = zend_new_array(8);
  134. co = php_intl_calendar_fetch_object(object);
  135. cal = co->ucal;
  136. if (cal == NULL) {
  137. ZVAL_FALSE(&zv);
  138. zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
  139. return debug_info;
  140. }
  141. ZVAL_TRUE(&zv);
  142. zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
  143. ZVAL_STRING(&zv, const_cast<char*>(cal->getType()));
  144. zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &zv);
  145. {
  146. zval ztz,
  147. ztz_debug;
  148. int is_tmp;
  149. HashTable *debug_info_tz;
  150. timezone_object_construct(&cal->getTimeZone(), &ztz , 0);
  151. debug_info_tz = Z_OBJ_HANDLER(ztz, get_debug_info)(Z_OBJ(ztz), &is_tmp);
  152. assert(is_tmp == 1);
  153. array_init(&ztz_debug);
  154. zend_hash_copy(Z_ARRVAL(ztz_debug), debug_info_tz, zval_add_ref);
  155. zend_hash_destroy(debug_info_tz);
  156. FREE_HASHTABLE(debug_info_tz);
  157. zend_hash_str_update(debug_info, "timeZone", sizeof("timeZone") - 1, &ztz_debug);
  158. }
  159. {
  160. UErrorCode uec = U_ZERO_ERROR;
  161. Locale locale = cal->getLocale(ULOC_VALID_LOCALE, uec);
  162. if (U_SUCCESS(uec)) {
  163. ZVAL_STRING(&zv, const_cast<char*>(locale.getName()));
  164. zend_hash_str_update(debug_info, "locale", sizeof("locale") - 1, &zv);
  165. } else {
  166. ZVAL_STRING(&zv, const_cast<char*>(u_errorName(uec)));
  167. zend_hash_str_update(debug_info, "locale", sizeof("locale") - 1, &zv);
  168. }
  169. }
  170. array_init_size(&zfields, UCAL_FIELD_COUNT);
  171. for (int i = 0;
  172. i < sizeof(debug_info_fields) / sizeof(*debug_info_fields);
  173. i++) {
  174. UErrorCode uec = U_ZERO_ERROR;
  175. const char *name = debug_info_fields[i].name;
  176. int32_t res = cal->get(debug_info_fields[i].field, uec);
  177. if (U_SUCCESS(uec)) {
  178. add_assoc_long(&zfields, name, (zend_long)res);
  179. } else {
  180. add_assoc_string(&zfields, name, const_cast<char*>(u_errorName(uec)));
  181. }
  182. }
  183. zend_hash_str_update(debug_info, "fields", sizeof("fields") - 1, &zfields);
  184. return debug_info;
  185. }
  186. /* }}} */
  187. /* {{{ void calendar_object_init(Calendar_object* to)
  188. * Initialize internals of Calendar_object not specific to zend standard objects.
  189. */
  190. static void calendar_object_init(Calendar_object *co)
  191. {
  192. intl_error_init(CALENDAR_ERROR_P(co));
  193. co->ucal = NULL;
  194. }
  195. /* }}} */
  196. /* {{{ Calendar_objects_free */
  197. static void Calendar_objects_free(zend_object *object)
  198. {
  199. Calendar_object* co = php_intl_calendar_fetch_object(object);
  200. if (co->ucal) {
  201. delete co->ucal;
  202. co->ucal = NULL;
  203. }
  204. intl_error_reset(CALENDAR_ERROR_P(co));
  205. zend_object_std_dtor(&co->zo);
  206. }
  207. /* }}} */
  208. /* {{{ Calendar_object_create */
  209. static zend_object *Calendar_object_create(zend_class_entry *ce)
  210. {
  211. Calendar_object* intern;
  212. intern = (Calendar_object*)ecalloc(1, sizeof(Calendar_object) + sizeof(zval) * (ce->default_properties_count - 1));
  213. zend_object_std_init(&intern->zo, ce);
  214. object_properties_init(&intern->zo, ce);
  215. calendar_object_init(intern);
  216. intern->zo.handlers = &Calendar_handlers;
  217. return &intern->zo;
  218. }
  219. /* }}} */
  220. /* {{{ calendar_register_IntlCalendar_class
  221. * Initialize 'IntlCalendar' class
  222. */
  223. void calendar_register_IntlCalendar_class(void)
  224. {
  225. /* Create and register 'IntlCalendar' class. */
  226. Calendar_ce_ptr = register_class_IntlCalendar();
  227. Calendar_ce_ptr->create_object = Calendar_object_create;
  228. memcpy( &Calendar_handlers, &std_object_handlers,
  229. sizeof Calendar_handlers);
  230. Calendar_handlers.offset = XtOffsetOf(Calendar_object, zo);
  231. Calendar_handlers.clone_obj = Calendar_clone_obj;
  232. Calendar_handlers.get_debug_info = Calendar_get_debug_info;
  233. Calendar_handlers.free_obj = Calendar_objects_free;
  234. /* Declare 'IntlCalendar' class constants */
  235. #define CALENDAR_DECL_LONG_CONST(name, val) \
  236. zend_declare_class_constant_long(Calendar_ce_ptr, name, sizeof(name) - 1, \
  237. val)
  238. CALENDAR_DECL_LONG_CONST("FIELD_ERA", UCAL_ERA);
  239. CALENDAR_DECL_LONG_CONST("FIELD_YEAR", UCAL_YEAR);
  240. CALENDAR_DECL_LONG_CONST("FIELD_MONTH", UCAL_MONTH);
  241. CALENDAR_DECL_LONG_CONST("FIELD_WEEK_OF_YEAR", UCAL_WEEK_OF_YEAR);
  242. CALENDAR_DECL_LONG_CONST("FIELD_WEEK_OF_MONTH", UCAL_WEEK_OF_MONTH);
  243. CALENDAR_DECL_LONG_CONST("FIELD_DATE", UCAL_DATE);
  244. CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_YEAR", UCAL_DAY_OF_YEAR);
  245. CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_WEEK", UCAL_DAY_OF_WEEK);
  246. CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_WEEK_IN_MONTH", UCAL_DAY_OF_WEEK_IN_MONTH);
  247. CALENDAR_DECL_LONG_CONST("FIELD_AM_PM", UCAL_AM_PM);
  248. CALENDAR_DECL_LONG_CONST("FIELD_HOUR", UCAL_HOUR);
  249. CALENDAR_DECL_LONG_CONST("FIELD_HOUR_OF_DAY", UCAL_HOUR_OF_DAY);
  250. CALENDAR_DECL_LONG_CONST("FIELD_MINUTE", UCAL_MINUTE);
  251. CALENDAR_DECL_LONG_CONST("FIELD_SECOND", UCAL_SECOND);
  252. CALENDAR_DECL_LONG_CONST("FIELD_MILLISECOND", UCAL_MILLISECOND);
  253. CALENDAR_DECL_LONG_CONST("FIELD_ZONE_OFFSET", UCAL_ZONE_OFFSET);
  254. CALENDAR_DECL_LONG_CONST("FIELD_DST_OFFSET", UCAL_DST_OFFSET);
  255. CALENDAR_DECL_LONG_CONST("FIELD_YEAR_WOY", UCAL_YEAR_WOY);
  256. CALENDAR_DECL_LONG_CONST("FIELD_DOW_LOCAL", UCAL_DOW_LOCAL);
  257. CALENDAR_DECL_LONG_CONST("FIELD_EXTENDED_YEAR", UCAL_EXTENDED_YEAR);
  258. CALENDAR_DECL_LONG_CONST("FIELD_JULIAN_DAY", UCAL_JULIAN_DAY);
  259. CALENDAR_DECL_LONG_CONST("FIELD_MILLISECONDS_IN_DAY", UCAL_MILLISECONDS_IN_DAY);
  260. CALENDAR_DECL_LONG_CONST("FIELD_IS_LEAP_MONTH", UCAL_IS_LEAP_MONTH);
  261. CALENDAR_DECL_LONG_CONST("FIELD_FIELD_COUNT", UCAL_FIELD_COUNT);
  262. CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_MONTH", UCAL_DAY_OF_MONTH);
  263. CALENDAR_DECL_LONG_CONST("DOW_SUNDAY", UCAL_SUNDAY);
  264. CALENDAR_DECL_LONG_CONST("DOW_MONDAY", UCAL_MONDAY);
  265. CALENDAR_DECL_LONG_CONST("DOW_TUESDAY", UCAL_TUESDAY);
  266. CALENDAR_DECL_LONG_CONST("DOW_WEDNESDAY", UCAL_WEDNESDAY);
  267. CALENDAR_DECL_LONG_CONST("DOW_THURSDAY", UCAL_THURSDAY);
  268. CALENDAR_DECL_LONG_CONST("DOW_FRIDAY", UCAL_FRIDAY);
  269. CALENDAR_DECL_LONG_CONST("DOW_SATURDAY", UCAL_SATURDAY);
  270. CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKDAY", UCAL_WEEKDAY);
  271. CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKEND", UCAL_WEEKEND);
  272. CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKEND_OFFSET", UCAL_WEEKEND_ONSET);
  273. CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKEND_CEASE", UCAL_WEEKEND_CEASE);
  274. CALENDAR_DECL_LONG_CONST("WALLTIME_FIRST", UCAL_WALLTIME_FIRST);
  275. CALENDAR_DECL_LONG_CONST("WALLTIME_LAST", UCAL_WALLTIME_LAST);
  276. CALENDAR_DECL_LONG_CONST("WALLTIME_NEXT_VALID", UCAL_WALLTIME_NEXT_VALID);
  277. /* Create and register 'IntlGregorianCalendar' class. */
  278. GregorianCalendar_ce_ptr = register_class_IntlGregorianCalendar(Calendar_ce_ptr);
  279. }
  280. /* }}} */