Skip to content

ext/intl: IntlDateFormatter::parse adds new optional argument. #13779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/intl/dateformat/dateformat.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int>|false
Expand Down
9 changes: 8 additions & 1 deletion ext/intl/dateformat/dateformat_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 62 additions & 8 deletions ext/intl/dateformat/dateformat_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blerg I hate those sorts of macros.... but that's an existing problem with the codebase.


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)
{
Expand Down
35 changes: 35 additions & 0 deletions ext/intl/tests/gh13766.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
IntlDateFormatter::parse update its calendar
--EXTENSIONS--
intl
--FILE--
<?php
$oIntlDateFormatter = new IntlDateFormatter("en_GB");
$oIntlDateFormatter->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