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 <unicode/calendar.h>
27 #include "intl_convertcpp.h"
28 
29 #include "../common/common_date.h"
30 
31 extern "C" {
32 #include "../php_intl.h"
33 #define USE_TIMEZONE_POINTER 1
34 #include "timezone_class.h"
35 #include "intl_convert.h"
36 #include <zend_exceptions.h>
37 #include <ext/date/php_date.h>
38 }
39 #include "common/common_enum.h"
40 
41 using icu::Locale;
42 using icu::Calendar;
43 
PHP_METHOD(IntlTimeZone,__construct)44 U_CFUNC PHP_METHOD(IntlTimeZone, __construct)
45 {
46 	zend_throw_exception( NULL,
47 		"An object of this type cannot be created with the new operator",
48 		0 );
49 }
50 
PHP_FUNCTION(intltz_create_time_zone)51 U_CFUNC PHP_FUNCTION(intltz_create_time_zone)
52 {
53 	char	*str_id;
54 	size_t	 str_id_len;
55 	intl_error_reset(NULL);
56 
57 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str_id, &str_id_len) == FAILURE) {
58 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
59 			"intltz_create_time_zone: bad arguments", 0);
60 		RETURN_NULL();
61 	}
62 
63 	UErrorCode status = UErrorCode();
64 	UnicodeString id = UnicodeString();
65 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
66 		intl_error_set(NULL, status,
67 			"intltz_create_time_zone: could not convert time zone id to UTF-16", 0);
68 		RETURN_NULL();
69 	}
70 
71 	//guaranteed non-null; GMT if timezone cannot be understood
72 	TimeZone *tz = TimeZone::createTimeZone(id);
73 	timezone_object_construct(tz, return_value, 1);
74 }
75 
PHP_FUNCTION(intltz_from_date_time_zone)76 U_CFUNC PHP_FUNCTION(intltz_from_date_time_zone)
77 {
78 	zval				*zv_timezone;
79 	TimeZone			*tz;
80 	php_timezone_obj	*tzobj;
81 	intl_error_reset(NULL);
82 
83 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O",
84 			&zv_timezone, php_date_get_timezone_ce()) == FAILURE) {
85 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
86 			"intltz_from_date_time_zone: bad arguments", 0);
87 		RETURN_NULL();
88 	}
89 
90 	tzobj = Z_PHPTIMEZONE_P(zv_timezone);
91 	if (!tzobj->initialized) {
92 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
93 			"intltz_from_date_time_zone: DateTimeZone object is unconstructed",
94 			0);
95 		RETURN_NULL();
96 	}
97 
98 	tz = timezone_convert_datetimezone(tzobj->type, tzobj, FALSE, NULL,
99 		"intltz_from_date_time_zone");
100 	if (tz == NULL) {
101 		RETURN_NULL();
102 	}
103 
104 	timezone_object_construct(tz, return_value, 1);
105 }
106 
PHP_FUNCTION(intltz_create_default)107 U_CFUNC PHP_FUNCTION(intltz_create_default)
108 {
109 	intl_error_reset(NULL);
110 
111 	if (zend_parse_parameters_none() == FAILURE) {
112 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
113 			"intltz_create_default: bad arguments", 0);
114 		RETURN_NULL();
115 	}
116 
117 	TimeZone *tz = TimeZone::createDefault();
118 	timezone_object_construct(tz, return_value, 1);
119 }
120 
PHP_FUNCTION(intltz_get_gmt)121 U_CFUNC PHP_FUNCTION(intltz_get_gmt)
122 {
123 	intl_error_reset(NULL);
124 
125 	if (zend_parse_parameters_none() == FAILURE) {
126 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
127 			"intltz_get_gmt: bad arguments", 0);
128 		RETURN_NULL();
129 	}
130 
131 	timezone_object_construct(TimeZone::getGMT(), return_value, 0);
132 }
133 
134 #if U_ICU_VERSION_MAJOR_NUM >= 49
PHP_FUNCTION(intltz_get_unknown)135 U_CFUNC PHP_FUNCTION(intltz_get_unknown)
136 {
137 	intl_error_reset(NULL);
138 
139 	if (zend_parse_parameters_none() == FAILURE) {
140 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
141 			"intltz_get_unknown: bad arguments", 0);
142 		RETURN_NULL();
143 	}
144 
145 	timezone_object_construct(&TimeZone::getUnknown(), return_value, 0);
146 }
147 #endif
148 
PHP_FUNCTION(intltz_create_enumeration)149 U_CFUNC PHP_FUNCTION(intltz_create_enumeration)
150 {
151 	zval				*arg = NULL;
152 	StringEnumeration	*se	  = NULL;
153 	intl_error_reset(NULL);
154 
155 	/* double indirection to have the zend engine destroy the new zval that
156 	 * results from separation */
157 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
158 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
159 			"intltz_create_enumeration: bad arguments", 0);
160 		RETURN_FALSE;
161 	}
162 
163 	if (arg == NULL || Z_TYPE_P(arg) == IS_NULL) {
164 		se = TimeZone::createEnumeration();
165 	} else if (Z_TYPE_P(arg) == IS_LONG) {
166 int_offset:
167 		if (Z_LVAL_P(arg) < (zend_long)INT32_MIN ||
168 				Z_LVAL_P(arg) > (zend_long)INT32_MAX) {
169 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
170 				"intltz_create_enumeration: value is out of range", 0);
171 			RETURN_FALSE;
172 		} else {
173 			se = TimeZone::createEnumeration((int32_t) Z_LVAL_P(arg));
174 		}
175 	} else if (Z_TYPE_P(arg) == IS_DOUBLE) {
176 double_offset:
177 		convert_to_long_ex(arg);
178 		goto int_offset;
179 	} else if (Z_TYPE_P(arg) == IS_OBJECT || Z_TYPE_P(arg) == IS_STRING) {
180 		zend_long lval;
181 		double dval;
182 		convert_to_string_ex(arg);
183 		switch (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), &lval, &dval, 0)) {
184 		case IS_DOUBLE:
185 			zval_ptr_dtor(arg);
186 			ZVAL_DOUBLE(arg, dval);
187 			goto double_offset;
188 		case IS_LONG:
189 			zval_ptr_dtor(arg);
190 			ZVAL_LONG(arg, lval);
191 			goto int_offset;
192 		}
193 		/* else call string version */
194 		se = TimeZone::createEnumeration(Z_STRVAL_P(arg));
195 	} else {
196 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
197 			"intltz_create_enumeration: invalid argument type", 0);
198 		RETURN_FALSE;
199 	}
200 
201 	if (se) {
202 		IntlIterator_from_StringEnumeration(se, return_value);
203 	} else {
204 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
205 			"intltz_create_enumeration: error obtaining enumeration", 0);
206 		RETVAL_FALSE;
207 	}
208 }
209 
PHP_FUNCTION(intltz_count_equivalent_ids)210 U_CFUNC PHP_FUNCTION(intltz_count_equivalent_ids)
211 {
212 	char	*str_id;
213 	size_t	 str_id_len;
214 	intl_error_reset(NULL);
215 
216 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
217 			&str_id, &str_id_len) == FAILURE) {
218 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
219 			"intltz_count_equivalent_ids: bad arguments", 0);
220 		RETURN_FALSE;
221 	}
222 
223 	UErrorCode status = UErrorCode();
224 	UnicodeString id = UnicodeString();
225 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
226 		intl_error_set(NULL, status,
227 			"intltz_count_equivalent_ids: could not convert time zone id to UTF-16", 0);
228 		RETURN_FALSE;
229 	}
230 
231 	int32_t result = TimeZone::countEquivalentIDs(id);
232 	RETURN_LONG((zend_long)result);
233 }
234 
235 #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
PHP_FUNCTION(intltz_create_time_zone_id_enumeration)236 U_CFUNC PHP_FUNCTION(intltz_create_time_zone_id_enumeration)
237 {
238 	zend_long zoneType,
239 			  offset_arg;
240 	char	 *region		= NULL;
241 	size_t	  region_len	= 0;
242 	int32_t	  offset,
243 			 *offsetp	= NULL;
244 	zend_bool arg3isnull = 1;
245 
246 	intl_error_reset(NULL);
247 
248 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s!l!",
249 			&zoneType, &region, &region_len, &offset_arg, &arg3isnull) == FAILURE) {
250 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
251 			"intltz_create_time_zone_id_enumeration: bad arguments", 0);
252 		RETURN_FALSE;
253 	}
254 
255 	if (zoneType != UCAL_ZONE_TYPE_ANY && zoneType != UCAL_ZONE_TYPE_CANONICAL
256 			&& zoneType != UCAL_ZONE_TYPE_CANONICAL_LOCATION) {
257 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
258 			"intltz_create_time_zone_id_enumeration: bad zone type", 0);
259 		RETURN_FALSE;
260 	}
261 
262 	if (!arg3isnull) {
263 		if (offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX) {
264 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
265 				"intltz_create_time_zone_id_enumeration: offset out of bounds", 0);
266 			RETURN_FALSE;
267 		}
268 		offset = (int32_t)offset_arg;
269 		offsetp = &offset;
270 	} //else leave offsetp NULL
271 
272 	StringEnumeration *se;
273 	UErrorCode uec = UErrorCode();
274 	se = TimeZone::createTimeZoneIDEnumeration((USystemTimeZoneType)zoneType,
275 		region, offsetp, uec);
276 	INTL_CHECK_STATUS(uec, "intltz_create_time_zone_id_enumeration: "
277 		"Error obtaining time zone id enumeration")
278 
279 	IntlIterator_from_StringEnumeration(se, return_value);
280 }
281 #endif
282 
PHP_FUNCTION(intltz_get_canonical_id)283 U_CFUNC PHP_FUNCTION(intltz_get_canonical_id)
284 {
285 	char	*str_id;
286 	size_t	 str_id_len;
287 	zval	*is_systemid = NULL;
288 	intl_error_reset(NULL);
289 
290 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z",
291 			&str_id, &str_id_len, &is_systemid) == FAILURE) {
292 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
293 			"intltz_get_canonical_id: bad arguments", 0);
294 		RETURN_FALSE;
295 	}
296 
297 	UErrorCode status = UErrorCode();
298 	UnicodeString id;
299 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
300 		intl_error_set(NULL, status,
301 			"intltz_get_canonical_id: could not convert time zone id to UTF-16", 0);
302 		RETURN_FALSE;
303 	}
304 
305 	UnicodeString result;
306 	UBool isSystemID;
307 	TimeZone::getCanonicalID(id, result, isSystemID, status);
308 	INTL_CHECK_STATUS(status, "intltz_get_canonical_id: error obtaining canonical ID");
309 
310 	zend_string *u8str =intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
311 	INTL_CHECK_STATUS(status,
312 		"intltz_get_canonical_id: could not convert time zone id to UTF-16");
313 	RETVAL_NEW_STR(u8str);
314 
315 	if (is_systemid) { /* by-ref argument passed */
316 		ZVAL_DEREF(is_systemid);
317 		zval_ptr_dtor(is_systemid);
318 		ZVAL_BOOL(is_systemid, isSystemID);
319 	}
320 }
321 
322 #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
PHP_FUNCTION(intltz_get_region)323 U_CFUNC PHP_FUNCTION(intltz_get_region)
324 {
325 	char	*str_id;
326 	size_t	 str_id_len;
327 	char	 outbuf[3];
328 	intl_error_reset(NULL);
329 
330 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
331 			&str_id, &str_id_len) == FAILURE) {
332 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
333 			"intltz_get_region: bad arguments", 0);
334 		RETURN_FALSE;
335 	}
336 
337 	UErrorCode status = UErrorCode();
338 	UnicodeString id;
339 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
340 		intl_error_set(NULL, status,
341 			"intltz_get_region: could not convert time zone id to UTF-16", 0);
342 		RETURN_FALSE;
343 	}
344 
345 	int32_t region_len = TimeZone::getRegion(id, outbuf, sizeof(outbuf), status);
346 	INTL_CHECK_STATUS(status, "intltz_get_region: Error obtaining region");
347 
348 	RETURN_STRINGL(outbuf, region_len);
349 }
350 #endif
351 
PHP_FUNCTION(intltz_get_tz_data_version)352 U_CFUNC PHP_FUNCTION(intltz_get_tz_data_version)
353 {
354 	intl_error_reset(NULL);
355 
356 	if (zend_parse_parameters_none() == FAILURE) {
357 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
358 			"intltz_get_tz_data_version: bad arguments", 0);
359 		RETURN_FALSE;
360 	}
361 
362 	UErrorCode status = UErrorCode();
363 	const char *res = TimeZone::getTZDataVersion(status);
364 	INTL_CHECK_STATUS(status, "intltz_get_tz_data_version: "
365 		"Error obtaining time zone data version");
366 
367 	RETURN_STRING(res);
368 }
369 
PHP_FUNCTION(intltz_get_equivalent_id)370 U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id)
371 {
372 	char	   *str_id;
373 	size_t		str_id_len;
374 	zend_long	index;
375 	intl_error_reset(NULL);
376 
377 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl",
378 			&str_id, &str_id_len, &index) == FAILURE ||
379 			index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX) {
380 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
381 			"intltz_get_equivalent_id: bad arguments", 0);
382 		RETURN_FALSE;
383 	}
384 
385 	UErrorCode status = UErrorCode();
386 	UnicodeString id;
387 	if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) {
388 		intl_error_set(NULL, status,
389 			"intltz_get_equivalent_id: could not convert time zone id to UTF-16", 0);
390 		RETURN_FALSE;
391 	}
392 
393 	const UnicodeString result = TimeZone::getEquivalentID(id, (int32_t)index);
394 	zend_string *u8str;
395 
396 	u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status);
397 	INTL_CHECK_STATUS(status, "intltz_get_equivalent_id: "
398 		"could not convert resulting time zone id to UTF-16");
399 	RETVAL_NEW_STR(u8str);
400 }
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 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
409 			"intltz_get_id: bad arguments", 0);
410 		RETURN_FALSE;
411 	}
412 
413 	TIMEZONE_METHOD_FETCH_OBJECT;
414 
415 	UnicodeString id_us;
416 	to->utimezone->getID(id_us);
417 
418 	zend_string *u8str;
419 
420 	u8str = intl_convert_utf16_to_utf8(
421 		id_us.getBuffer(), id_us.length(), TIMEZONE_ERROR_CODE_P(to));
422 	INTL_METHOD_CHECK_STATUS(to, "intltz_get_id: Could not convert id to UTF-8");
423 
424 	RETVAL_NEW_STR(u8str);
425 }
426 
PHP_FUNCTION(intltz_use_daylight_time)427 U_CFUNC PHP_FUNCTION(intltz_use_daylight_time)
428 {
429 	TIMEZONE_METHOD_INIT_VARS;
430 
431 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
432 			&object, TimeZone_ce_ptr) == FAILURE) {
433 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
434 			"intltz_use_daylight_time: bad arguments", 0);
435 		RETURN_FALSE;
436 	}
437 
438 	TIMEZONE_METHOD_FETCH_OBJECT;
439 
440 	RETURN_BOOL(to->utimezone->useDaylightTime());
441 }
442 
PHP_FUNCTION(intltz_get_offset)443 U_CFUNC PHP_FUNCTION(intltz_get_offset)
444 {
445 	double		date;
446 	zend_bool	local;
447 	zval		*rawOffsetArg,
448 				*dstOffsetArg;
449 	int32_t		rawOffset,
450 				dstOffset;
451 	TIMEZONE_METHOD_INIT_VARS;
452 
453 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
454 			"Odbz/z/", &object, TimeZone_ce_ptr, &date, &local, &rawOffsetArg,
455 			&dstOffsetArg) == FAILURE) {
456 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
457 			"intltz_get_offset: bad arguments", 0);
458 		RETURN_FALSE;
459 	}
460 
461 	TIMEZONE_METHOD_FETCH_OBJECT;
462 
463 	to->utimezone->getOffset((UDate) date, (UBool) local, rawOffset, dstOffset,
464 		TIMEZONE_ERROR_CODE(to));
465 
466 	INTL_METHOD_CHECK_STATUS(to, "intltz_get_offset: error obtaining offset");
467 
468 	zval_ptr_dtor(rawOffsetArg);
469 	ZVAL_LONG(rawOffsetArg, rawOffset);
470 	zval_ptr_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