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