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