Skip to content

Commit a9568d5

Browse files
committed
Update IntlTimeZone methods for ICU 52
Adds: string IntlTimeZone::getWindowsID(string id) string IntlTimeZone::getIDForWindowsID(string winID[, string region]) And matching procedural functions
1 parent 0f8d0df commit a9568d5

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
IntlTimeZone::getIDForWindowsID basic test
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('intl'))
6+
die('skip intl extension not enabled');
7+
--FILE--
8+
<?php
9+
10+
$tzs = array(
11+
'Gnomeregan' => array(NULL),
12+
'India Standard Time' => array(NULL),
13+
'Pacific Standard Time' => array('001', 'CA', 'MX', 'US', 'ZZ'),
14+
'Romance Standard Time' => array('001', 'BE', 'DK', 'ES', 'FR'),
15+
);
16+
17+
foreach ($tzs as $tz => $regions) {
18+
echo "** $tz\n";
19+
foreach ($regions as $region) {
20+
var_dump(IntlTimeZone::getIDForWindowsID($tz, $region));
21+
if (intl_get_error_code() != U_ZERO_ERROR) {
22+
echo "Error: ", intl_get_error_message(), "\n";
23+
}
24+
}
25+
}
26+
27+
--EXPECT--
28+
** Gnomeregan
29+
bool(false)
30+
Error: intltz_get_windows_id: Unknown windows timezone: U_ILLEGAL_ARGUMENT_ERROR
31+
** India Standard Time
32+
string(13) "Asia/Calcutta"
33+
** Pacific Standard Time
34+
string(19) "America/Los_Angeles"
35+
string(17) "America/Vancouver"
36+
string(15) "America/Tijuana"
37+
string(19) "America/Los_Angeles"
38+
string(7) "PST8PDT"
39+
** Romance Standard Time
40+
string(12) "Europe/Paris"
41+
string(15) "Europe/Brussels"
42+
string(17) "Europe/Copenhagen"
43+
string(13) "Europe/Madrid"
44+
string(12) "Europe/Paris"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
IntlTimeZone::getWindowsID basic test
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('intl'))
6+
die('skip intl extension not enabled');
7+
--FILE--
8+
<?php
9+
10+
$tzs = array(
11+
'America/Bogota',
12+
'America/Havana',
13+
'America/Indiana/Knox',
14+
'America/Los_Angeles',
15+
'Azeroth/Kalimdor/Durotar',
16+
'Africa/Casablanca',
17+
'Asia/Singapore',
18+
'Australia/Perth',
19+
'Europe/London',
20+
'Europe/Istanbul',
21+
);
22+
23+
foreach ($tzs as $tz) {
24+
var_dump(IntlTimeZone::getWindowsID($tz));
25+
if (intl_get_error_code() != U_ZERO_ERROR) {
26+
echo "Error: ", intl_get_error_message(), "\n";
27+
}
28+
}
29+
30+
--EXPECT--
31+
string(24) "SA Pacific Standard Time"
32+
string(21) "Eastern Standard Time"
33+
string(21) "Central Standard Time"
34+
string(21) "Pacific Standard Time"
35+
bool(false)
36+
Error: intltz_get_windows_id: Unknown system timezone: U_ILLEGAL_ARGUMENT_ERROR
37+
string(21) "Morocco Standard Time"
38+
string(23) "Singapore Standard Time"
39+
string(26) "W. Australia Standard Time"
40+
string(17) "GMT Standard Time"
41+
string(20) "Turkey Standard Time"

ext/intl/timezone/timezone_class.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,17 @@ ZEND_END_ARG_INFO()
439439
ZEND_BEGIN_ARG_INFO_EX(ainfo_tz_void, 0, 0, 0)
440440
ZEND_END_ARG_INFO()
441441

442+
#if U_ICU_VERSION_MAJOR_NUM >= 52
443+
ZEND_BEGIN_ARG_INFO_EX(ainfo_tz_getWindowsID, 0, ZEND_RETURN_VALUE, 1)
444+
ZEND_ARG_INFO(0, timezone)
445+
ZEND_END_ARG_INFO()
446+
447+
ZEND_BEGIN_ARG_INFO_EX(ainfo_tz_getIDForWindowsID, 0, ZEND_RETURN_VALUE, 1)
448+
ZEND_ARG_INFO(0, timezone)
449+
ZEND_ARG_INFO(0, region)
450+
ZEND_END_ARG_INFO()
451+
#endif
452+
442453
/* }}} */
443454

444455
/* {{{ TimeZone_class_functions
@@ -475,6 +486,10 @@ static zend_function_entry TimeZone_class_functions[] = {
475486
PHP_ME_MAPPING(toDateTimeZone, intltz_to_date_time_zone, ainfo_tz_void, ZEND_ACC_PUBLIC)
476487
PHP_ME_MAPPING(getErrorCode, intltz_get_error_code, ainfo_tz_void, ZEND_ACC_PUBLIC)
477488
PHP_ME_MAPPING(getErrorMessage, intltz_get_error_message, ainfo_tz_void, ZEND_ACC_PUBLIC)
489+
#if U_ICU_VERSION_MAJOR_NUM >= 52
490+
PHP_ME_MAPPING(getWindowsID, intltz_get_windows_id, ainfo_tz_getWindowsID, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
491+
PHP_ME_MAPPING(getIDForWindowsID, intltz_get_id_for_windows_id, ainfo_tz_getIDForWindowsID, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
492+
#endif
478493
PHP_FE_END
479494
};
480495
/* }}} */

ext/intl/timezone/timezone_methods.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,81 @@ U_CFUNC PHP_FUNCTION(intltz_get_error_message)
647647
message = intl_error_get_message(TIMEZONE_ERROR_P(to));
648648
RETURN_STR(message);
649649
}
650+
651+
#if U_ICU_VERSION_MAJOR_NUM >= 52
652+
/* {{{ proto string IntlTimeZone::getWindowsID(string $timezone)
653+
proto string intltz_get_windows_id(string $timezone)
654+
Translate a system timezone (e.g. "America/Los_Angeles" into a
655+
Windows Timezone (e.g. "Pacific Standard Time")
656+
*/
657+
U_CFUNC PHP_FUNCTION(intltz_get_windows_id)
658+
{
659+
zend_string *id, *winID;
660+
UnicodeString uID, uWinID;
661+
UErrorCode error;
662+
663+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &id) == FAILURE) {
664+
return;
665+
}
666+
667+
error = U_ZERO_ERROR;
668+
if (intl_stringFromChar(uID, id->val, id->len, &error) == FAILURE) {
669+
intl_error_set(NULL, error,
670+
"intltz_get_windows_id: could not convert time zone id to UTF-16", 0);
671+
RETURN_FALSE;
672+
}
673+
674+
error = U_ZERO_ERROR;
675+
TimeZone::getWindowsID(uID, uWinID, error);
676+
INTL_CHECK_STATUS(error, "intltz_get_windows_id: Unable to get timezone from windows ID");
677+
if (uWinID.length() == 0) {
678+
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
679+
"intltz_get_windows_id: Unknown system timezone", 0);
680+
RETURN_FALSE;
681+
}
682+
683+
error = U_ZERO_ERROR;
684+
winID = intl_convert_utf16_to_utf8(uWinID.getBuffer(), uWinID.length(), &error);
685+
INTL_CHECK_STATUS(error, "intltz_get_windows_id: could not convert time zone id to UTF-8");
686+
RETURN_STR(winID);
687+
}
688+
/* }}} */
689+
690+
/* {{{ proto string IntlTimeZone::getIDForWindowsID(string $timezone[, string $region = NULL])
691+
proto string intltz_get_id_for_windows_id(string $timezone[, string $region = NULL])
692+
Translate a windows timezone (e.g. "Pacific Time Zone" into a
693+
System Timezone (e.g. "America/Los_Angeles")
694+
*/
695+
U_CFUNC PHP_FUNCTION(intltz_get_id_for_windows_id)
696+
{
697+
zend_string *winID, *region = NULL, *id;
698+
UnicodeString uWinID, uID;
699+
UErrorCode error;
700+
701+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S", &winID, &region) == FAILURE) {
702+
return;
703+
}
704+
705+
error = U_ZERO_ERROR;
706+
if (intl_stringFromChar(uWinID, winID->val, winID->len, &error) == FAILURE) {
707+
intl_error_set(NULL, error,
708+
"intltz_get_id_for_windows_id: could not convert time zone id to UTF-16", 0);
709+
RETURN_FALSE;
710+
}
711+
712+
error = U_ZERO_ERROR;
713+
TimeZone::getIDForWindowsID(uWinID, region ? region->val : NULL, uID, error);
714+
INTL_CHECK_STATUS(error, "intltz_get_id_for_windows_id: Unable to get windows ID for timezone");
715+
if (uID.length() == 0) {
716+
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
717+
"intltz_get_windows_id: Unknown windows timezone", 0);
718+
RETURN_FALSE;
719+
}
720+
721+
error = U_ZERO_ERROR;
722+
id = intl_convert_utf16_to_utf8(uID.getBuffer(), uID.length(), &error);
723+
INTL_CHECK_STATUS(error, "intltz_get_id_for_windows_id: could not convert time zone id to UTF-8");
724+
RETURN_STR(id);
725+
}
726+
/* }}} */
727+
#endif

ext/intl/timezone/timezone_methods.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ PHP_FUNCTION(intltz_get_error_code);
6565

6666
PHP_FUNCTION(intltz_get_error_message);
6767

68+
#if U_ICU_VERSION_MAJOR_NUM >= 52
69+
PHP_FUNCTION(intltz_get_windows_id);
70+
PHP_FUNCTION(intltz_get_id_for_windows_id);
71+
#endif
72+
6873
#endif /* #ifndef TIMEZONE_METHODS_H */

0 commit comments

Comments
 (0)