Skip to content
Open
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
67 changes: 38 additions & 29 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,12 @@ PHP_FUNCTION(date_format)
}
/* }}} */

static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{ */
typedef enum {
PHP_DATE_MODIFY_WARNING,
PHP_DATE_MODIFY_THROW
} php_date_modify_error_mode;

static bool php_date_modify(zval *object, char *modify, size_t modify_len, const php_date_modify_error_mode error_mode) /* {{{ */
{
php_date_obj *dateobj;
timelib_time *tmp_time;
Expand All @@ -3233,10 +3238,22 @@ static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{

if (err && err->error_count) {
/* spit out the first library error message, at least */
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
if (error_mode == PHP_DATE_MODIFY_THROW) {
zend_string *func_name = get_active_function_or_method_name();
zend_throw_exception_ex(date_ce_date_malformed_string_exception, 0,
"%s(): Failed to parse time string (%s) at position %d (%c): %s",
ZSTR_VAL(func_name),
modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
zend_string_release_ex(func_name, false);
} else {
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
}
timelib_time_dtor(tmp_time);
return 0;
}
Expand Down Expand Up @@ -3305,7 +3322,7 @@ PHP_FUNCTION(date_modify)
RETURN_THROWS();
}

if (!php_date_modify(object, modify, modify_len)) {
if (!php_date_modify(object, modify, modify_len, PHP_DATE_MODIFY_WARNING)) {
RETURN_FALSE;
}

Expand All @@ -3319,21 +3336,16 @@ PHP_METHOD(DateTime, modify)
zval *object;
char *modify;
size_t modify_len;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(modify, modify_len)
ZEND_PARSE_PARAMETERS_END();

zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
if (!php_date_modify(object, modify, modify_len)) {
zend_restore_error_handling(&zeh);
if (!php_date_modify(object, modify, modify_len, PHP_DATE_MODIFY_THROW)) {
RETURN_THROWS();
}

zend_restore_error_handling(&zeh);

RETURN_OBJ_COPY(Z_OBJ_P(object));
}
/* }}} */
Expand All @@ -3344,7 +3356,6 @@ PHP_METHOD(DateTimeImmutable, modify)
zval *object, new_object;
char *modify;
size_t modify_len;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -3353,15 +3364,11 @@ PHP_METHOD(DateTimeImmutable, modify)

date_clone_immutable(object, &new_object);

zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
if (!php_date_modify(&new_object, modify, modify_len)) {
if (!php_date_modify(&new_object, modify, modify_len, PHP_DATE_MODIFY_THROW)) {
zval_ptr_dtor(&new_object);
zend_restore_error_handling(&zeh);
RETURN_THROWS();
}

zend_restore_error_handling(&zeh);

RETURN_OBJ(Z_OBJ(new_object));
}
/* }}} */
Expand Down Expand Up @@ -3418,7 +3425,7 @@ PHP_METHOD(DateTimeImmutable, add)
}
/* }}} */

static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
static void php_date_sub(zval *object, zval *interval, zval *return_value, const bool should_throw) /* {{{ */
{
php_date_obj *dateobj;
php_interval_obj *intobj;
Expand All @@ -3430,7 +3437,15 @@ static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{
DATE_CHECK_INITIALIZED(intobj->initialized, Z_OBJCE_P(interval));

if (intobj->diff->have_weekday_relative || intobj->diff->have_special_relative) {
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
if (should_throw) {
zend_string *func_name = get_active_function_or_method_name();
zend_throw_exception_ex(date_ce_date_invalid_operation_exception, 0,
"%s(): Only non-special relative time specifications are supported for subtraction",
ZSTR_VAL(func_name));
zend_string_release_ex(func_name, false);
} else {
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
}
return;
}

Expand All @@ -3452,7 +3467,7 @@ PHP_FUNCTION(date_sub)
RETURN_THROWS();
}

php_date_sub(object, interval, return_value);
php_date_sub(object, interval, return_value, false);
RETURN_OBJ_COPY(Z_OBJ_P(object));
}
/* }}} */
Expand All @@ -3461,15 +3476,12 @@ PHP_FUNCTION(date_sub)
PHP_METHOD(DateTime, sub)
{
zval *object, *interval;
zend_error_handling zeh;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
RETURN_THROWS();
}

zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
php_date_sub(object, interval, return_value);
zend_restore_error_handling(&zeh);
php_date_sub(object, interval, return_value, true);

RETURN_OBJ_COPY(Z_OBJ_P(object));
}
Expand All @@ -3479,7 +3491,6 @@ PHP_METHOD(DateTime, sub)
PHP_METHOD(DateTimeImmutable, sub)
{
zval *object, *interval, new_object;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -3488,9 +3499,7 @@ PHP_METHOD(DateTimeImmutable, sub)

date_clone_immutable(object, &new_object);

zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
php_date_sub(&new_object, interval, return_value);
zend_restore_error_handling(&zeh);
php_date_sub(&new_object, interval, return_value, true);

RETURN_OBJ(Z_OBJ(new_object));
}
Expand Down
Loading