diff --git a/ext/intl/dateformat/dateformat.stub.php b/ext/intl/dateformat/dateformat.stub.php index 06f26b0658739..7582af2717fbe 100644 --- a/ext/intl/dateformat/dateformat.stub.php +++ b/ext/intl/dateformat/dateformat.stub.php @@ -159,6 +159,11 @@ public static function formatObject($datetime, $format = null, ?string $locale = */ public function parse(string $string, &$offset = null): int|float|false {} + /** + * @param int $offset + */ + public function parseToCalendar(string $string, &$offset = null): int|float|false {} + /** * @param int $offset * @return array|false diff --git a/ext/intl/dateformat/dateformat_arginfo.h b/ext/intl/dateformat/dateformat_arginfo.h index 4d16a26ef1673..df1de7d6895f4 100644 --- a/ext/intl/dateformat/dateformat_arginfo.h +++ b/ext/intl/dateformat/dateformat_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 91f1dbe4843fd1d4dff7266e814a3c8f9aed882a */ + * Stub hash: 56b66b1b51220ddbff698ec4c9a6ae60f3e0bfb0 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlDateFormatter___construct, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, locale, IS_STRING, 1) @@ -75,6 +75,11 @@ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_IntlDateFormatte ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, offset, "null") ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_IntlDateFormatter_parseToCalendar, 0, 1, MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_FALSE) + ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0) + ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, offset, "null") +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_IntlDateFormatter_localtime, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0) ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, offset, "null") @@ -104,6 +109,7 @@ ZEND_FUNCTION(datefmt_is_lenient); ZEND_FUNCTION(datefmt_format); ZEND_FUNCTION(datefmt_format_object); ZEND_FUNCTION(datefmt_parse); +ZEND_METHOD(IntlDateFormatter, parseToCalendar); ZEND_FUNCTION(datefmt_localtime); ZEND_FUNCTION(datefmt_get_error_code); ZEND_FUNCTION(datefmt_get_error_message); @@ -127,6 +133,7 @@ static const zend_function_entry class_IntlDateFormatter_methods[] = { ZEND_RAW_FENTRY("format", zif_datefmt_format, arginfo_class_IntlDateFormatter_format, ZEND_ACC_PUBLIC, NULL, NULL) ZEND_RAW_FENTRY("formatObject", zif_datefmt_format_object, arginfo_class_IntlDateFormatter_formatObject, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC, NULL, NULL) ZEND_RAW_FENTRY("parse", zif_datefmt_parse, arginfo_class_IntlDateFormatter_parse, ZEND_ACC_PUBLIC, NULL, NULL) + ZEND_ME(IntlDateFormatter, parseToCalendar, arginfo_class_IntlDateFormatter_parseToCalendar, ZEND_ACC_PUBLIC) ZEND_RAW_FENTRY("localtime", zif_datefmt_localtime, arginfo_class_IntlDateFormatter_localtime, ZEND_ACC_PUBLIC, NULL, NULL) ZEND_RAW_FENTRY("getErrorCode", zif_datefmt_get_error_code, arginfo_class_IntlDateFormatter_getErrorCode, ZEND_ACC_PUBLIC, NULL, NULL) ZEND_RAW_FENTRY("getErrorMessage", zif_datefmt_get_error_message, arginfo_class_IntlDateFormatter_getErrorMessage, ZEND_ACC_PUBLIC, NULL, NULL) diff --git a/ext/intl/dateformat/dateformat_parse.c b/ext/intl/dateformat/dateformat_parse.c index 69aeff886d3b9..82b5a71bbee8f 100644 --- a/ext/intl/dateformat/dateformat_parse.c +++ b/ext/intl/dateformat/dateformat_parse.c @@ -31,7 +31,7 @@ * if set to 1 - store any error encountered in the parameter parse_error * if set to 0 - no need to store any error encountered in the parameter parse_error */ -static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, size_t text_len, int32_t *parse_pos, zval *return_value) +static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, size_t text_len, int32_t *parse_pos, bool update_calendar, zval *return_value) { double result = 0; UDate timestamp =0; @@ -42,13 +42,22 @@ static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* tex intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo)); INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" ); - timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); - if( text_utf16 ){ - efree(text_utf16); + if (UNEXPECTED(update_calendar)) { + UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo)); + udat_parseCalendar(DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); + if (text_utf16) { + efree(text_utf16); + } + INTL_METHOD_CHECK_STATUS( dfo, "Calendar parsing failed" ); + timestamp = ucal_getMillis( parsed_calendar, &INTL_DATA_ERROR_CODE(dfo)); + } else { + timestamp = udat_parse(DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); + if (text_utf16) { + efree(text_utf16); + } } INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" ); - /* Since return is in sec. */ result = (double)timestamp / U_MILLIS_PER_SECOND; if (result > (double)LONG_MAX || result < (double)LONG_MIN) { @@ -145,18 +154,63 @@ PHP_FUNCTION(datefmt_parse) RETURN_FALSE; } parse_pos = (int32_t)long_parse_pos; - if((size_t)parse_pos > text_len) { + if ((size_t)parse_pos > text_len) { RETURN_FALSE; } } - internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value); - if(z_parse_pos) { + internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos ? &parse_pos : NULL, false, return_value); + if (z_parse_pos) { zval_ptr_dtor(z_parse_pos); ZVAL_LONG(z_parse_pos, parse_pos); } } /* }}} */ +PHP_METHOD(IntlDateFormatter, parseToCalendar) +{ + zend_string *text_to_parse = NULL; + zval* z_parse_pos = NULL; + int32_t parse_pos = -1; + + DATE_FORMAT_METHOD_INIT_VARS; + + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STR(text_to_parse) + Z_PARAM_OPTIONAL + Z_PARAM_ZVAL(z_parse_pos) + ZEND_PARSE_PARAMETERS_END(); + + object = ZEND_THIS; + + /* Fetch the object. */ + DATE_FORMAT_METHOD_FETCH_OBJECT; + + if (z_parse_pos) { + zend_long long_parse_pos; + ZVAL_DEREF(z_parse_pos); + bool failed = false; + long_parse_pos = zval_try_get_long(z_parse_pos, &failed); + if (failed) { + zend_argument_type_error(2, "must be of type int, %s given", zend_zval_value_name(z_parse_pos)); + RETURN_THROWS(); + } + if (ZEND_LONG_INT_OVFL(long_parse_pos)) { + intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR); + intl_error_set_custom_msg(NULL, "String index is out of valid range.", 0); + RETURN_FALSE; + } + parse_pos = (int32_t)long_parse_pos; + if (parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) { + RETURN_FALSE; + } + } + internal_parse_to_timestamp( dfo, ZSTR_VAL(text_to_parse), ZSTR_LEN(text_to_parse), z_parse_pos ? &parse_pos : NULL, true, return_value); + if (z_parse_pos) { + zval_ptr_dtor(z_parse_pos); + ZVAL_LONG(z_parse_pos, parse_pos); + } +} + /* {{{ Parse the string $value to a localtime array */ PHP_FUNCTION(datefmt_localtime) { diff --git a/ext/intl/tests/gh13766.phpt b/ext/intl/tests/gh13766.phpt new file mode 100644 index 0000000000000..b01b557e497a6 --- /dev/null +++ b/ext/intl/tests/gh13766.phpt @@ -0,0 +1,35 @@ +--TEST-- +IntlDateFormatter::parse update its calendar +--EXTENSIONS-- +intl +--FILE-- +setTimeZone('Europe/Berlin'); +$oIntlDateFormatter->setPattern('VV'); + +var_dump($oIntlDateFormatter->parse('America/Los_Angeles', $offset1)); +var_dump($oIntlDateFormatter->getTimeZone()->getID()); +var_dump($oIntlDateFormatter->parseToCalendar('America/Los_Angeles', $offset2)); +var_dump($oIntlDateFormatter->getTimeZone()->getID()); +$offset3 = "offset"; + +try { + $oIntlDateFormatter->parseToCalendar('America/Los_Angeles', $offset3); +} catch (\TypeError $e) { + echo $e->getMessage() . PHP_EOL; +} +$offset3 = PHP_INT_MAX * 16; +try { + $oIntlDateFormatter->parseToCalendar('America/Los_Angeles', $offset3); +} catch (\ValueError $e) { + echo $e->getMessage(); +} +--EXPECTF-- +int(%d) +string(13) "Europe/Berlin" +int(%d) +string(19) "America/Los_Angeles" +IntlDateFormatter::parseToCalendar(): Argument #2 ($offset) must be of type int, string given + +Deprecated: Implicit conversion from float 1.4757395258967641E+20 to int loses precision in %s on line %d