Skip to content

ext/intl: Refactor timezone parameter passing #19409

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 2 commits into from
Aug 8, 2025
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
7 changes: 2 additions & 5 deletions ext/intl/calendar/calendar.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ class IntlCalendar
private function __construct() {}

/**
* @param IntlTimeZone|DateTimeZone|string|null $timezone
* @tentative-return-type
* @alias intlcal_create_instance
*/
public static function createInstance($timezone = null, ?string $locale = null): ?IntlCalendar {}
public static function createInstance(IntlTimeZone|DateTimeZone|string|null $timezone = null, ?string $locale = null): ?IntlCalendar {}

/**
* @tentative-return-type
Expand Down Expand Up @@ -355,11 +354,9 @@ public function setSkippedWallTimeOption(int $option): true {}
public function setTime(float $timestamp): bool {}

/**
* @param IntlTimeZone|DateTimeZone|string|null $timezone
* @tentative-return-type
* @alias intlcal_set_time_zone
*/
public function setTimeZone($timezone): bool {}
public function setTimeZone(IntlTimeZone|DateTimeZone|string|null $timezone): bool {}

/**
* @tentative-return-type
Expand Down
10 changes: 5 additions & 5 deletions ext/intl/calendar/calendar_arginfo.h

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

60 changes: 45 additions & 15 deletions ext/intl/calendar/calendar_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ U_CFUNC PHP_METHOD(IntlCalendar, __construct)

U_CFUNC PHP_FUNCTION(intlcal_create_instance)
{
zval *zv_timezone = NULL;
zend_object *timezone_object = nullptr;
zend_string *timezone_string = nullptr;
char *locale_str = NULL;
size_t locale_len = 0;
TimeZone *timeZone;
UErrorCode status = U_ZERO_ERROR;
intl_error_reset(NULL);

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(zv_timezone)
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
Z_PARAM_STRING_OR_NULL(locale_str, locale_len)
ZEND_PARSE_PARAMETERS_END();

timeZone = timezone_process_timezone_argument(zv_timezone, NULL);
if (timeZone == NULL) {
TimeZone *timeZone = timezone_process_timezone_argument(timezone_object, timezone_string, nullptr);
if (timeZone == nullptr) {
RETURN_NULL();
}

Expand Down Expand Up @@ -296,26 +296,28 @@ U_CFUNC PHP_FUNCTION(intlcal_add)
RETURN_TRUE;
}

/* {{{ Set formatter's timezone. */
U_CFUNC PHP_FUNCTION(intlcal_set_time_zone)
{
zval *zv_timezone;
TimeZone *timeZone;
zend_object *timezone_object = nullptr;
zend_string *timezone_string = nullptr;

CALENDAR_METHOD_INIT_VARS;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
"Oz!", &object, Calendar_ce_ptr, &zv_timezone) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJECT_OF_CLASS(object, Calendar_ce_ptr)
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
ZEND_PARSE_PARAMETERS_END();

CALENDAR_METHOD_FETCH_OBJECT;

if (zv_timezone == NULL) {
if (timezone_object == nullptr && timezone_string == nullptr) {
RETURN_TRUE; /* the method does nothing if passed null */
}

timeZone = timezone_process_timezone_argument(zv_timezone,
CALENDAR_ERROR_P(co));
if (timeZone == NULL) {
TimeZone *timeZone = timezone_process_timezone_argument(
timezone_object, timezone_string, CALENDAR_ERROR_P(co));
if (timeZone == nullptr) {
RETURN_FALSE;
}

Expand All @@ -324,6 +326,34 @@ U_CFUNC PHP_FUNCTION(intlcal_set_time_zone)
RETURN_TRUE;
}

U_CFUNC PHP_METHOD(IntlCalendar, setTimeZone)
{
zend_object *timezone_object = nullptr;
zend_string *timezone_string = nullptr;

CALENDAR_METHOD_INIT_VARS;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
ZEND_PARSE_PARAMETERS_END();

object = ZEND_THIS;
CALENDAR_METHOD_FETCH_OBJECT;

if (timezone_object == nullptr && timezone_string == nullptr) {
RETURN_TRUE; /* the method does nothing if passed null */
}

TimeZone *timeZone = timezone_process_timezone_argument(
timezone_object, timezone_string, CALENDAR_ERROR_P(co));
if (timeZone == nullptr) {
RETURN_FALSE;
}

co->ucal->adoptTimeZone(timeZone);

RETURN_TRUE;
}

static void _php_intlcal_before_after(
UBool (Calendar::*func)(const Calendar&, UErrorCode&) const,
Expand Down
33 changes: 21 additions & 12 deletions ext/intl/calendar/gregoriancalendar_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ static bool set_gregorian_calendar_time_zone(GregorianCalendar *gcal, UErrorCode

static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
{
zval *tz_object = NULL;
zend_object *timezone_object = nullptr;
zend_string *timezone_string = nullptr;
zval args_a[6],
*args = &args_a[0];
char *locale = NULL;
Expand Down Expand Up @@ -112,15 +113,23 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool

// argument parsing
if (variant <= 2) {
if (zend_parse_parameters(MIN(ZEND_NUM_ARGS(), 2),
"|z!s!", &tz_object, &locale, &locale_len) == FAILURE) {
RETURN_THROWS();
}
}
if (variant > 2 && zend_parse_parameters(ZEND_NUM_ARGS(),
"lll|lll", &largs[0], &largs[1], &largs[2], &largs[3], &largs[4],
&largs[5]) == FAILURE) {
RETURN_THROWS();
/* These dummy variables are needed because the 2 param constructor allows trailing nulls... */
zval *dummy1, *dummy2, *dummy3, *dummy4;
ZEND_PARSE_PARAMETERS_START(0, 6)
Z_PARAM_OPTIONAL
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
Z_PARAM_STRING_OR_NULL(locale, locale_len)
Z_PARAM_ZVAL(dummy1)
Z_PARAM_ZVAL(dummy2)
Z_PARAM_ZVAL(dummy3)
Z_PARAM_ZVAL(dummy4)
ZEND_PARSE_PARAMETERS_END();
}
if (variant > 2
&& zend_parse_parameters(ZEND_NUM_ARGS(), "lll|lll",
&largs[0], &largs[1], &largs[2], &largs[3], &largs[4], &largs[5]) == FAILURE
) {
RETURN_THROWS();
}

// instantion of ICU object
Expand All @@ -134,8 +143,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool

if (variant <= 2) {
// From timezone and locale (0 to 2 arguments)
TimeZone *tz = timezone_process_timezone_argument(tz_object, NULL);
if (tz == NULL) {
TimeZone *tz = timezone_process_timezone_argument(timezone_object, timezone_string, nullptr);
if (tz == nullptr) {
// TODO: Exception should always occur already?
if (!EG(exception)) {
zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
Expand Down
10 changes: 3 additions & 7 deletions ext/intl/dateformat/dateformat.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,26 @@ class IntlDateFormatter
public const int TRADITIONAL = UNKNOWN;

/**
* @param IntlTimeZone|DateTimeZone|string|null $timezone
* @param IntlCalendar|int|null $calendar
*/
public function __construct(
?string $locale,
int $dateType = IntlDateFormatter::FULL,
int $timeType = IntlDateFormatter::FULL,
$timezone = null,
IntlTimeZone|DateTimeZone|string|null $timezone = null,
$calendar = null,
?string $pattern = null
) {}

/**
* @param IntlTimeZone|DateTimeZone|string|null $timezone
* @tentative-return-type
* @alias datefmt_create
*/
public static function create(
?string $locale,
int $dateType = IntlDateFormatter::FULL,
int $timeType = IntlDateFormatter::FULL,
$timezone = null,
IntlTimeZone|DateTimeZone|string|null $timezone = null,
IntlCalendar|int|null $calendar = null,
?string $pattern = null
): ?IntlDateFormatter {}
Expand Down Expand Up @@ -101,11 +99,9 @@ public function getCalendarObject(): IntlCalendar|false|null {}
public function getTimeZone(): IntlTimeZone|false {}

/**
* @param IntlTimeZone|DateTimeZone|string|null $timezone
* @tentative-return-type
* @alias datefmt_set_timezone
*/
public function setTimeZone($timezone): bool {}
public function setTimeZone(IntlTimeZone|DateTimeZone|string|null $timezone): bool {}

/**
* @tentative-return-type
Expand Down
12 changes: 6 additions & 6 deletions ext/intl/dateformat/dateformat_arginfo.h

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

41 changes: 33 additions & 8 deletions ext/intl/dateformat/dateformat_attrcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,46 @@ U_CFUNC PHP_FUNCTION(datefmt_get_timezone)
/* {{{ Set formatter's timezone. */
U_CFUNC PHP_FUNCTION(datefmt_set_timezone)
{
zval *timezone_zv;
TimeZone *timezone;
zend_object *timezone_object = nullptr;
zend_string *timezone_string = nullptr;

DATE_FORMAT_METHOD_INIT_VARS;

if ( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
"Oz", &object, IntlDateFormatter_ce_ptr, &timezone_zv) == FAILURE) {
RETURN_THROWS();
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJECT_OF_CLASS(object, IntlDateFormatter_ce_ptr)
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
ZEND_PARSE_PARAMETERS_END();

DATE_FORMAT_METHOD_FETCH_OBJECT;

TimeZone *timezone = timezone_process_timezone_argument(
timezone_object, timezone_string, INTL_DATA_ERROR_P(dfo));
if (timezone == nullptr) {
RETURN_FALSE;
}

fetch_datefmt(dfo)->adoptTimeZone(timezone);

RETURN_TRUE;
}

U_CFUNC PHP_METHOD(IntlDateFormatter, setTimeZone)
{
zend_object *timezone_object = nullptr;
zend_string *timezone_string = nullptr;

DATE_FORMAT_METHOD_INIT_VARS;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
ZEND_PARSE_PARAMETERS_END();

object = ZEND_THIS;
DATE_FORMAT_METHOD_FETCH_OBJECT;

timezone = timezone_process_timezone_argument(timezone_zv,
INTL_DATA_ERROR_P(dfo));
if (timezone == NULL) {
TimeZone *timezone = timezone_process_timezone_argument(
timezone_object, timezone_string, INTL_DATA_ERROR_P(dfo));
if (timezone == nullptr) {
RETURN_FALSE;
}

Expand Down
Loading