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 #include "../intl_cppshims.h"
18 
19 #include <unicode/timezone.h>
20 #include <unicode/calendar.h>
21 #include <unicode/datefmt.h>
22 
23 extern "C" {
24 #include "../php_intl.h"
25 #include "dateformat_class.h"
26 #include "dateformat_attrcpp.h"
27 #define USE_TIMEZONE_POINTER 1
28 #include "../timezone/timezone_class.h"
29 #define USE_CALENDAR_POINTER 1
30 #include "../calendar/calendar_class.h"
31 }
32 
33 #include "../intl_convertcpp.h"
34 #include "dateformat_helpers.h"
35 
fetch_datefmt(IntlDateFormatter_object * dfo)36 static inline DateFormat *fetch_datefmt(IntlDateFormatter_object *dfo) {
37 	return (DateFormat *)dfo->datef_data.udatf;
38 }
39 
40 /* {{{ proto string IntlDateFormatter::getTimeZoneId()
41  * Get formatter timezone_id. }}} */
42 /* {{{ proto string datefmt_get_timezone_id(IntlDateFormatter $mf)
43  * Get formatter timezone_id.
44  */
PHP_FUNCTION(datefmt_get_timezone_id)45 U_CFUNC PHP_FUNCTION(datefmt_get_timezone_id)
46 {
47 	zend_string *u8str;
48 	DATE_FORMAT_METHOD_INIT_VARS;
49 
50 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
51 			&object, IntlDateFormatter_ce_ptr ) == FAILURE) {
52 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,	"datefmt_get_timezone_"
53 				"id: unable to parse input params", 0);
54 		RETURN_FALSE;
55 	}
56 
57 	DATE_FORMAT_METHOD_FETCH_OBJECT;
58 
59 	UnicodeString res = UnicodeString();
60 	fetch_datefmt(dfo)->getTimeZone().getID(res);
61 	u8str = intl_charFromString(res, &INTL_DATA_ERROR_CODE(dfo));
62 	INTL_METHOD_CHECK_STATUS(dfo, "Could not convert time zone id to UTF-8");
63 
64 	RETVAL_STR(u8str);
65 }
66 
67 /* {{{ proto IntlTimeZone IntlDateFormatter::getTimeZone()
68  * Get formatter timezone. }}} */
69 /* {{{ proto IntlTimeZone datefmt_get_timezone(IntlDateFormatter $mf)
70  * Get formatter timezone.
71  */
PHP_FUNCTION(datefmt_get_timezone)72 U_CFUNC PHP_FUNCTION(datefmt_get_timezone)
73 {
74 	DATE_FORMAT_METHOD_INIT_VARS;
75 
76 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
77 			&object, IntlDateFormatter_ce_ptr ) == FAILURE) {
78 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
79 			"datefmt_get_timezone: unable to parse input params", 0 );
80 		RETURN_FALSE;
81 	}
82 
83 	DATE_FORMAT_METHOD_FETCH_OBJECT;
84 
85 	const TimeZone& tz = fetch_datefmt(dfo)->getTimeZone();
86 	TimeZone *tz_clone = tz.clone();
87 	if (tz_clone == NULL) {
88 		intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
89 				"datefmt_get_timezone: Out of memory when cloning time zone",
90 				0);
91 		RETURN_FALSE;
92 	}
93 
94 	timezone_object_construct(tz_clone, return_value, 1);
95 }
96 
97 /* {{{ proto boolean IntlDateFormatter::setTimeZone(mixed $timezone)
98  * Set formatter's timezone. */
PHP_FUNCTION(datefmt_set_timezone)99 U_CFUNC PHP_FUNCTION(datefmt_set_timezone)
100 {
101 	zval		*timezone_zv;
102 	TimeZone	*timezone;
103 
104 	DATE_FORMAT_METHOD_INIT_VARS;
105 
106 	if ( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
107 			"Oz", &object, IntlDateFormatter_ce_ptr, &timezone_zv) == FAILURE) {
108 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_set_timezone: "
109 				"unable to parse input params", 0);
110 		RETURN_FALSE;
111 	}
112 
113 	DATE_FORMAT_METHOD_FETCH_OBJECT;
114 
115 	timezone = timezone_process_timezone_argument(timezone_zv,
116 			INTL_DATA_ERROR_P(dfo), "datefmt_set_timezone");
117 	if (timezone == NULL) {
118 		RETURN_FALSE;
119 	}
120 
121 	fetch_datefmt(dfo)->adoptTimeZone(timezone);
122 }
123 
124 /* {{{ proto int IntlDateFormatter::getCalendar( )
125  * Get formatter calendar type. }}} */
126 /* {{{ proto int datefmt_get_calendar(IntlDateFormatter $mf)
127  * Get formatter calendar type.
128  */
PHP_FUNCTION(datefmt_get_calendar)129 U_CFUNC PHP_FUNCTION(datefmt_get_calendar)
130 {
131 	DATE_FORMAT_METHOD_INIT_VARS;
132 
133 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
134 			&object, IntlDateFormatter_ce_ptr ) == FAILURE) {
135 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
136 			"datefmt_get_calendar: unable to parse input params", 0);
137 		RETURN_FALSE;
138 	}
139 
140 	DATE_FORMAT_METHOD_FETCH_OBJECT;
141 
142 	if (dfo->calendar == -1) {
143 		/* an IntlCalendar was provided to the constructor */
144 		RETURN_FALSE;
145 	}
146 
147 	RETURN_LONG(dfo->calendar);
148 }
149 /* }}} */
150 
151 /* {{{ proto IntlCalendar IntlDateFormatter::getCalendarObject()
152  * Get formatter calendar. }}} */
153 /* {{{ proto IntlCalendar datefmt_get_calendar_object(IntlDateFormatter $mf)
154  * Get formatter calendar.
155  */
PHP_FUNCTION(datefmt_get_calendar_object)156 U_CFUNC PHP_FUNCTION(datefmt_get_calendar_object)
157 {
158 	DATE_FORMAT_METHOD_INIT_VARS;
159 
160 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
161 			&object, IntlDateFormatter_ce_ptr ) == FAILURE) {
162 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
163 				"datefmt_get_calendar_object: unable to parse input params",
164 				0);
165 		RETURN_FALSE;
166 	}
167 
168 	DATE_FORMAT_METHOD_FETCH_OBJECT;
169 
170 	const Calendar *cal = fetch_datefmt(dfo)->getCalendar();
171 	if (cal == NULL) {
172 		RETURN_NULL();
173 	}
174 
175 	Calendar *cal_clone = cal->clone();
176 	if (cal_clone == NULL) {
177 		intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
178 				"datefmt_get_calendar_object: Out of memory when cloning "
179 				"calendar", 0);
180 		RETURN_FALSE;
181 	}
182 
183 	calendar_object_create(return_value, cal_clone);
184 }
185 /* }}} */
186 
187 /* {{{ proto bool IntlDateFormatter::setCalendar(mixed $calendar)
188  * Set formatter's calendar. }}} */
189 /* {{{ proto bool datefmt_set_calendar(IntlDateFormatter $mf, mixed $calendar)
190  * Set formatter's calendar.
191  */
PHP_FUNCTION(datefmt_set_calendar)192 U_CFUNC PHP_FUNCTION(datefmt_set_calendar)
193 {
194 	zval	*calendar_zv;
195 	DATE_FORMAT_METHOD_INIT_VARS;
196 
197 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz",
198 			&object, IntlDateFormatter_ce_ptr, &calendar_zv) == FAILURE) {
199 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
200 			"datefmt_set_calendar: unable to parse input params", 0);
201 		RETURN_FALSE;
202 	}
203 
204 	DATE_FORMAT_METHOD_FETCH_OBJECT;
205 
206 	Calendar	*cal;
207 	zend_long		cal_type;
208 	bool		cal_owned;
209 	Locale		locale = Locale::createFromName(dfo->requested_locale);
210 	// getting the actual locale from the DateFormat is not enough
211 	// because we would have lost modifiers such as @calendar. We
212 	// must store the requested locale on object creation
213 
214 	if (datefmt_process_calendar_arg(calendar_zv, locale,
215 			"datefmt_set_calendar",	INTL_DATA_ERROR_P(dfo), cal, cal_type,
216 			cal_owned) == FAILURE) {
217 		RETURN_FALSE;
218 	}
219 
220 	if (cal_owned) {
221 		/* a non IntlCalendar was specified, we want to keep the timezone */
222 		TimeZone *old_timezone = fetch_datefmt(dfo)->getTimeZone().clone();
223 		if (old_timezone == NULL) {
224 			intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
225 					"datefmt_set_calendar: Out of memory when cloning calendar",
226 					0);
227 			delete cal;
228 			RETURN_FALSE;
229 		}
230 		cal->adoptTimeZone(old_timezone);
231 	} else {
232 		cal = cal->clone();
233 		if (cal == NULL) {
234 			intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
235 					"datefmt_set_calendar: Out of memory when cloning calendar",
236 					0);
237 			RETURN_FALSE;
238 		}
239 	}
240 
241 	fetch_datefmt(dfo)->adoptCalendar(cal);
242 
243 	dfo->calendar = cal_type;
244 
245 	RETURN_TRUE;
246 }
247 /* }}} */
248