Skip to content

Fixed bug #73239 (DateTime shows strange error message with invalid timezone) #8594

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

Merged
merged 5 commits into from
Jun 2, 2022
Merged
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
34 changes: 11 additions & 23 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static PHP_INI_MH(OnUpdate_date_timezone);

/* {{{ INI Settings */
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
STD_PHP_INI_ENTRY("date.timezone", "UTC", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL)
Expand Down Expand Up @@ -342,7 +342,6 @@ static PHP_GINIT_FUNCTION(date)
date_globals->default_timezone = NULL;
date_globals->timezone = NULL;
date_globals->tzcache = NULL;
date_globals->timezone_valid = 0;
}
/* }}} */

Expand Down Expand Up @@ -478,19 +477,18 @@ timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const t
/* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
static PHP_INI_MH(OnUpdate_date_timezone)
{
if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
if (new_value && ZSTR_VAL(new_value) && !timelib_timezone_id_is_valid(ZSTR_VAL(new_value), DATE_TIMEZONEDB)) {
php_error_docref(
NULL, E_WARNING,
"Invalid date.timezone value '%s', using '%s' instead",
ZSTR_VAL(new_value),
DATEG(default_timezone) ? DATEG(default_timezone) : "UTC"
);
return FAILURE;
}

DATEG(timezone_valid) = 0;
if (stage == PHP_INI_STAGE_RUNTIME) {
if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
if (DATEG(default_timezone) && *DATEG(default_timezone)) {
php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
}
} else {
DATEG(timezone_valid) = 1;
}
if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
return FAILURE;
}

return SUCCESS;
Comment on lines +490 to 494
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
return FAILURE;
}
return SUCCESS;
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);

Maybe?

Expand All @@ -500,7 +498,7 @@ static PHP_INI_MH(OnUpdate_date_timezone)
/* {{{ Helper functions */
static char* guess_timezone(const timelib_tzdb *tzdb)
{
/* Checking configure timezone */
/* Checking whether timezone has been set with date_default_timezone_set() */
if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
return DATEG(timezone);
}
Expand All @@ -514,16 +512,6 @@ static char* guess_timezone(const timelib_tzdb *tzdb)
return Z_STRVAL_P(ztz);
}
} else if (*DATEG(default_timezone)) {
if (DATEG(timezone_valid) == 1) {
return DATEG(default_timezone);
}

if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
return "UTC";
}

DATEG(timezone_valid) = 1;
return DATEG(default_timezone);
}
/* Fallback to UTC */
Expand Down
1 change: 0 additions & 1 deletion ext/date/php_date.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ ZEND_BEGIN_MODULE_GLOBALS(date)
char *timezone;
HashTable *tzcache;
timelib_error_container *last_errors;
int timezone_valid;
ZEND_END_MODULE_GLOBALS(date)

#define DATEG(v) ZEND_MODULE_GLOBALS_ACCESSOR(date, v)
Expand Down
13 changes: 13 additions & 0 deletions ext/date/tests/bug73239.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Bug #73239 (Odd warning/exception message with invalid timezones)
--FILE--
<?php
ini_set('date.timezone', 'dummy');
try {
$dt = new DateTime('now');
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECTF--
Warning: ini_set(): Invalid date.timezone value 'dummy', using 'UTC' instead in %sbug73239.php on line %d
1 change: 1 addition & 0 deletions ext/date/tests/date_default_timezone_get-1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ date.timezone=
echo date('e'), "\n";
?>
--EXPECT--
Warning: PHP Startup: Invalid date.timezone value '', using 'UTC' instead in Unknown on line 0
UTC
UTC
1 change: 1 addition & 0 deletions ext/date/tests/date_default_timezone_get-2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ date.timezone=
echo date_default_timezone_get(), "\n";
?>
--EXPECT--
Warning: PHP Startup: Invalid date.timezone value '', using 'UTC' instead in Unknown on line 0
UTC
2 changes: 1 addition & 1 deletion ext/date/tests/date_default_timezone_get-4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ date.timezone=Incorrect/Zone
echo date_default_timezone_get(), "\n";
?>
--EXPECTF--
Warning: date_default_timezone_get(): Invalid date.timezone value 'Incorrect/Zone', we selected the timezone 'UTC' for now. in %sdate_default_timezone_get-4.php on line %d
Warning: PHP Startup: Invalid date.timezone value 'Incorrect/Zone', using 'UTC' instead in %s on line %d
UTC
3 changes: 2 additions & 1 deletion ext/date/tests/date_default_timezone_set-1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ date.timezone=
echo date(DATE_ISO8601, $date3), "\n";
echo date(DATE_ISO8601, $date4), "\n";
?>
--EXPECT--
--EXPECTF--
Warning: PHP Startup: Invalid date.timezone value '', using 'UTC' instead in %s on line %d
America/Indiana/Knox
2005-01-12T03:00:00-0500
2005-07-12T03:00:00-0500
Expand Down
24 changes: 24 additions & 0 deletions ext/date/tests/ini_set_incorrect-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Test invalid time zone and defaults
--FILE--
<?php
echo ini_get("date.timezone"), "\n";

ini_set("date.timezone", "foo");
echo ini_get("date.timezone"), "\n";

ini_set("date.timezone", "Europe/London");
echo ini_get("date.timezone"), "\n";

ini_set("date.timezone", "Mars/Valles_Marineris");
echo ini_get("date.timezone"), "\n";
?>
--EXPECTF--
UTC

Warning: ini_set(): Invalid date.timezone value 'foo', using 'UTC' instead in %sini_set_incorrect-002.php on line %d
UTC
Europe/London

Warning: ini_set(): Invalid date.timezone value 'Mars/Valles_Marineris', using 'Europe/London' instead in %sini_set_incorrect-002.php on line %d
Europe/London
2 changes: 1 addition & 1 deletion ext/date/tests/ini_set_incorrect.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ ini_set("date.timezone", "Incorrect/Zone");

?>
--EXPECTF--
Warning: ini_set(): Invalid date.timezone value 'Incorrect/Zone', we selected the timezone 'UTC' for now. in %sini_set_incorrect.php on line %d
Warning: ini_set(): Invalid date.timezone value 'Incorrect/Zone', using 'UTC' instead in %sini_set_incorrect.php on line %d
5 changes: 3 additions & 2 deletions ext/intl/tests/dateformat_invalid_timezone.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ try {
echo $e->getMessage();
}
?>
--EXPECT--
IntlDateFormatter::__construct(): Invalid date.timezone value 'Mars/Utopia_Planitia', we selected the timezone 'UTC' for now.
--EXPECTF--
Warning: PHP Startup: Invalid date.timezone value 'Mars/Utopia_Planitia', using 'UTC' instead in %s on line %d
Wat?
2 changes: 1 addition & 1 deletion sapi/cli/tests/006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (PCRE_JIT_SUPPORT == false) {
}
?>
--INI--
date.timezone=
date.timezone=UTC
--FILE--
<?php

Expand Down