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 			"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 			"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 				"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 			"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 			"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 			"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 			"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 				"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, "error obtaining time zone id enumeration")
258 
259 	IntlIterator_from_StringEnumeration(se, return_value);
260 }
261 
PHP_FUNCTION(intltz_get_canonical_id)262 U_CFUNC PHP_FUNCTION(intltz_get_canonical_id)
263 {
264 	char	*str_id;
265 	size_t	 str_id_len;
266 	zval	*is_systemid = NULL;
267 	intl_error_reset(NULL);
268 
269 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z",
270 			&str_id, &str_id_len, &is_systemid) == FAILURE) {
271 		RETURN_THROWS();
272 	}
273 
274 	UErrorCode status = UErrorCode();
275 	UnicodeString id;
276 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
277 		intl_error_set(NULL, status,
278 			"could not convert time zone id to UTF-16", 0);
279 		RETURN_FALSE;
280 	}
281 
282 	UnicodeString result;
283 	UBool isSystemID;
284 	TimeZone::getCanonicalID(id, result, isSystemID, status);
285 	INTL_CHECK_STATUS(status, "error obtaining canonical ID");
286 
287 	zend_string *u8str =intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
288 	INTL_CHECK_STATUS(status,
289 		"could not convert time zone id to UTF-16");
290 	RETVAL_NEW_STR(u8str);
291 
292 	if (is_systemid) { /* by-ref argument passed */
293 		ZVAL_DEREF(is_systemid);
294 		zval_ptr_dtor(is_systemid);
295 		ZVAL_BOOL(is_systemid, isSystemID);
296 	}
297 }
298 
PHP_FUNCTION(intltz_get_region)299 U_CFUNC PHP_FUNCTION(intltz_get_region)
300 {
301 	char	*str_id;
302 	size_t	 str_id_len;
303 	char	 outbuf[3];
304 	intl_error_reset(NULL);
305 
306 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
307 			&str_id, &str_id_len) == FAILURE) {
308 		RETURN_THROWS();
309 	}
310 
311 	UErrorCode status = UErrorCode();
312 	UnicodeString id;
313 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
314 		intl_error_set(NULL, status,
315 			"could not convert time zone id to UTF-16", 0);
316 		RETURN_FALSE;
317 	}
318 
319 	int32_t region_len = TimeZone::getRegion(id, outbuf, sizeof(outbuf), status);
320 	INTL_CHECK_STATUS(status, "error obtaining region");
321 
322 	RETURN_STRINGL(outbuf, region_len);
323 }
324 
PHP_FUNCTION(intltz_get_tz_data_version)325 U_CFUNC PHP_FUNCTION(intltz_get_tz_data_version)
326 {
327 	intl_error_reset(NULL);
328 
329 	if (zend_parse_parameters_none() == FAILURE) {
330 		RETURN_THROWS();
331 	}
332 
333 	UErrorCode status = UErrorCode();
334 	const char *res = TimeZone::getTZDataVersion(status);
335 	INTL_CHECK_STATUS(status, "error obtaining time zone data version");
336 
337 	RETURN_STRING(res);
338 }
339 
PHP_FUNCTION(intltz_get_equivalent_id)340 U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id)
341 {
342 	char	   *str_id;
343 	size_t		str_id_len;
344 	zend_long	index;
345 	intl_error_reset(NULL);
346 
347 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &str_id, &str_id_len, &index) == FAILURE) {
348 		RETURN_THROWS();
349 	}
350 
351 	if (UNEXPECTED(index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX)) {
352 		RETURN_FALSE;
353 	}
354 
355 	UErrorCode status = UErrorCode();
356 	UnicodeString id;
357 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
358 		intl_error_set(NULL, status,
359 			"could not convert time zone id to UTF-16", 0);
360 		RETURN_FALSE;
361 	}
362 
363 	const UnicodeString result = TimeZone::getEquivalentID(id, (int32_t)index);
364 	zend_string *u8str;
365 
366 	u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
367 	INTL_CHECK_STATUS(status, "could not convert resulting time zone id to UTF-16");
368 	RETVAL_NEW_STR(u8str);
369 }
370 
371 #if U_ICU_VERSION_MAJOR_NUM >= 74
PHP_FUNCTION(intltz_get_iana_id)372 U_CFUNC PHP_FUNCTION(intltz_get_iana_id)
373 {
374 	char	*str_id;
375 	size_t	 str_id_len;
376 	intl_error_reset(NULL);
377 
378 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
379 			&str_id, &str_id_len) == FAILURE) {
380 		RETURN_THROWS();
381 	}
382 
383 	UErrorCode status = UErrorCode();
384 	UnicodeString id;
385 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
386 		intl_error_set(NULL, status,
387 			"could not convert time zone id to UTF-16", 0);
388 		RETURN_FALSE;
389 	}
390 
391 	UnicodeString result;
392 	TimeZone::getIanaID(id, result, status);
393 	INTL_CHECK_STATUS(status, "error obtaining IANA ID");
394 
395 	zend_string *u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
396 	INTL_CHECK_STATUS(status,
397 		"could not convert time zone id to UTF-16");
398 	RETVAL_NEW_STR(u8str);
399 }
400 #endif
401 
PHP_FUNCTION(intltz_get_id)402 U_CFUNC PHP_FUNCTION(intltz_get_id)
403 {
404 	TIMEZONE_METHOD_INIT_VARS;
405 
406 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
407 			&object, TimeZone_ce_ptr) == FAILURE) {
408 		RETURN_THROWS();
409 	}
410 
411 	TIMEZONE_METHOD_FETCH_OBJECT;
412 
413 	UnicodeString id_us;
414 	to->utimezone->getID(id_us);
415 
416 	zend_string *u8str;
417 
418 	u8str = intl_convert_utf16_to_utf8(
419 		id_us.getBuffer(), id_us.length(), TIMEZONE_ERROR_CODE_P(to));
420 	INTL_METHOD_CHECK_STATUS(to, "could not convert id to UTF-8");
421 
422 	RETVAL_NEW_STR(u8str);
423 }
424 
PHP_FUNCTION(intltz_use_daylight_time)425 U_CFUNC PHP_FUNCTION(intltz_use_daylight_time)
426 {
427 	TIMEZONE_METHOD_INIT_VARS;
428 
429 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
430 			&object, TimeZone_ce_ptr) == FAILURE) {
431 		RETURN_THROWS();
432 	}
433 
434 	TIMEZONE_METHOD_FETCH_OBJECT;
435 
436 	RETURN_BOOL(to->utimezone->useDaylightTime());
437 }
438 
PHP_FUNCTION(intltz_get_offset)439 U_CFUNC PHP_FUNCTION(intltz_get_offset)
440 {
441 	double		date;
442 	bool	local;
443 	zval		*rawOffsetArg,
444 				*dstOffsetArg;
445 	int32_t		rawOffset,
446 				dstOffset;
447 	TIMEZONE_METHOD_INIT_VARS;
448 
449 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
450 			"Odbz/z/", &object, TimeZone_ce_ptr, &date, &local, &rawOffsetArg,
451 			&dstOffsetArg) == FAILURE) {
452 		RETURN_THROWS();
453 	}
454 
455 	TIMEZONE_METHOD_FETCH_OBJECT;
456 
457 	to->utimezone->getOffset((UDate) date, (UBool) local, rawOffset, dstOffset,
458 		TIMEZONE_ERROR_CODE(to));
459 
460 	INTL_METHOD_CHECK_STATUS(to, "error obtaining offset");
461 
462 	zval_ptr_dtor(rawOffsetArg);
463 	ZVAL_LONG(rawOffsetArg, rawOffset);
464 	zval_ptr_dtor(dstOffsetArg);
465 	ZVAL_LONG(dstOffsetArg, dstOffset);
466 
467 	RETURN_TRUE;
468 }
469 
PHP_FUNCTION(intltz_get_raw_offset)470 U_CFUNC PHP_FUNCTION(intltz_get_raw_offset)
471 {
472 	TIMEZONE_METHOD_INIT_VARS;
473 
474 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
475 			"O", &object, TimeZone_ce_ptr) == FAILURE) {
476 		RETURN_THROWS();
477 	}
478 
479 	TIMEZONE_METHOD_FETCH_OBJECT;
480 
481 	RETURN_LONG(to->utimezone->getRawOffset());
482 }
483 
PHP_FUNCTION(intltz_has_same_rules)484 U_CFUNC PHP_FUNCTION(intltz_has_same_rules)
485 {
486 	zval			*other_object;
487 	TimeZone_object	*other_to;
488 	TIMEZONE_METHOD_INIT_VARS;
489 
490 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
491 			"OO", &object, TimeZone_ce_ptr, &other_object, TimeZone_ce_ptr)
492 			== FAILURE) {
493 		RETURN_THROWS();
494 	}
495 	TIMEZONE_METHOD_FETCH_OBJECT;
496 	other_to = Z_INTL_TIMEZONE_P(other_object);
497 	if (other_to->utimezone == NULL) {
498 		intl_errors_set(&to->err, U_ILLEGAL_ARGUMENT_ERROR,
499 			"The second IntlTimeZone is unconstructed", 0);
500 		RETURN_FALSE;
501 	}
502 
503 	RETURN_BOOL(to->utimezone->hasSameRules(*other_to->utimezone));
504 }
505 
506 static constexpr TimeZone::EDisplayType display_types[] = {
507 	TimeZone::SHORT,				TimeZone::LONG,
508 	TimeZone::SHORT_GENERIC,		TimeZone::LONG_GENERIC,
509 	TimeZone::SHORT_GMT,			TimeZone::LONG_GMT,
510 	TimeZone::SHORT_COMMONLY_USED,	TimeZone::GENERIC_LOCATION
511 };
512 
PHP_FUNCTION(intltz_get_display_name)513 U_CFUNC PHP_FUNCTION(intltz_get_display_name)
514 {
515 	bool	daylight		= 0;
516 	zend_long	display_type	= TimeZone::LONG;
517 	const char *locale_str		= NULL;
518 	size_t		dummy			= 0;
519 	TIMEZONE_METHOD_INIT_VARS;
520 
521 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
522 			"O|bls!", &object, TimeZone_ce_ptr, &daylight, &display_type,
523 			&locale_str, &dummy) == FAILURE) {
524 		RETURN_THROWS();
525 	}
526 
527 	bool found = false;
528 	for (int i = 0; !found && i < sizeof(display_types)/sizeof(*display_types); i++) {
529 		if (display_types[i] == display_type)
530 			found = true;
531 	}
532 	if (!found) {
533 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
534 			"wrong display type", 0);
535 		RETURN_FALSE;
536 	}
537 
538 	if (!locale_str) {
539 		locale_str = intl_locale_get_default();
540 	}
541 
542 	TIMEZONE_METHOD_FETCH_OBJECT;
543 
544 	UnicodeString result;
545 	to->utimezone->getDisplayName((UBool)daylight, (TimeZone::EDisplayType)display_type,
546 		Locale::createFromName(locale_str), result);
547 
548 	zend_string *u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), TIMEZONE_ERROR_CODE_P(to));
549 	INTL_METHOD_CHECK_STATUS(to, "could not convert resulting time zone id to UTF-16");
550 
551 	RETVAL_NEW_STR(u8str);
552 }
553 
PHP_FUNCTION(intltz_get_dst_savings)554 U_CFUNC PHP_FUNCTION(intltz_get_dst_savings)
555 {
556 	TIMEZONE_METHOD_INIT_VARS;
557 
558 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
559 			"O", &object, TimeZone_ce_ptr) == FAILURE) {
560 		RETURN_THROWS();
561 	}
562 
563 	TIMEZONE_METHOD_FETCH_OBJECT;
564 
565 	RETURN_LONG((zend_long)to->utimezone->getDSTSavings());
566 }
567 
PHP_FUNCTION(intltz_to_date_time_zone)568 U_CFUNC PHP_FUNCTION(intltz_to_date_time_zone)
569 {
570 	zval tmp;
571 	TIMEZONE_METHOD_INIT_VARS;
572 
573 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
574 			"O", &object, TimeZone_ce_ptr) == FAILURE) {
575 		RETURN_THROWS();
576 	}
577 
578 	TIMEZONE_METHOD_FETCH_OBJECT;
579 
580 	zval *ret = timezone_convert_to_datetimezone(to->utimezone,
581 		&TIMEZONE_ERROR(to), "intltz_to_date_time_zone", &tmp);
582 
583 	if (ret) {
584 		ZVAL_COPY_VALUE(return_value, ret);
585 	} else {
586 		RETURN_FALSE;
587 	}
588 }
589 
PHP_FUNCTION(intltz_get_error_code)590 U_CFUNC PHP_FUNCTION(intltz_get_error_code)
591 {
592 	TIMEZONE_METHOD_INIT_VARS
593 
594 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
595 			&object, TimeZone_ce_ptr) == FAILURE) {
596 		RETURN_THROWS();
597 	}
598 
599 	/* Fetch the object (without resetting its last error code ). */
600 	to = Z_INTL_TIMEZONE_P(object);
601 	if (to == NULL)
602 		RETURN_FALSE;
603 
604 	RETURN_LONG((zend_long)TIMEZONE_ERROR_CODE(to));
605 }
606 
PHP_FUNCTION(intltz_get_error_message)607 U_CFUNC PHP_FUNCTION(intltz_get_error_message)
608 {
609 	zend_string* message = NULL;
610 	TIMEZONE_METHOD_INIT_VARS
611 
612 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
613 			&object, TimeZone_ce_ptr) == FAILURE) {
614 		RETURN_THROWS();
615 	}
616 
617 
618 	/* Fetch the object (without resetting its last error code ). */
619 	to = Z_INTL_TIMEZONE_P(object);
620 	if (to == NULL)
621 		RETURN_FALSE;
622 
623 	/* Return last error message. */
624 	message = intl_error_get_message(TIMEZONE_ERROR_P(to));
625 	RETURN_STR(message);
626 }
627 
628 #if U_ICU_VERSION_MAJOR_NUM >= 52
629 /* {{{ Translate a system timezone (e.g. "America/Los_Angeles" into a
630 Windows Timezone (e.g. "Pacific Standard Time")
631  */
PHP_FUNCTION(intltz_get_windows_id)632 U_CFUNC PHP_FUNCTION(intltz_get_windows_id)
633 {
634 	zend_string *id, *winID;
635 	UnicodeString uID, uWinID;
636 	UErrorCode error;
637 
638 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &id) == FAILURE) {
639 		RETURN_THROWS();
640 	}
641 
642 	error = U_ZERO_ERROR;
643 	if (intl_stringFromChar(uID, id->val, id->len, &error) == FAILURE) {
644 		intl_error_set(NULL, error,
645 		               "could not convert time zone id to UTF-16", 0);
646 		RETURN_FALSE;
647 	}
648 
649 	error = U_ZERO_ERROR;
650 	TimeZone::getWindowsID(uID, uWinID, error);
651 	INTL_CHECK_STATUS(error, "intltz_get_windows_id: Unable to get timezone from windows ID");
652 	if (uWinID.length() == 0) {
653 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
654 		               "unknown system timezone", 0);
655 		RETURN_FALSE;
656 	}
657 
658 	error = U_ZERO_ERROR;
659 	winID = intl_convert_utf16_to_utf8(uWinID.getBuffer(), uWinID.length(), &error);
660 	INTL_CHECK_STATUS(error, "could not convert time zone id to UTF-8");
661 	RETURN_STR(winID);
662 }
663 /* }}} */
664 
665 /* {{{ Translate a windows timezone (e.g. "Pacific Time Zone" into a
666 System Timezone (e.g. "America/Los_Angeles")
667  */
PHP_FUNCTION(intltz_get_id_for_windows_id)668 U_CFUNC PHP_FUNCTION(intltz_get_id_for_windows_id)
669 {
670 	zend_string *winID, *region = NULL, *id;
671 	UnicodeString uWinID, uID;
672 	UErrorCode error;
673 
674 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &winID, &region) == FAILURE) {
675 		RETURN_THROWS();
676 	}
677 
678 	error = U_ZERO_ERROR;
679 	if (intl_stringFromChar(uWinID, winID->val, winID->len, &error) == FAILURE) {
680 		intl_error_set(NULL, error,
681 		               "could not convert time zone id to UTF-16", 0);
682 		RETURN_FALSE;
683 	}
684 
685 	error = U_ZERO_ERROR;
686 	TimeZone::getIDForWindowsID(uWinID, region ? region->val : NULL, uID, error);
687 	INTL_CHECK_STATUS(error, "unable to get windows ID for timezone");
688 	if (uID.length() == 0) {
689 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
690 		               "unknown windows timezone", 0);
691 		RETURN_FALSE;
692 	}
693 
694 	error = U_ZERO_ERROR;
695 	id = intl_convert_utf16_to_utf8(uID.getBuffer(), uID.length(), &error);
696 	INTL_CHECK_STATUS(error, "could not convert time zone id to UTF-8");
697 	RETURN_STR(id);
698 }
699 /* }}} */
700 #endif
701