dateformat_parse.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <unicode/ustring.h>
  20. #include <math.h>
  21. #include "php_intl.h"
  22. #include "intl_convert.h"
  23. #include "dateformat.h"
  24. #include "dateformat_class.h"
  25. #include "dateformat_parse.h"
  26. #include "dateformat_data.h"
  27. /* {{{
  28. * Internal function which calls the udat_parse
  29. * param int store_error acts like a boolean
  30. * if set to 1 - store any error encountered in the parameter parse_error
  31. * if set to 0 - no need to store any error encountered in the parameter parse_error
  32. */
  33. static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value TSRMLS_DC)
  34. {
  35. double result = 0;
  36. UDate timestamp =0;
  37. UChar* text_utf16 = NULL;
  38. int32_t text_utf16_len = 0;
  39. /* Convert timezone to UTF-16. */
  40. intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
  41. INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
  42. timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
  43. if( text_utf16 ){
  44. efree(text_utf16);
  45. }
  46. INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" );
  47. /* Since return is in sec. */
  48. result = (double)timestamp / U_MILLIS_PER_SECOND;
  49. if(result > LONG_MAX || result < -LONG_MAX) {
  50. ZVAL_DOUBLE(return_value, result<0?ceil(result):floor(result));
  51. } else {
  52. ZVAL_LONG(return_value, (long)result);
  53. }
  54. }
  55. /* }}} */
  56. static void add_to_localtime_arr( IntlDateFormatter_object *dfo, zval* return_value, const UCalendar *parsed_calendar, long calendar_field, char* key_name TSRMLS_DC)
  57. {
  58. long calendar_field_val = ucal_get( parsed_calendar, calendar_field, &INTL_DATA_ERROR_CODE(dfo));
  59. INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : could not get a field from calendar" );
  60. if( strcmp(key_name, CALENDAR_YEAR )==0 ){
  61. /* since tm_year is years from 1900 */
  62. add_assoc_long( return_value, key_name,( calendar_field_val-1900) );
  63. }else if( strcmp(key_name, CALENDAR_WDAY )==0 ){
  64. /* since tm_wday starts from 0 whereas ICU WDAY start from 1 */
  65. add_assoc_long( return_value, key_name,( calendar_field_val-1) );
  66. }else{
  67. add_assoc_long( return_value, key_name, calendar_field_val );
  68. }
  69. }
  70. /* {{{
  71. * Internal function which calls the udat_parseCalendar
  72. */
  73. static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value TSRMLS_DC)
  74. {
  75. UCalendar *parsed_calendar = NULL;
  76. UChar* text_utf16 = NULL;
  77. int32_t text_utf16_len = 0;
  78. long isInDST = 0;
  79. /* Convert timezone to UTF-16. */
  80. intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
  81. INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
  82. parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
  83. udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
  84. if (text_utf16) {
  85. efree(text_utf16);
  86. }
  87. INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" );
  88. array_init( return_value );
  89. /* Add entries from various fields of the obtained parsed_calendar */
  90. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_SECOND, CALENDAR_SEC TSRMLS_CC);
  91. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MINUTE, CALENDAR_MIN TSRMLS_CC);
  92. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_HOUR_OF_DAY, CALENDAR_HOUR TSRMLS_CC);
  93. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_YEAR, CALENDAR_YEAR TSRMLS_CC);
  94. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_MONTH, CALENDAR_MDAY TSRMLS_CC);
  95. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_WEEK, CALENDAR_WDAY TSRMLS_CC);
  96. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_YEAR, CALENDAR_YDAY TSRMLS_CC);
  97. add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MONTH, CALENDAR_MON TSRMLS_CC);
  98. /* Is in DST? */
  99. isInDST = ucal_inDaylightTime(parsed_calendar , &INTL_DATA_ERROR_CODE(dfo));
  100. INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : while checking if currently in DST." );
  101. add_assoc_long( return_value, CALENDAR_ISDST,(isInDST==1?1:0));
  102. }
  103. /* }}} */
  104. /* {{{ proto integer IntlDateFormatter::parse( string $text_to_parse [, int $parse_pos] )
  105. * Parse the string $value starting at parse_pos to a Unix timestamp -int }}}*/
  106. /* {{{ proto integer datefmt_parse( IntlDateFormatter $fmt, string $text_to_parse [, int $parse_pos] )
  107. * Parse the string $value starting at parse_pos to a Unix timestamp -int }}}*/
  108. PHP_FUNCTION(datefmt_parse)
  109. {
  110. char* text_to_parse = NULL;
  111. int32_t text_len =0;
  112. zval* z_parse_pos = NULL;
  113. int32_t parse_pos = -1;
  114. DATE_FORMAT_METHOD_INIT_VARS;
  115. /* Parse parameters. */
  116. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|z!",
  117. &object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
  118. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_parse: unable to parse input params", 0 TSRMLS_CC );
  119. RETURN_FALSE;
  120. }
  121. /* Fetch the object. */
  122. DATE_FORMAT_METHOD_FETCH_OBJECT;
  123. if (z_parse_pos) {
  124. convert_to_long(z_parse_pos);
  125. parse_pos = (int32_t)Z_LVAL_P(z_parse_pos);
  126. if(parse_pos > text_len) {
  127. RETURN_FALSE;
  128. }
  129. }
  130. internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
  131. if(z_parse_pos) {
  132. zval_dtor(z_parse_pos);
  133. ZVAL_LONG(z_parse_pos, parse_pos);
  134. }
  135. }
  136. /* }}} */
  137. /* {{{ proto integer IntlDateFormatter::localtime( string $text_to_parse[, int $parse_pos] )
  138. * Parse the string $value to a localtime array }}}*/
  139. /* {{{ proto integer datefmt_localtime( IntlDateFormatter $fmt, string $text_to_parse[, int $parse_pos ])
  140. * Parse the string $value to a localtime array }}}*/
  141. PHP_FUNCTION(datefmt_localtime)
  142. {
  143. char* text_to_parse = NULL;
  144. int32_t text_len =0;
  145. zval* z_parse_pos = NULL;
  146. int32_t parse_pos = -1;
  147. DATE_FORMAT_METHOD_INIT_VARS;
  148. /* Parse parameters. */
  149. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|z!",
  150. &object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
  151. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_parse_to_localtime: unable to parse input params", 0 TSRMLS_CC );
  152. RETURN_FALSE;
  153. }
  154. /* Fetch the object. */
  155. DATE_FORMAT_METHOD_FETCH_OBJECT;
  156. if(z_parse_pos) {
  157. convert_to_long(z_parse_pos);
  158. parse_pos = (int32_t)Z_LVAL_P(z_parse_pos);
  159. if(parse_pos > text_len) {
  160. RETURN_FALSE;
  161. }
  162. }
  163. internal_parse_to_localtime( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
  164. if(z_parse_pos) {
  165. zval_dtor(z_parse_pos);
  166. ZVAL_LONG(z_parse_pos, parse_pos);
  167. }
  168. }
  169. /* }}} */