Skip to content

Commit 22a3866

Browse files
committed
ext/intl: Timezone::getIanaID method addition.
returns the primary IANA zone ID from the provided timezone ID. Most of the time, timezone ID==IANA ID. available from icu >= 74. Close GH-13419.
1 parent ef61ed1 commit 22a3866

File tree

6 files changed

+93
-2
lines changed

6 files changed

+93
-2
lines changed

ext/intl/php_intl.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ function intltz_to_date_time_zone(IntlTimeZone $timezone): DateTimeZone|false {}
616616

617617
function intltz_use_daylight_time(IntlTimeZone $timezone): bool {}
618618

619+
#if U_ICU_VERSION_MAJOR_NUM >= 74
620+
function intltz_get_iana_id(string $timezoneId): string|false {}
621+
#endif
622+
619623
/* transliterator */
620624

621625
function transliterator_create(string $id, int $direction = Transliterator::FORWARD): ?Transliterator {}

ext/intl/php_intl_arginfo.h

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
IntlTimeZone::getIanaID
3+
--EXTENSIONS--
4+
intl
5+
--SKIPIF--
6+
<?php if (version_compare(INTL_ICU_VERSION, '74.0') < 0) die('skip for ICU >= 74.0'); ?>
7+
--FILE--
8+
<?php
9+
ini_set("intl.error_level", E_WARNING);
10+
11+
var_dump(IntlTimeZone::getIanaID("php\x81"));
12+
var_dump(IntlTimeZone::getIanaID("Galaxy/Pluto"));
13+
var_dump(IntlTimeZone::getIanaID('Europe/Dublin'));
14+
var_dump(IntlTimeZone::getIanaID('Asia/Calcutta'));
15+
var_dump(intltz_get_iana_id('Asia/Calcutta') === IntlTimeZone::getIanaID('Asia/Calcutta'));
16+
?>
17+
--EXPECTF--
18+
Warning: IntlTimeZone::getIanaID(): could not convert time zone id to UTF-16 in %s on line %d
19+
bool(false)
20+
21+
Warning: IntlTimeZone::getIanaID(): error obtaining IANA ID in %s on line %d
22+
bool(false)
23+
string(13) "Europe/Dublin"
24+
string(12) "Asia/Kolkata"
25+
bool(true)

ext/intl/timezone/timezone.stub.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ public function getErrorMessage(): string|false {}
112112
*/
113113
public static function getGMT(): IntlTimeZone {}
114114

115+
#if U_ICU_VERSION_MAJOR_NUM >= 74
116+
/**
117+
* @alias intltz_get_iana_id
118+
*/
119+
public static function getIanaID(string $timezoneId): string|false {}
120+
#endif
121+
115122
/**
116123
* @tentative-return-type
117124
* @alias intltz_get_id

ext/intl/timezone/timezone_arginfo.h

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/timezone/timezone_methods.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,37 @@ U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id)
371371
RETVAL_NEW_STR(u8str);
372372
}
373373

374+
#if U_ICU_VERSION_MAJOR_NUM >= 74
375+
U_CFUNC PHP_FUNCTION(intltz_get_iana_id)
376+
{
377+
char *str_id;
378+
size_t str_id_len;
379+
intl_error_reset(NULL);
380+
381+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
382+
&str_id, &str_id_len) == FAILURE) {
383+
RETURN_THROWS();
384+
}
385+
386+
UErrorCode status = UErrorCode();
387+
UnicodeString id;
388+
if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
389+
intl_error_set(NULL, status,
390+
"could not convert time zone id to UTF-16", 0);
391+
RETURN_FALSE;
392+
}
393+
394+
UnicodeString result;
395+
TimeZone::getIanaID(id, result, status);
396+
INTL_CHECK_STATUS(status, "error obtaining IANA ID");
397+
398+
zend_string *u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
399+
INTL_CHECK_STATUS(status,
400+
"could not convert time zone id to UTF-16");
401+
RETVAL_NEW_STR(u8str);
402+
}
403+
#endif
404+
374405
U_CFUNC PHP_FUNCTION(intltz_get_id)
375406
{
376407
TIMEZONE_METHOD_INIT_VARS;

0 commit comments

Comments
 (0)