timezone_methods.cpp 20 KB

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