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