Skip to content

ext/intl: TimeZone address todo to throw exceptions on error. #17215

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 4 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
17 changes: 10 additions & 7 deletions ext/intl/tests/bug62017.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ intl
--FILE--
<?php
ini_set('intl.error_level', E_WARNING);
var_dump(
try {
datefmt_create('', IntlDateFormatter::NONE, IntlDateFormatter::NONE, "\xFF",
IntlDateFormatter::GREGORIAN, 'a'));
IntlDateFormatter::GREGORIAN, 'a');
} catch (IntlException $e) {
echo PHP_EOL."Exception: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . PHP_EOL;
Copy link
Member

Choose a reason for hiding this comment

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

Might be sufficient to only printf $e->getMessage(), but that should be done consistently for this test case. Leaving as is, is also fine for me.

}

try {
var_dump(
new IntlDateFormatter('', IntlDateFormatter::NONE, IntlDateFormatter::NONE, "Europe/Lisbon",
IntlDateFormatter::GREGORIAN, "\x80"));
IntlDateFormatter::GREGORIAN, "\x80");
} catch (IntlException $e) {
echo PHP_EOL."Exception: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . PHP_EOL;
}
?>
--EXPECTF--
Warning: datefmt_create(): datefmt_create: Time zone identifier given is not a valid UTF-8 string in %s on line %d
NULL

Exception: IntlDateFormatter::__construct(): datefmt_create: error converting pattern to UTF-16 in %sbug62017.php on line %d
Exception: datefmt_create: Time zone identifier given is not a valid UTF-8 string in %s on line %d

Exception: IntlDateFormatter::__construct(): datefmt_create: error converting pattern to UTF-16 in %s on line %d
11 changes: 7 additions & 4 deletions ext/intl/tests/calendar_createInstance_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class X extends IntlTimeZone {
function __construct() {}
}

var_dump(intlcal_create_instance(new X, NULL));
try {
intlcal_create_instance(new X, NULL);
} catch (IntlException $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
Warning: intlcal_create_instance(): intlcal_create_instance: passed IntlTimeZone is not properly constructed in %s on line %d
NULL
--EXPECT--
intlcal_create_instance: passed IntlTimeZone is not properly constructed
3 changes: 2 additions & 1 deletion ext/intl/tests/dateformat___construct_bad_tz_cal.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ try {
}
?>
--EXPECTF--
Exception: IntlDateFormatter::__construct(): datefmt_create: No such time zone: 'bad timezone' in %s on line %d

Exception: datefmt_create: No such time zone: 'bad timezone' in %s on line %d

Exception: IntlDateFormatter::__construct(): datefmt_create: Invalid value for calendar type; it must be one of IntlDateFormatter::TRADITIONAL (locale's default calendar) or IntlDateFormatter::GREGORIAN. Alternatively, it can be an IntlCalendar object in %s on line %d

Expand Down
20 changes: 12 additions & 8 deletions ext/intl/tests/dateformat_setTimeZone_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ ini_set("date.timezone", 'Atlantic/Azores');

$df = new IntlDateFormatter(NULL, 0, 0);

var_dump($df->setTimeZone(array()));
var_dump($df->setTimeZone('non existing timezone'));
try {
$df->setTimeZone(array());
} catch (IntlException $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
$df->setTimeZone('non existing timezone');
} catch (IntlException $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
Warning: Array to string conversion in %s on line %d

Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'Array' in %s on line %d
bool(false)

Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'non existing timezone' in %s on line %d
bool(false)
datefmt_set_timezone: No such time zone: 'Array'
datefmt_set_timezone: No such time zone: 'non existing timezone'
11 changes: 7 additions & 4 deletions ext/intl/tests/dateformat_set_timezone_id3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ function ut_main()

$res_str .= "-----------";
$res_str .= "\nTrying to set timezone_id= $timezone_id_entry";
if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed");
try {
ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry );
} catch (IntlException $e) {
echo $e->getMessage() . PHP_EOL;
}
$timezone_id = ut_datefmt_get_timezone_id( $fmt );
$res_str .= "\nAfter call to set_timezone_id : timezone_id= $timezone_id";
$formatted = ut_datefmt_format( $fmt, 0);
Expand All @@ -58,9 +62,8 @@ include_once( 'ut_common.inc' );
ut_run();
?>
--EXPECTF--
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d

Warning: datefmt_set_timezone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d
datefmt_set_timezone: No such time zone: 'CN'
datefmt_set_timezone: No such time zone: 'CN'

After creation of the dateformatter : timezone_id= US/Pacific
-----------
Expand Down
11 changes: 7 additions & 4 deletions ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ function ut_main()

$res_str .= "-----------";
$res_str .= "\nTrying to set timezone_id= $timezone_id_entry";
if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed");
try {
ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry );
} catch (IntlException $e) {
echo $e->getMessage() . PHP_EOL;
}
$timezone_id = ut_datefmt_get_timezone_id( $fmt );
$res_str .= "\nAfter call to set_timezone_id : timezone_id= $timezone_id";
$formatted = ut_datefmt_format( $fmt, 0);
Expand All @@ -58,9 +62,8 @@ include_once( 'ut_common.inc' );
ut_run();
?>
--EXPECTF--
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d

Warning: datefmt_set_timezone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d
datefmt_set_timezone: No such time zone: 'CN'
datefmt_set_timezone: No such time zone: 'CN'

After creation of the dateformatter : timezone_id= US/Pacific
-----------
Expand Down
32 changes: 6 additions & 26 deletions ext/intl/timezone/timezone_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <unicode/timezone.h>
#include <unicode/calendar.h>
#include "../intl_convertcpp.h"
#include "../intl_common.h"

#include "../common/common_date.h"

Expand Down Expand Up @@ -147,24 +148,15 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr)) {
TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone);

/* TODO Throw proper Error exceptions for uninitialized classes and failure to clone */
if (to->utimezone == NULL) {
spprintf(&message, 0, "%s: passed IntlTimeZone is not "
zend_throw_error(IntlException_ce_ptr, "%s: passed IntlTimeZone is not "
"properly constructed", func);
if (message) {
intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
efree(message);
}
zval_ptr_dtor_str(&local_zv_tz);
return NULL;
}
timeZone = to->utimezone->clone();
if (UNEXPECTED(timeZone == NULL)) {
spprintf(&message, 0, "%s: could not clone TimeZone", func);
if (message) {
intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
efree(message);
}
zend_throw_error(IntlException_ce_ptr, "%s: could not clone TimeZone", func);
zval_ptr_dtor_str(&local_zv_tz);
return NULL;
}
Expand All @@ -185,32 +177,20 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
}
if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone),
&status) == FAILURE) {
spprintf(&message, 0, "%s: Time zone identifier given is not a "
zend_throw_error(IntlException_ce_ptr, "%s: Time zone identifier given is not a "
"valid UTF-8 string", func);
if (message) {
intl_errors_set(outside_error, status, message, 1);
efree(message);
}
zval_ptr_dtor_str(&local_zv_tz);
return NULL;
}
timeZone = TimeZone::createTimeZone(id);
if (UNEXPECTED(timeZone == NULL)) {
spprintf(&message, 0, "%s: Could not create time zone", func);
if (message) {
intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
efree(message);
}
zend_throw_error(IntlException_ce_ptr, "%s: Could not create time zone", func);
zval_ptr_dtor_str(&local_zv_tz);
return NULL;
}
if (*timeZone == TimeZone::getUnknown()) {
spprintf(&message, 0, "%s: No such time zone: '%s'",
zend_throw_error(IntlException_ce_ptr, "%s: No such time zone: '%s'",
func, Z_STRVAL_P(zv_timezone));
if (message) {
intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
efree(message);
}
zval_ptr_dtor_str(&local_zv_tz);
delete timeZone;
return NULL;
Expand Down
Loading