dateformat_attr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: Kirti Velankar <kirtig@yahoo-inc.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "../php_intl.h"
  20. #include "dateformat_class.h"
  21. #include "../intl_convert.h"
  22. #include "dateformat_class.h"
  23. #include "dateformat_attr.h"
  24. #include <unicode/ustring.h>
  25. #include <unicode/udat.h>
  26. /* {{{ proto unicode IntlDateFormatter::getDateType( )
  27. * Get formatter datetype. }}} */
  28. /* {{{ proto string datefmt_get_datetype( IntlDateFormatter $mf )
  29. * Get formatter datetype.
  30. */
  31. PHP_FUNCTION( datefmt_get_datetype )
  32. {
  33. DATE_FORMAT_METHOD_INIT_VARS;
  34. /* Parse parameters. */
  35. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
  36. {
  37. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  38. "datefmt_get_datetype: unable to parse input params", 0 TSRMLS_CC );
  39. RETURN_FALSE;
  40. }
  41. /* Fetch the object. */
  42. DATE_FORMAT_METHOD_FETCH_OBJECT;
  43. INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter datetype." );
  44. RETURN_LONG(dfo->date_type );
  45. }
  46. /* }}} */
  47. /* {{{ proto unicode IntlDateFormatter::getTimeType( )
  48. * Get formatter timetype. }}} */
  49. /* {{{ proto string datefmt_get_timetype( IntlDateFormatter $mf )
  50. * Get formatter timetype.
  51. */
  52. PHP_FUNCTION( datefmt_get_timetype )
  53. {
  54. DATE_FORMAT_METHOD_INIT_VARS;
  55. /* Parse parameters. */
  56. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
  57. {
  58. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  59. "datefmt_get_timetype: unable to parse input params", 0 TSRMLS_CC );
  60. RETURN_FALSE;
  61. }
  62. /* Fetch the object. */
  63. DATE_FORMAT_METHOD_FETCH_OBJECT;
  64. INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timetype." );
  65. RETURN_LONG(dfo->time_type );
  66. }
  67. /* }}} */
  68. /* {{{ proto string IntlDateFormatter::getPattern( )
  69. * Get formatter pattern. }}} */
  70. /* {{{ proto string datefmt_get_pattern( IntlDateFormatter $mf )
  71. * Get formatter pattern.
  72. */
  73. PHP_FUNCTION( datefmt_get_pattern )
  74. {
  75. UChar value_buf[64];
  76. int length = USIZE( value_buf );
  77. UChar* value = value_buf;
  78. zend_bool is_pattern_localized =FALSE;
  79. DATE_FORMAT_METHOD_INIT_VARS;
  80. /* Parse parameters. */
  81. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
  82. {
  83. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  84. "datefmt_get_pattern: unable to parse input params", 0 TSRMLS_CC );
  85. RETURN_FALSE;
  86. }
  87. /* Fetch the object. */
  88. DATE_FORMAT_METHOD_FETCH_OBJECT;
  89. length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo));
  90. if(INTL_DATA_ERROR_CODE(dfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
  91. ++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
  92. INTL_DATA_ERROR_CODE(dfo) = U_ZERO_ERROR;
  93. value = eumalloc(length);
  94. length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo) );
  95. if(U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
  96. efree(value);
  97. value = value_buf;
  98. }
  99. }
  100. INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter pattern" );
  101. INTL_METHOD_RETVAL_UTF8( dfo, value, length, ( value != value_buf ) );
  102. }
  103. /* }}} */
  104. /* {{{ proto bool IntlDateFormatter::setPattern( string $pattern )
  105. * Set formatter pattern. }}} */
  106. /* {{{ proto bool datefmt_set_pattern( IntlDateFormatter $mf, string $pattern )
  107. * Set formatter pattern.
  108. */
  109. PHP_FUNCTION( datefmt_set_pattern )
  110. {
  111. char* value = NULL;
  112. int value_len = 0;
  113. int slength = 0;
  114. UChar* svalue = NULL;
  115. zend_bool is_pattern_localized =FALSE;
  116. DATE_FORMAT_METHOD_INIT_VARS;
  117. /* Parse parameters. */
  118. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
  119. &object, IntlDateFormatter_ce_ptr, &value, &value_len ) == FAILURE )
  120. {
  121. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  122. "datefmt_set_pattern: unable to parse input params", 0 TSRMLS_CC);
  123. RETURN_FALSE;
  124. }
  125. DATE_FORMAT_METHOD_FETCH_OBJECT;
  126. /* Convert given pattern to UTF-16. */
  127. intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(dfo));
  128. INTL_METHOD_CHECK_STATUS(dfo, "Error converting pattern to UTF-16" );
  129. udat_applyPattern(DATE_FORMAT_OBJECT(dfo), (UBool)is_pattern_localized, svalue, slength);
  130. if (svalue) {
  131. efree(svalue);
  132. }
  133. INTL_METHOD_CHECK_STATUS(dfo, "Error setting symbol value");
  134. RETURN_TRUE;
  135. }
  136. /* }}} */
  137. /* {{{ proto string IntlDateFormatter::getLocale()
  138. * Get formatter locale. }}} */
  139. /* {{{ proto string datefmt_get_locale(IntlDateFormatter $mf)
  140. * Get formatter locale.
  141. */
  142. PHP_FUNCTION( datefmt_get_locale )
  143. {
  144. char *loc;
  145. long loc_type =ULOC_ACTUAL_LOCALE;
  146. DATE_FORMAT_METHOD_INIT_VARS;
  147. /* Parse parameters. */
  148. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l",
  149. &object, IntlDateFormatter_ce_ptr,&loc_type) == FAILURE )
  150. {
  151. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  152. "datefmt_get_locale: unable to parse input params", 0 TSRMLS_CC );
  153. RETURN_FALSE;
  154. }
  155. /* Fetch the object. */
  156. DATE_FORMAT_METHOD_FETCH_OBJECT;
  157. loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), loc_type,&INTL_DATA_ERROR_CODE(dfo));
  158. INTL_METHOD_CHECK_STATUS(dfo, "Error getting locale");
  159. RETURN_STRING(loc, 1);
  160. }
  161. /* }}} */
  162. /* {{{ proto string IntlDateFormatter::isLenient()
  163. * Get formatter isLenient. }}} */
  164. /* {{{ proto string datefmt_isLenient(IntlDateFormatter $mf)
  165. * Get formatter locale.
  166. */
  167. PHP_FUNCTION( datefmt_is_lenient )
  168. {
  169. DATE_FORMAT_METHOD_INIT_VARS;
  170. /* Parse parameters. */
  171. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  172. &object, IntlDateFormatter_ce_ptr ) == FAILURE )
  173. {
  174. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  175. "datefmt_is_lenient: unable to parse input params", 0 TSRMLS_CC );
  176. RETURN_FALSE;
  177. }
  178. /* Fetch the object. */
  179. DATE_FORMAT_METHOD_FETCH_OBJECT;
  180. RETVAL_BOOL(udat_isLenient(DATE_FORMAT_OBJECT(dfo)));
  181. }
  182. /* }}} */
  183. /* {{{ proto string IntlDateFormatter::setLenient()
  184. * Set formatter lenient. }}} */
  185. /* {{{ proto string datefmt_setLenient(IntlDateFormatter $mf)
  186. * Set formatter lenient.
  187. */
  188. PHP_FUNCTION( datefmt_set_lenient )
  189. {
  190. zend_bool isLenient = FALSE;
  191. DATE_FORMAT_METHOD_INIT_VARS;
  192. /* Parse parameters. */
  193. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ob",
  194. &object, IntlDateFormatter_ce_ptr,&isLenient ) == FAILURE )
  195. {
  196. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  197. "datefmt_set_lenient: unable to parse input params", 0 TSRMLS_CC );
  198. RETURN_FALSE;
  199. }
  200. /* Fetch the object. */
  201. DATE_FORMAT_METHOD_FETCH_OBJECT;
  202. udat_setLenient(DATE_FORMAT_OBJECT(dfo), (UBool)isLenient );
  203. }
  204. /* }}} */