dateformat_attrcpp.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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. #include "../intl_cppshims.h"
  17. #include <unicode/timezone.h>
  18. #include <unicode/calendar.h>
  19. #include <unicode/datefmt.h>
  20. extern "C" {
  21. #include "../php_intl.h"
  22. #include "dateformat_class.h"
  23. #include "dateformat_attrcpp.h"
  24. #define USE_TIMEZONE_POINTER 1
  25. #include "../timezone/timezone_class.h"
  26. #define USE_CALENDAR_POINTER 1
  27. #include "../calendar/calendar_class.h"
  28. }
  29. #include "../intl_convertcpp.h"
  30. #include "dateformat_helpers.h"
  31. static inline DateFormat *fetch_datefmt(IntlDateFormatter_object *dfo) {
  32. return (DateFormat *)dfo->datef_data.udatf;
  33. }
  34. /* {{{ proto string IntlDateFormatter::getTimeZoneId()
  35. * Get formatter timezone_id. }}} */
  36. /* {{{ proto string datefmt_get_timezone_id(IntlDateFormatter $mf)
  37. * Get formatter timezone_id.
  38. */
  39. U_CFUNC PHP_FUNCTION(datefmt_get_timezone_id)
  40. {
  41. DATE_FORMAT_METHOD_INIT_VARS;
  42. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  43. &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
  44. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_get_timezone_"
  45. "id: unable to parse input params", 0 TSRMLS_CC);
  46. RETURN_FALSE;
  47. }
  48. DATE_FORMAT_METHOD_FETCH_OBJECT;
  49. UnicodeString res = UnicodeString();
  50. fetch_datefmt(dfo)->getTimeZone().getID(res);
  51. intl_charFromString(res, &Z_STRVAL_P(return_value),
  52. &Z_STRLEN_P(return_value), &INTL_DATA_ERROR_CODE(dfo));
  53. INTL_METHOD_CHECK_STATUS(dfo, "Could not convert time zone id to UTF-8");
  54. Z_TYPE_P(return_value) = IS_STRING;
  55. }
  56. /* {{{ proto IntlTimeZone IntlDateFormatter::getTimeZone()
  57. * Get formatter timezone. }}} */
  58. /* {{{ proto IntlTimeZone datefmt_get_timezone(IntlDateFormatter $mf)
  59. * Get formatter timezone.
  60. */
  61. U_CFUNC PHP_FUNCTION(datefmt_get_timezone)
  62. {
  63. DATE_FORMAT_METHOD_INIT_VARS;
  64. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  65. &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
  66. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  67. "datefmt_get_timezone: unable to parse input params", 0 TSRMLS_CC );
  68. RETURN_FALSE;
  69. }
  70. DATE_FORMAT_METHOD_FETCH_OBJECT;
  71. const TimeZone& tz = fetch_datefmt(dfo)->getTimeZone();
  72. TimeZone *tz_clone = tz.clone();
  73. if (tz_clone == NULL) {
  74. intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
  75. "datefmt_get_timezone: Out of memory when cloning time zone",
  76. 0 TSRMLS_CC);
  77. RETURN_FALSE;
  78. }
  79. object_init_ex(return_value, TimeZone_ce_ptr);
  80. timezone_object_construct(tz_clone, return_value, 1 TSRMLS_CC);
  81. }
  82. U_CFUNC PHP_FUNCTION(datefmt_set_timezone_id)
  83. {
  84. php_error_docref0(NULL TSRMLS_CC, E_DEPRECATED,
  85. "Use datefmt_set_timezone() instead, which also accepts a plain "
  86. "time zone identifier and for which this function is now an "
  87. "alias");
  88. PHP_FN(datefmt_set_timezone)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  89. }
  90. /* {{{ proto boolean IntlDateFormatter::setTimeZone(mixed $timezone)
  91. * Set formatter's timezone. }}} */
  92. /* {{{ proto boolean datefmt_set_timezone_id(IntlDateFormatter $mf, $timezone_id)
  93. * Set formatter timezone_id.
  94. */
  95. U_CFUNC PHP_FUNCTION(datefmt_set_timezone)
  96. {
  97. zval **timezone_zv;
  98. TimeZone *timezone;
  99. DATE_FORMAT_METHOD_INIT_VARS;
  100. if ( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
  101. "OZ", &object, IntlDateFormatter_ce_ptr, &timezone_zv) == FAILURE) {
  102. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_set_timezone: "
  103. "unable to parse input params", 0 TSRMLS_CC);
  104. RETURN_FALSE;
  105. }
  106. DATE_FORMAT_METHOD_FETCH_OBJECT;
  107. timezone = timezone_process_timezone_argument(timezone_zv,
  108. INTL_DATA_ERROR_P(dfo), "datefmt_set_timezone" TSRMLS_CC);
  109. if (timezone == NULL) {
  110. RETURN_FALSE;
  111. }
  112. fetch_datefmt(dfo)->adoptTimeZone(timezone);
  113. }
  114. /* {{{ proto int IntlDateFormatter::getCalendar( )
  115. * Get formatter calendar type. }}} */
  116. /* {{{ proto int datefmt_get_calendar(IntlDateFormatter $mf)
  117. * Get formatter calendar type.
  118. */
  119. U_CFUNC PHP_FUNCTION(datefmt_get_calendar)
  120. {
  121. DATE_FORMAT_METHOD_INIT_VARS;
  122. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  123. &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
  124. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  125. "datefmt_get_calendar: unable to parse input params", 0 TSRMLS_CC);
  126. RETURN_FALSE;
  127. }
  128. DATE_FORMAT_METHOD_FETCH_OBJECT;
  129. if (dfo->calendar == -1) {
  130. /* an IntlCalendar was provided to the constructor */
  131. RETURN_FALSE;
  132. }
  133. RETURN_LONG(dfo->calendar);
  134. }
  135. /* }}} */
  136. /* {{{ proto IntlCalendar IntlDateFormatter::getCalendarObject()
  137. * Get formatter calendar. }}} */
  138. /* {{{ proto IntlCalendar datefmt_get_calendar_object(IntlDateFormatter $mf)
  139. * Get formatter calendar.
  140. */
  141. U_CFUNC PHP_FUNCTION(datefmt_get_calendar_object)
  142. {
  143. DATE_FORMAT_METHOD_INIT_VARS;
  144. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  145. &object, IntlDateFormatter_ce_ptr ) == FAILURE) {
  146. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  147. "datefmt_get_calendar_object: unable to parse input params",
  148. 0 TSRMLS_CC);
  149. RETURN_FALSE;
  150. }
  151. DATE_FORMAT_METHOD_FETCH_OBJECT;
  152. const Calendar *cal = fetch_datefmt(dfo)->getCalendar();
  153. if (cal == NULL) {
  154. RETURN_NULL();
  155. }
  156. Calendar *cal_clone = cal->clone();
  157. if (cal_clone == NULL) {
  158. intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
  159. "datefmt_get_calendar_object: Out of memory when cloning "
  160. "calendar", 0 TSRMLS_CC);
  161. RETURN_FALSE;
  162. }
  163. calendar_object_create(return_value, cal_clone TSRMLS_CC);
  164. }
  165. /* }}} */
  166. /* {{{ proto bool IntlDateFormatter::setCalendar(mixed $calendar)
  167. * Set formatter's calendar. }}} */
  168. /* {{{ proto bool datefmt_set_calendar(IntlDateFormatter $mf, mixed $calendar)
  169. * Set formatter's calendar.
  170. */
  171. U_CFUNC PHP_FUNCTION(datefmt_set_calendar)
  172. {
  173. zval *calendar_zv;
  174. DATE_FORMAT_METHOD_INIT_VARS;
  175. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz",
  176. &object, IntlDateFormatter_ce_ptr, &calendar_zv) == FAILURE) {
  177. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  178. "datefmt_set_calendar: unable to parse input params", 0 TSRMLS_CC);
  179. RETURN_FALSE;
  180. }
  181. DATE_FORMAT_METHOD_FETCH_OBJECT;
  182. Calendar *cal;
  183. long cal_type;
  184. bool cal_owned;
  185. Locale locale = Locale::createFromName(dfo->requested_locale);
  186. // getting the actual locale from the DateFormat is not enough
  187. // because we would have lost modifiers such as @calendar. We
  188. // must store the requested locale on object creation
  189. if (datefmt_process_calendar_arg(calendar_zv, locale,
  190. "datefmt_set_calendar", INTL_DATA_ERROR_P(dfo), cal, cal_type,
  191. cal_owned TSRMLS_CC) == FAILURE) {
  192. RETURN_FALSE;
  193. }
  194. if (cal_owned) {
  195. /* a non IntlCalendar was specified, we want to keep the timezone */
  196. TimeZone *old_timezone = fetch_datefmt(dfo)->getTimeZone().clone();
  197. if (old_timezone == NULL) {
  198. intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
  199. "datefmt_set_calendar: Out of memory when cloning calendar",
  200. 0 TSRMLS_CC);
  201. delete cal;
  202. RETURN_FALSE;
  203. }
  204. cal->adoptTimeZone(old_timezone);
  205. } else {
  206. cal = cal->clone();
  207. if (cal == NULL) {
  208. intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
  209. "datefmt_set_calendar: Out of memory when cloning calendar",
  210. 0 TSRMLS_CC);
  211. RETURN_FALSE;
  212. }
  213. }
  214. fetch_datefmt(dfo)->adoptCalendar(cal);
  215. dfo->calendar = cal_type;
  216. RETURN_TRUE;
  217. }
  218. /* }}} */