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