PageRenderTime 47ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/intl/dateformat/dateformat_parse.c

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