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/calendar.h>
18 #include <unicode/gregocal.h>
19 #include <unicode/datefmt.h>
20 #include <unicode/smpdtfmt.h>
21 #include <unicode/locid.h>
22
23 #include "../intl_convertcpp.h"
24
25 extern "C" {
26 #include "../php_intl.h"
27 #include "../locale/locale.h"
28 #define USE_CALENDAR_POINTER 1
29 #include "../calendar/calendar_class.h"
30 #include <ext/date/php_date.h>
31 #include "../common/common_date.h"
32 }
33
34 using icu::Locale;
35 using icu::DateFormat;
36 using icu::GregorianCalendar;
37 using icu::StringPiece;
38 using icu::SimpleDateFormat;
39
40 static constexpr DateFormat::EStyle valid_styles[] = {
41 DateFormat::kNone,
42 DateFormat::kFull,
43 DateFormat::kLong,
44 DateFormat::kMedium,
45 DateFormat::kShort,
46 DateFormat::kFullRelative,
47 DateFormat::kLongRelative,
48 DateFormat::kMediumRelative,
49 DateFormat::kShortRelative,
50 };
51
valid_format(zval * z)52 static bool valid_format(zval *z) {
53 if (Z_TYPE_P(z) == IS_LONG) {
54 zend_long lval = Z_LVAL_P(z);
55 for (int i = 0; i < sizeof(valid_styles) / sizeof(*valid_styles); i++) {
56 if ((zend_long)valid_styles[i] == lval) {
57 return true;
58 }
59 }
60 }
61
62 return false;
63 }
64
PHP_FUNCTION(datefmt_format_object)65 U_CFUNC PHP_FUNCTION(datefmt_format_object)
66 {
67 zval *object,
68 *format = NULL;
69 char *locale_str = NULL;
70 size_t locale_len;
71 bool pattern = false;
72 UDate date;
73 TimeZone *timeZone = NULL;
74 UErrorCode status = U_ZERO_ERROR;
75 DateFormat *df = NULL;
76 Calendar *cal = NULL;
77 DateFormat::EStyle dateStyle = DateFormat::kDefault,
78 timeStyle = DateFormat::kDefault;
79
80 ZEND_PARSE_PARAMETERS_START(1, 3)
81 Z_PARAM_OBJECT(object)
82 Z_PARAM_OPTIONAL
83 Z_PARAM_ZVAL(format)
84 Z_PARAM_STRING_OR_NULL(locale_str, locale_len)
85 ZEND_PARSE_PARAMETERS_END();
86
87 if (!locale_str) {
88 locale_str = (char *)intl_locale_get_default();
89 }
90
91 if (format == NULL || Z_TYPE_P(format) == IS_NULL) {
92 //nothing
93 } else if (Z_TYPE_P(format) == IS_ARRAY) {
94 HashTable *ht = Z_ARRVAL_P(format);
95 if (zend_hash_num_elements(ht) != 2) {
96 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
97 "datefmt_format_object: bad format; if array, it must have "
98 "two elements", 0);
99 RETURN_FALSE;
100 }
101
102 uint32_t idx = 0;
103 zval *z;
104 ZEND_HASH_FOREACH_VAL(ht, z) {
105 if (!valid_format(z)) {
106 if (idx == 0) {
107 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
108 "datefmt_format_object: bad format; the date format (first "
109 "element of the array) is not valid", 0);
110 } else {
111 ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
112 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
113 "datefmt_format_object: bad format; the time format (second "
114 "element of the array) is not valid", 0);
115 }
116 RETURN_FALSE;
117 }
118 if (idx == 0) {
119 dateStyle = (DateFormat::EStyle)Z_LVAL_P(z);
120 } else {
121 ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
122 timeStyle = (DateFormat::EStyle)Z_LVAL_P(z);
123 }
124 idx++;
125 } ZEND_HASH_FOREACH_END();
126 ZEND_ASSERT(idx == 2 && "We checked that there are two elements above");
127 } else if (Z_TYPE_P(format) == IS_LONG) {
128 if (!valid_format(format)) {
129 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
130 "datefmt_format_object: the date/time format type is invalid",
131 0);
132 RETURN_FALSE;
133 }
134 dateStyle = timeStyle = (DateFormat::EStyle)Z_LVAL_P(format);
135 } else {
136 if (!try_convert_to_string(format)) {
137 RETURN_THROWS();
138 }
139 if (Z_STRLEN_P(format) == 0) {
140 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
141 "datefmt_format_object: the format is empty", 0);
142 RETURN_FALSE;
143 }
144 pattern = true;
145 }
146
147 //there's no support for relative time in ICU yet
148 if (timeStyle != DateFormat::NONE) {
149 timeStyle = (DateFormat::EStyle)(timeStyle & ~DateFormat::kRelative);
150 }
151
152 zend_class_entry *instance_ce = Z_OBJCE_P(object);
153 if (instanceof_function(instance_ce, Calendar_ce_ptr)) {
154 Calendar *obj_cal = calendar_fetch_native_calendar(Z_OBJ_P(object));
155 if (obj_cal == NULL) {
156 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
157 "datefmt_format_object: bad IntlCalendar instance: "
158 "not initialized properly", 0);
159 RETURN_FALSE;
160 }
161 timeZone = obj_cal->getTimeZone().clone();
162 date = obj_cal->getTime(status);
163 if (U_FAILURE(status)) {
164 intl_error_set(NULL, status,
165 "datefmt_format_object: error obtaining instant from "
166 "IntlCalendar", 0);
167 RETVAL_FALSE;
168 goto cleanup;
169 }
170 cal = obj_cal->clone();
171 } else if (instanceof_function(instance_ce, php_date_get_interface_ce())) {
172 if (intl_datetime_decompose(object, &date, &timeZone, NULL,
173 "datefmt_format_object") == FAILURE) {
174 RETURN_FALSE;
175 }
176 cal = new GregorianCalendar(Locale::createFromName(locale_str), status);
177 if (U_FAILURE(status)) {
178 intl_error_set(NULL, status,
179 "datefmt_format_object: could not create GregorianCalendar",
180 0);
181 RETVAL_FALSE;
182 goto cleanup;
183 }
184 } else {
185 intl_error_set(NULL, status, "datefmt_format_object: the passed object "
186 "must be an instance of either IntlCalendar or DateTimeInterface",
187 0);
188 RETURN_FALSE;
189 }
190
191 if (pattern) {
192 StringPiece sp(Z_STRVAL_P(format));
193 df = new SimpleDateFormat(
194 UnicodeString::fromUTF8(sp),
195 Locale::createFromName(locale_str),
196 status);
197
198 if (U_FAILURE(status)) {
199 intl_error_set(NULL, status,
200 "datefmt_format_object: could not create SimpleDateFormat",
201 0);
202 RETVAL_FALSE;
203 goto cleanup;
204 }
205 } else {
206 df = DateFormat::createDateTimeInstance(dateStyle, timeStyle,
207 Locale::createFromName(locale_str));
208
209 if (df == NULL) { /* according to ICU sources, this should never happen */
210 intl_error_set(NULL, status,
211 "datefmt_format_object: could not create DateFormat",
212 0);
213 RETVAL_FALSE;
214 goto cleanup;
215 }
216 }
217
218 //must be in this order (or have the cal adopt the tz)
219 df->adoptCalendar(cal);
220 cal = NULL;
221 df->adoptTimeZone(timeZone);
222 timeZone = NULL;
223
224 {
225 zend_string *u8str;
226 UnicodeString result = UnicodeString();
227 df->format(date, result);
228
229 u8str = intl_charFromString(result, &status);
230 if (!u8str) {
231 intl_error_set(NULL, status,
232 "datefmt_format_object: error converting result to UTF-8",
233 0);
234 RETVAL_FALSE;
235 goto cleanup;
236 }
237 RETVAL_STR(u8str);
238 }
239
240
241 cleanup:
242 delete df;
243 delete timeZone;
244 delete cal;
245 }
246