Skip to content

Commit eea544d

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.
1 parent ed1c9d8 commit eea544d

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
?>
16+
--EXPECTF--
17+
Warning: IntlTimeZone::getIanaID(): intltz_get_iana_id: could not convert time zone id to UTF-16 in %s on line %d
18+
bool(false)
19+
20+
Warning: IntlTimeZone::getIanaID(): intltz_get_iana_id: error obtaining IANA ID in %s on line %d
21+
bool(false)
22+
string(13) "Europe/Dublin"
23+
string(12) "Asia/Kolkata"

ext/intl/timezone/timezone.stub.php

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

115+
#if U_ICU_VERSION_MAJOR_NUM >= 74
116+
/**
117+
* @tentative-return-type
118+
* @alias intltz_get_iana_id
119+
*/
120+
public static function getIanaID(string $timezoneId): string|false {}
121+
#endif
122+
115123
/**
116124
* @tentative-return-type
117125
* @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+
"intltz_get_iana_id: 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, "intltz_get_iana_id: 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+
"intltz_get_iana_id: 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)