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 (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 
106 /* {{{ Get formatter calendar type. */
PHP_FUNCTION(datefmt_get_calendar)107 U_CFUNC PHP_FUNCTION(datefmt_get_calendar)
108 {
109 	DATE_FORMAT_METHOD_INIT_VARS;
110 
111 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
112 			&object, IntlDateFormatter_ce_ptr ) == FAILURE) {
113 		RETURN_THROWS();
114 	}
115 
116 	DATE_FORMAT_METHOD_FETCH_OBJECT;
117 
118 	if (dfo->calendar == -1) {
119 		/* an IntlCalendar was provided to the constructor */
120 		RETURN_FALSE;
121 	}
122 
123 	RETURN_LONG(dfo->calendar);
124 }
125 /* }}} */
126 
127 /* {{{ Get formatter calendar. */
PHP_FUNCTION(datefmt_get_calendar_object)128 U_CFUNC PHP_FUNCTION(datefmt_get_calendar_object)
129 {
130 	DATE_FORMAT_METHOD_INIT_VARS;
131 
132 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
133 			&object, IntlDateFormatter_ce_ptr ) == FAILURE) {
134 		RETURN_THROWS();
135 	}
136 
137 	DATE_FORMAT_METHOD_FETCH_OBJECT;
138 
139 	const Calendar *cal = fetch_datefmt(dfo)->getCalendar();
140 	if (cal == NULL) {
141 		RETURN_NULL();
142 	}
143 
144 	Calendar *cal_clone = cal->clone();
145 	if (cal_clone == NULL) {
146 		intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
147 				"datefmt_get_calendar_object: Out of memory when cloning "
148 				"calendar", 0);
149 		RETURN_FALSE;
150 	}
151 
152 	calendar_object_create(return_value, cal_clone);
153 }
154 /* }}} */
155 
156 /* {{{ Set formatter's calendar. */
PHP_FUNCTION(datefmt_set_calendar)157 U_CFUNC PHP_FUNCTION(datefmt_set_calendar)
158 {
159 	zend_object *calendar_obj;
160 	zend_long calendar_long;
161 	bool calendar_is_null;
162 	DATE_FORMAT_METHOD_INIT_VARS;
163 
164 	object = getThis();
165 
166 	if (object) {
167 		ZEND_PARSE_PARAMETERS_START(1, 1)
168 			Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL(calendar_obj, Calendar_ce_ptr, calendar_long, calendar_is_null)
169 		ZEND_PARSE_PARAMETERS_END();
170 	} else {
171 		ZEND_PARSE_PARAMETERS_START(2, 2)
172 			Z_PARAM_OBJECT_OF_CLASS(object, IntlDateFormatter_ce_ptr)
173 			Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL(calendar_obj, Calendar_ce_ptr, calendar_long, calendar_is_null)
174 		ZEND_PARSE_PARAMETERS_END();
175 	}
176 
177 	DATE_FORMAT_METHOD_FETCH_OBJECT;
178 
179 	Calendar	*cal;
180 	zend_long		cal_type;
181 	bool		cal_owned;
182 	Locale		locale = Locale::createFromName(dfo->requested_locale);
183 	// getting the actual locale from the DateFormat is not enough
184 	// because we would have lost modifiers such as @calendar. We
185 	// must store the requested locale on object creation
186 
187 	if (datefmt_process_calendar_arg(calendar_obj, calendar_long, calendar_is_null, locale,
188 			"datefmt_set_calendar",	INTL_DATA_ERROR_P(dfo), cal, cal_type, cal_owned) == FAILURE
189 	) {
190 		RETURN_FALSE;
191 	}
192 
193 	if (cal_owned) {
194 		/* a non IntlCalendar was specified, we want to keep the timezone */
195 		TimeZone *old_timezone = fetch_datefmt(dfo)->getTimeZone().clone();
196 		if (old_timezone == NULL) {
197 			intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
198 					"datefmt_set_calendar: Out of memory when cloning calendar",
199 					0);
200 			delete cal;
201 			RETURN_FALSE;
202 		}
203 		cal->adoptTimeZone(old_timezone);
204 	} else {
205 		cal = cal->clone();
206 		if (cal == NULL) {
207 			intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
208 					"datefmt_set_calendar: Out of memory when cloning calendar",
209 					0);
210 			RETURN_FALSE;
211 		}
212 	}
213 
214 	fetch_datefmt(dfo)->adoptCalendar(cal);
215 
216 	dfo->calendar = cal_type;
217 
218 	RETURN_TRUE;
219 }
220 /* }}} */
221