timezone_methods.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /*
  2. +----------------------------------------------------------------------+
  3. | This source file is subject to version 3.01 of the PHP license, |
  4. | that is bundled with this package in the file LICENSE, and is |
  5. | available through the world-wide-web at the following url: |
  6. | https://www.php.net/license/3_01.txt |
  7. | If you did not receive a copy of the PHP license and are unable to |
  8. | obtain it through the world-wide-web, please send a note to |
  9. | license@php.net so we can mail you a copy immediately. |
  10. +----------------------------------------------------------------------+
  11. | Authors: Gustavo Lopes <cataphract@php.net> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include "../intl_cppshims.h"
  18. #include <unicode/locid.h>
  19. #include <unicode/timezone.h>
  20. #include <unicode/ustring.h>
  21. #include <unicode/calendar.h>
  22. #include "intl_convertcpp.h"
  23. #include "../common/common_date.h"
  24. extern "C" {
  25. #include "../php_intl.h"
  26. #define USE_TIMEZONE_POINTER 1
  27. #include "timezone_class.h"
  28. #include "intl_convert.h"
  29. #include <zend_exceptions.h>
  30. #include <ext/date/php_date.h>
  31. }
  32. #include "common/common_enum.h"
  33. using icu::Locale;
  34. using icu::Calendar;
  35. U_CFUNC PHP_METHOD(IntlTimeZone, __construct)
  36. {
  37. zend_throw_exception( NULL,
  38. "An object of this type cannot be created with the new operator",
  39. 0 );
  40. }
  41. U_CFUNC PHP_FUNCTION(intltz_create_time_zone)
  42. {
  43. char *str_id;
  44. size_t str_id_len;
  45. intl_error_reset(NULL);
  46. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str_id, &str_id_len) == FAILURE) {
  47. RETURN_THROWS();
  48. }
  49. UErrorCode status = UErrorCode();
  50. UnicodeString id = UnicodeString();
  51. if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
  52. intl_error_set(NULL, status,
  53. "intltz_create_time_zone: could not convert time zone id to UTF-16", 0);
  54. RETURN_NULL();
  55. }
  56. //guaranteed non-null; GMT if timezone cannot be understood
  57. TimeZone *tz = TimeZone::createTimeZone(id);
  58. timezone_object_construct(tz, return_value, 1);
  59. }
  60. U_CFUNC PHP_FUNCTION(intltz_from_date_time_zone)
  61. {
  62. zval *zv_timezone;
  63. TimeZone *tz;
  64. php_timezone_obj *tzobj;
  65. intl_error_reset(NULL);
  66. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O",
  67. &zv_timezone, php_date_get_timezone_ce()) == FAILURE) {
  68. RETURN_THROWS();
  69. }
  70. tzobj = Z_PHPTIMEZONE_P(zv_timezone);
  71. if (!tzobj->initialized) {
  72. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  73. "intltz_from_date_time_zone: DateTimeZone object is unconstructed",
  74. 0);
  75. RETURN_NULL();
  76. }
  77. tz = timezone_convert_datetimezone(tzobj->type, tzobj, false, NULL,
  78. "intltz_from_date_time_zone");
  79. if (tz == NULL) {
  80. RETURN_NULL();
  81. }
  82. timezone_object_construct(tz, return_value, 1);
  83. }
  84. U_CFUNC PHP_FUNCTION(intltz_create_default)
  85. {
  86. intl_error_reset(NULL);
  87. if (zend_parse_parameters_none() == FAILURE) {
  88. return;
  89. }
  90. TimeZone *tz = TimeZone::createDefault();
  91. timezone_object_construct(tz, return_value, 1);
  92. }
  93. U_CFUNC PHP_FUNCTION(intltz_get_gmt)
  94. {
  95. intl_error_reset(NULL);
  96. if (zend_parse_parameters_none() == FAILURE) {
  97. RETURN_THROWS();
  98. }
  99. timezone_object_construct(TimeZone::getGMT(), return_value, 0);
  100. }
  101. U_CFUNC PHP_FUNCTION(intltz_get_unknown)
  102. {
  103. intl_error_reset(NULL);
  104. if (zend_parse_parameters_none() == FAILURE) {
  105. RETURN_THROWS();
  106. }
  107. timezone_object_construct(&TimeZone::getUnknown(), return_value, 0);
  108. }
  109. U_CFUNC PHP_FUNCTION(intltz_create_enumeration)
  110. {
  111. zval *arg = NULL;
  112. StringEnumeration *se = NULL;
  113. intl_error_reset(NULL);
  114. /* double indirection to have the zend engine destroy the new zval that
  115. * results from separation */
  116. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
  117. RETURN_THROWS();
  118. }
  119. if (arg == NULL || Z_TYPE_P(arg) == IS_NULL) {
  120. se = TimeZone::createEnumeration();
  121. } else if (Z_TYPE_P(arg) == IS_LONG) {
  122. int_offset:
  123. if (Z_LVAL_P(arg) < (zend_long)INT32_MIN ||
  124. Z_LVAL_P(arg) > (zend_long)INT32_MAX) {
  125. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  126. "intltz_create_enumeration: value is out of range", 0);
  127. RETURN_FALSE;
  128. } else {
  129. se = TimeZone::createEnumeration((int32_t) Z_LVAL_P(arg));
  130. }
  131. } else if (Z_TYPE_P(arg) == IS_DOUBLE) {
  132. double_offset:
  133. convert_to_long(arg);
  134. goto int_offset;
  135. } else if (Z_TYPE_P(arg) == IS_OBJECT || Z_TYPE_P(arg) == IS_STRING) {
  136. zend_long lval;
  137. double dval;
  138. if (!try_convert_to_string(arg)) {
  139. RETURN_THROWS();
  140. }
  141. switch (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), &lval, &dval, 0)) {
  142. case IS_DOUBLE:
  143. zval_ptr_dtor(arg);
  144. ZVAL_DOUBLE(arg, dval);
  145. goto double_offset;
  146. case IS_LONG:
  147. zval_ptr_dtor(arg);
  148. ZVAL_LONG(arg, lval);
  149. goto int_offset;
  150. }
  151. /* else call string version */
  152. se = TimeZone::createEnumeration(Z_STRVAL_P(arg));
  153. } else {
  154. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  155. "intltz_create_enumeration: invalid argument type", 0);
  156. RETURN_FALSE;
  157. }
  158. if (se) {
  159. IntlIterator_from_StringEnumeration(se, return_value);
  160. } else {
  161. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  162. "intltz_create_enumeration: error obtaining enumeration", 0);
  163. RETVAL_FALSE;
  164. }
  165. }
  166. U_CFUNC PHP_FUNCTION(intltz_count_equivalent_ids)
  167. {
  168. char *str_id;
  169. size_t str_id_len;
  170. intl_error_reset(NULL);
  171. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
  172. &str_id, &str_id_len) == FAILURE) {
  173. RETURN_THROWS();
  174. }
  175. UErrorCode status = UErrorCode();
  176. UnicodeString id = UnicodeString();
  177. if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
  178. intl_error_set(NULL, status,
  179. "intltz_count_equivalent_ids: could not convert time zone id to UTF-16", 0);
  180. RETURN_FALSE;
  181. }
  182. int32_t result = TimeZone::countEquivalentIDs(id);
  183. RETURN_LONG((zend_long)result);
  184. }
  185. U_CFUNC PHP_FUNCTION(intltz_create_time_zone_id_enumeration)
  186. {
  187. zend_long zoneType,
  188. offset_arg;
  189. char *region = NULL;
  190. size_t region_len = 0;
  191. int32_t offset,
  192. *offsetp = NULL;
  193. bool arg3isnull = 1;
  194. intl_error_reset(NULL);
  195. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s!l!",
  196. &zoneType, &region, &region_len, &offset_arg, &arg3isnull) == FAILURE) {
  197. RETURN_THROWS();
  198. }
  199. if (zoneType != UCAL_ZONE_TYPE_ANY && zoneType != UCAL_ZONE_TYPE_CANONICAL
  200. && zoneType != UCAL_ZONE_TYPE_CANONICAL_LOCATION) {
  201. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  202. "intltz_create_time_zone_id_enumeration: bad zone type", 0);
  203. RETURN_FALSE;
  204. }
  205. if (!arg3isnull) {
  206. if (offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX) {
  207. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  208. "intltz_create_time_zone_id_enumeration: offset out of bounds", 0);
  209. RETURN_FALSE;
  210. }
  211. offset = (int32_t)offset_arg;
  212. offsetp = &offset;
  213. } //else leave offsetp NULL
  214. StringEnumeration *se;
  215. UErrorCode uec = UErrorCode();
  216. se = TimeZone::createTimeZoneIDEnumeration((USystemTimeZoneType)zoneType,
  217. region, offsetp, uec);
  218. INTL_CHECK_STATUS(uec, "intltz_create_time_zone_id_enumeration: "
  219. "Error obtaining time zone id enumeration")
  220. IntlIterator_from_StringEnumeration(se, return_value);
  221. }
  222. U_CFUNC PHP_FUNCTION(intltz_get_canonical_id)
  223. {
  224. char *str_id;
  225. size_t str_id_len;
  226. zval *is_systemid = NULL;
  227. intl_error_reset(NULL);
  228. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z",
  229. &str_id, &str_id_len, &is_systemid) == FAILURE) {
  230. RETURN_THROWS();
  231. }
  232. UErrorCode status = UErrorCode();
  233. UnicodeString id;
  234. if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
  235. intl_error_set(NULL, status,
  236. "intltz_get_canonical_id: could not convert time zone id to UTF-16", 0);
  237. RETURN_FALSE;
  238. }
  239. UnicodeString result;
  240. UBool isSystemID;
  241. TimeZone::getCanonicalID(id, result, isSystemID, status);
  242. INTL_CHECK_STATUS(status, "intltz_get_canonical_id: error obtaining canonical ID");
  243. zend_string *u8str =intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
  244. INTL_CHECK_STATUS(status,
  245. "intltz_get_canonical_id: could not convert time zone id to UTF-16");
  246. RETVAL_NEW_STR(u8str);
  247. if (is_systemid) { /* by-ref argument passed */
  248. ZVAL_DEREF(is_systemid);
  249. zval_ptr_dtor(is_systemid);
  250. ZVAL_BOOL(is_systemid, isSystemID);
  251. }
  252. }
  253. U_CFUNC PHP_FUNCTION(intltz_get_region)
  254. {
  255. char *str_id;
  256. size_t str_id_len;
  257. char outbuf[3];
  258. intl_error_reset(NULL);
  259. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
  260. &str_id, &str_id_len) == FAILURE) {
  261. RETURN_THROWS();
  262. }
  263. UErrorCode status = UErrorCode();
  264. UnicodeString id;
  265. if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
  266. intl_error_set(NULL, status,
  267. "intltz_get_region: could not convert time zone id to UTF-16", 0);
  268. RETURN_FALSE;
  269. }
  270. int32_t region_len = TimeZone::getRegion(id, outbuf, sizeof(outbuf), status);
  271. INTL_CHECK_STATUS(status, "intltz_get_region: Error obtaining region");
  272. RETURN_STRINGL(outbuf, region_len);
  273. }
  274. U_CFUNC PHP_FUNCTION(intltz_get_tz_data_version)
  275. {
  276. intl_error_reset(NULL);
  277. if (zend_parse_parameters_none() == FAILURE) {
  278. RETURN_THROWS();
  279. }
  280. UErrorCode status = UErrorCode();
  281. const char *res = TimeZone::getTZDataVersion(status);
  282. INTL_CHECK_STATUS(status, "intltz_get_tz_data_version: "
  283. "Error obtaining time zone data version");
  284. RETURN_STRING(res);
  285. }
  286. U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id)
  287. {
  288. char *str_id;
  289. size_t str_id_len;
  290. zend_long index;
  291. intl_error_reset(NULL);
  292. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &str_id, &str_id_len, &index) == FAILURE) {
  293. RETURN_THROWS();
  294. }
  295. if (index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX) {
  296. RETURN_FALSE;
  297. }
  298. UErrorCode status = UErrorCode();
  299. UnicodeString id;
  300. if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
  301. intl_error_set(NULL, status,
  302. "intltz_get_equivalent_id: could not convert time zone id to UTF-16", 0);
  303. RETURN_FALSE;
  304. }
  305. const UnicodeString result = TimeZone::getEquivalentID(id, (int32_t)index);
  306. zend_string *u8str;
  307. u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
  308. INTL_CHECK_STATUS(status, "intltz_get_equivalent_id: "
  309. "could not convert resulting time zone id to UTF-16");
  310. RETVAL_NEW_STR(u8str);
  311. }
  312. U_CFUNC PHP_FUNCTION(intltz_get_id)
  313. {
  314. TIMEZONE_METHOD_INIT_VARS;
  315. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  316. &object, TimeZone_ce_ptr) == FAILURE) {
  317. RETURN_THROWS();
  318. }
  319. TIMEZONE_METHOD_FETCH_OBJECT;
  320. UnicodeString id_us;
  321. to->utimezone->getID(id_us);
  322. zend_string *u8str;
  323. u8str = intl_convert_utf16_to_utf8(
  324. id_us.getBuffer(), id_us.length(), TIMEZONE_ERROR_CODE_P(to));
  325. INTL_METHOD_CHECK_STATUS(to, "intltz_get_id: Could not convert id to UTF-8");
  326. RETVAL_NEW_STR(u8str);
  327. }
  328. U_CFUNC PHP_FUNCTION(intltz_use_daylight_time)
  329. {
  330. TIMEZONE_METHOD_INIT_VARS;
  331. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  332. &object, TimeZone_ce_ptr) == FAILURE) {
  333. RETURN_THROWS();
  334. }
  335. TIMEZONE_METHOD_FETCH_OBJECT;
  336. RETURN_BOOL(to->utimezone->useDaylightTime());
  337. }
  338. U_CFUNC PHP_FUNCTION(intltz_get_offset)
  339. {
  340. double date;
  341. bool local;
  342. zval *rawOffsetArg,
  343. *dstOffsetArg;
  344. int32_t rawOffset,
  345. dstOffset;
  346. TIMEZONE_METHOD_INIT_VARS;
  347. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  348. "Odbz/z/", &object, TimeZone_ce_ptr, &date, &local, &rawOffsetArg,
  349. &dstOffsetArg) == FAILURE) {
  350. RETURN_THROWS();
  351. }
  352. TIMEZONE_METHOD_FETCH_OBJECT;
  353. to->utimezone->getOffset((UDate) date, (UBool) local, rawOffset, dstOffset,
  354. TIMEZONE_ERROR_CODE(to));
  355. INTL_METHOD_CHECK_STATUS(to, "intltz_get_offset: error obtaining offset");
  356. zval_ptr_dtor(rawOffsetArg);
  357. ZVAL_LONG(rawOffsetArg, rawOffset);
  358. zval_ptr_dtor(dstOffsetArg);
  359. ZVAL_LONG(dstOffsetArg, dstOffset);
  360. RETURN_TRUE;
  361. }
  362. U_CFUNC PHP_FUNCTION(intltz_get_raw_offset)
  363. {
  364. TIMEZONE_METHOD_INIT_VARS;
  365. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  366. "O", &object, TimeZone_ce_ptr) == FAILURE) {
  367. RETURN_THROWS();
  368. }
  369. TIMEZONE_METHOD_FETCH_OBJECT;
  370. RETURN_LONG(to->utimezone->getRawOffset());
  371. }
  372. U_CFUNC PHP_FUNCTION(intltz_has_same_rules)
  373. {
  374. zval *other_object;
  375. TimeZone_object *other_to;
  376. TIMEZONE_METHOD_INIT_VARS;
  377. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  378. "OO", &object, TimeZone_ce_ptr, &other_object, TimeZone_ce_ptr)
  379. == FAILURE) {
  380. RETURN_THROWS();
  381. }
  382. TIMEZONE_METHOD_FETCH_OBJECT;
  383. other_to = Z_INTL_TIMEZONE_P(other_object);
  384. if (other_to->utimezone == NULL) {
  385. intl_errors_set(&to->err, U_ILLEGAL_ARGUMENT_ERROR,
  386. "intltz_has_same_rules: The second IntlTimeZone is unconstructed", 0);
  387. RETURN_FALSE;
  388. }
  389. RETURN_BOOL(to->utimezone->hasSameRules(*other_to->utimezone));
  390. }
  391. static const TimeZone::EDisplayType display_types[] = {
  392. TimeZone::SHORT, TimeZone::LONG,
  393. TimeZone::SHORT_GENERIC, TimeZone::LONG_GENERIC,
  394. TimeZone::SHORT_GMT, TimeZone::LONG_GMT,
  395. TimeZone::SHORT_COMMONLY_USED, TimeZone::GENERIC_LOCATION
  396. };
  397. U_CFUNC PHP_FUNCTION(intltz_get_display_name)
  398. {
  399. bool daylight = 0;
  400. zend_long display_type = TimeZone::LONG;
  401. const char *locale_str = NULL;
  402. size_t dummy = 0;
  403. TIMEZONE_METHOD_INIT_VARS;
  404. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  405. "O|bls!", &object, TimeZone_ce_ptr, &daylight, &display_type,
  406. &locale_str, &dummy) == FAILURE) {
  407. RETURN_THROWS();
  408. }
  409. bool found = false;
  410. for (int i = 0; !found && i < sizeof(display_types)/sizeof(*display_types); i++) {
  411. if (display_types[i] == display_type)
  412. found = true;
  413. }
  414. if (!found) {
  415. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  416. "intltz_get_display_name: wrong display type", 0);
  417. RETURN_FALSE;
  418. }
  419. if (!locale_str) {
  420. locale_str = intl_locale_get_default();
  421. }
  422. TIMEZONE_METHOD_FETCH_OBJECT;
  423. UnicodeString result;
  424. to->utimezone->getDisplayName((UBool)daylight, (TimeZone::EDisplayType)display_type,
  425. Locale::createFromName(locale_str), result);
  426. zend_string *u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), TIMEZONE_ERROR_CODE_P(to));
  427. INTL_METHOD_CHECK_STATUS(to, "intltz_get_display_name: "
  428. "could not convert resulting time zone id to UTF-16");
  429. RETVAL_NEW_STR(u8str);
  430. }
  431. U_CFUNC PHP_FUNCTION(intltz_get_dst_savings)
  432. {
  433. TIMEZONE_METHOD_INIT_VARS;
  434. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  435. "O", &object, TimeZone_ce_ptr) == FAILURE) {
  436. RETURN_THROWS();
  437. }
  438. TIMEZONE_METHOD_FETCH_OBJECT;
  439. RETURN_LONG((zend_long)to->utimezone->getDSTSavings());
  440. }
  441. U_CFUNC PHP_FUNCTION(intltz_to_date_time_zone)
  442. {
  443. zval tmp;
  444. TIMEZONE_METHOD_INIT_VARS;
  445. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
  446. "O", &object, TimeZone_ce_ptr) == FAILURE) {
  447. RETURN_THROWS();
  448. }
  449. TIMEZONE_METHOD_FETCH_OBJECT;
  450. zval *ret = timezone_convert_to_datetimezone(to->utimezone,
  451. &TIMEZONE_ERROR(to), "intltz_to_date_time_zone", &tmp);
  452. if (ret) {
  453. ZVAL_COPY_VALUE(return_value, ret);
  454. } else {
  455. RETURN_FALSE;
  456. }
  457. }
  458. U_CFUNC PHP_FUNCTION(intltz_get_error_code)
  459. {
  460. TIMEZONE_METHOD_INIT_VARS
  461. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  462. &object, TimeZone_ce_ptr) == FAILURE) {
  463. RETURN_THROWS();
  464. }
  465. /* Fetch the object (without resetting its last error code ). */
  466. to = Z_INTL_TIMEZONE_P(object);
  467. if (to == NULL)
  468. RETURN_FALSE;
  469. RETURN_LONG((zend_long)TIMEZONE_ERROR_CODE(to));
  470. }
  471. U_CFUNC PHP_FUNCTION(intltz_get_error_message)
  472. {
  473. zend_string* message = NULL;
  474. TIMEZONE_METHOD_INIT_VARS
  475. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
  476. &object, TimeZone_ce_ptr) == FAILURE) {
  477. RETURN_THROWS();
  478. }
  479. /* Fetch the object (without resetting its last error code ). */
  480. to = Z_INTL_TIMEZONE_P(object);
  481. if (to == NULL)
  482. RETURN_FALSE;
  483. /* Return last error message. */
  484. message = intl_error_get_message(TIMEZONE_ERROR_P(to));
  485. RETURN_STR(message);
  486. }
  487. #if U_ICU_VERSION_MAJOR_NUM >= 52
  488. /* {{{ Translate a system timezone (e.g. "America/Los_Angeles" into a
  489. Windows Timezone (e.g. "Pacific Standard Time")
  490. */
  491. U_CFUNC PHP_FUNCTION(intltz_get_windows_id)
  492. {
  493. zend_string *id, *winID;
  494. UnicodeString uID, uWinID;
  495. UErrorCode error;
  496. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &id) == FAILURE) {
  497. RETURN_THROWS();
  498. }
  499. error = U_ZERO_ERROR;
  500. if (intl_stringFromChar(uID, id->val, id->len, &error) == FAILURE) {
  501. intl_error_set(NULL, error,
  502. "intltz_get_windows_id: could not convert time zone id to UTF-16", 0);
  503. RETURN_FALSE;
  504. }
  505. error = U_ZERO_ERROR;
  506. TimeZone::getWindowsID(uID, uWinID, error);
  507. INTL_CHECK_STATUS(error, "intltz_get_windows_id: Unable to get timezone from windows ID");
  508. if (uWinID.length() == 0) {
  509. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  510. "intltz_get_windows_id: Unknown system timezone", 0);
  511. RETURN_FALSE;
  512. }
  513. error = U_ZERO_ERROR;
  514. winID = intl_convert_utf16_to_utf8(uWinID.getBuffer(), uWinID.length(), &error);
  515. INTL_CHECK_STATUS(error, "intltz_get_windows_id: could not convert time zone id to UTF-8");
  516. RETURN_STR(winID);
  517. }
  518. /* }}} */
  519. /* {{{ Translate a windows timezone (e.g. "Pacific Time Zone" into a
  520. System Timezone (e.g. "America/Los_Angeles")
  521. */
  522. U_CFUNC PHP_FUNCTION(intltz_get_id_for_windows_id)
  523. {
  524. zend_string *winID, *region = NULL, *id;
  525. UnicodeString uWinID, uID;
  526. UErrorCode error;
  527. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &winID, &region) == FAILURE) {
  528. RETURN_THROWS();
  529. }
  530. error = U_ZERO_ERROR;
  531. if (intl_stringFromChar(uWinID, winID->val, winID->len, &error) == FAILURE) {
  532. intl_error_set(NULL, error,
  533. "intltz_get_id_for_windows_id: could not convert time zone id to UTF-16", 0);
  534. RETURN_FALSE;
  535. }
  536. error = U_ZERO_ERROR;
  537. TimeZone::getIDForWindowsID(uWinID, region ? region->val : NULL, uID, error);
  538. INTL_CHECK_STATUS(error, "intltz_get_id_for_windows_id: Unable to get windows ID for timezone");
  539. if (uID.length() == 0) {
  540. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  541. "intltz_get_windows_id: Unknown windows timezone", 0);
  542. RETURN_FALSE;
  543. }
  544. error = U_ZERO_ERROR;
  545. id = intl_convert_utf16_to_utf8(uID.getBuffer(), uID.length(), &error);
  546. INTL_CHECK_STATUS(error, "intltz_get_id_for_windows_id: could not convert time zone id to UTF-8");
  547. RETURN_STR(id);
  548. }
  549. /* }}} */
  550. #endif