Skip to content

Commit 52db0b4

Browse files
committed
ext/intl: TimeZone address todo to throw exceptions on error.
1 parent 8aac698 commit 52db0b4

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

ext/intl/tests/dateformat_setTimeZone_error.phpt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ ini_set("date.timezone", 'Atlantic/Azores');
1010

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

13-
var_dump($df->setTimeZone(array()));
14-
var_dump($df->setTimeZone('non existing timezone'));
13+
try {
14+
$df->setTimeZone(array());
15+
} catch (Throwable $e) {
16+
echo $e->getMessage() . PHP_EOL;
17+
}
1518

19+
try {
20+
$df->setTimeZone('non existing timezone');
21+
} catch (Throwable $e) {
22+
echo $e->getMessage();
23+
}
1624
?>
1725
--EXPECTF--
1826
Warning: Array to string conversion in %s on line %d
19-
20-
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'Array' in %s on line %d
21-
bool(false)
22-
23-
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'non existing timezone' in %s on line %d
24-
bool(false)
27+
datefmt_set_timezone: No such time zone: 'Array'
28+
datefmt_set_timezone: No such time zone: 'non existing timezone'

ext/intl/timezone/timezone_class.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,15 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
147147
instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr)) {
148148
TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone);
149149

150-
/* TODO Throw proper Error exceptions for uninitialized classes and failure to clone */
151150
if (to->utimezone == NULL) {
152-
spprintf(&message, 0, "%s: passed IntlTimeZone is not "
151+
zend_throw_error(NULL, "%s: passed IntlTimeZone is not "
153152
"properly constructed", func);
154-
if (message) {
155-
intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
156-
efree(message);
157-
}
158153
zval_ptr_dtor_str(&local_zv_tz);
159154
return NULL;
160155
}
161156
timeZone = to->utimezone->clone();
162157
if (UNEXPECTED(timeZone == NULL)) {
163-
spprintf(&message, 0, "%s: could not clone TimeZone", func);
164-
if (message) {
165-
intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
166-
efree(message);
167-
}
158+
zend_throw_error(NULL, "%s: could not clone TimeZone", func);
168159
zval_ptr_dtor_str(&local_zv_tz);
169160
return NULL;
170161
}
@@ -185,32 +176,20 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
185176
}
186177
if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone),
187178
&status) == FAILURE) {
188-
spprintf(&message, 0, "%s: Time zone identifier given is not a "
179+
zend_throw_error(NULL, "%s: Time zone identifier given is not a "
189180
"valid UTF-8 string", func);
190-
if (message) {
191-
intl_errors_set(outside_error, status, message, 1);
192-
efree(message);
193-
}
194181
zval_ptr_dtor_str(&local_zv_tz);
195182
return NULL;
196183
}
197184
timeZone = TimeZone::createTimeZone(id);
198185
if (UNEXPECTED(timeZone == NULL)) {
199-
spprintf(&message, 0, "%s: Could not create time zone", func);
200-
if (message) {
201-
intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
202-
efree(message);
203-
}
186+
zend_throw_error(NULL, "%s: Could not create time zone", func);
204187
zval_ptr_dtor_str(&local_zv_tz);
205188
return NULL;
206189
}
207190
if (*timeZone == TimeZone::getUnknown()) {
208-
spprintf(&message, 0, "%s: No such time zone: '%s'",
191+
zend_throw_error(NULL, "%s: No such time zone: '%s'",
209192
func, Z_STRVAL_P(zv_timezone));
210-
if (message) {
211-
intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
212-
efree(message);
213-
}
214193
zval_ptr_dtor_str(&local_zv_tz);
215194
delete timeZone;
216195
return NULL;

0 commit comments

Comments
 (0)