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 convert_to_string_ex(format);
146 if (Z_STRLEN_P(format) == 0) {
147 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
148 "datefmt_format_object: the format is empty", 0);
149 RETURN_FALSE;
150 }
151 pattern = true;
152 }
153
154 //there's no support for relative time in ICU yet
155 if (timeStyle != DateFormat::NONE) {
156 timeStyle = (DateFormat::EStyle)(timeStyle & ~DateFormat::kRelative);
157 }
158
159 zend_class_entry *instance_ce = Z_OBJCE_P(object);
160 if (instanceof_function(instance_ce, Calendar_ce_ptr)) {
161 Calendar *obj_cal = calendar_fetch_native_calendar(object);
162 if (obj_cal == NULL) {
163 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
164 "datefmt_format_object: bad IntlCalendar instance: "
165 "not initialized properly", 0);
166 RETURN_FALSE;
167 }
168 timeZone = obj_cal->getTimeZone().clone();
169 date = obj_cal->getTime(status);
170 if (U_FAILURE(status)) {
171 intl_error_set(NULL, status,
172 "datefmt_format_object: error obtaining instant from "
173 "IntlCalendar", 0);
174 RETVAL_FALSE;
175 goto cleanup;
176 }
177 cal = obj_cal->clone();
178 } else if (instanceof_function(instance_ce, php_date_get_interface_ce())) {
179 if (intl_datetime_decompose(object, &date, &timeZone, NULL,
180 "datefmt_format_object") == FAILURE) {
181 RETURN_FALSE;
182 }
183 cal = new GregorianCalendar(Locale::createFromName(locale_str), status);
184 if (U_FAILURE(status)) {
185 intl_error_set(NULL, status,
186 "datefmt_format_object: could not create GregorianCalendar",
187 0);
188 RETVAL_FALSE;
189 goto cleanup;
190 }
191 } else {
192 intl_error_set(NULL, status, "datefmt_format_object: the passed object "
193 "must be an instance of either IntlCalendar or DateTime",
194 0);
195 RETURN_FALSE;
196 }
197
198 if (pattern) {
199 StringPiece sp(Z_STRVAL_P(format));
200 df = new SimpleDateFormat(
201 UnicodeString::fromUTF8(sp),
202 Locale::createFromName(locale_str),
203 status);
204
205 if (U_FAILURE(status)) {
206 intl_error_set(NULL, status,
207 "datefmt_format_object: could not create SimpleDateFormat",
208 0);
209 RETVAL_FALSE;
210 goto cleanup;
211 }
212 } else {
213 df = DateFormat::createDateTimeInstance(dateStyle, timeStyle,
214 Locale::createFromName(locale_str));
215
216 if (df == NULL) { /* according to ICU sources, this should never happen */
217 intl_error_set(NULL, status,
218 "datefmt_format_object: could not create DateFormat",
219 0);
220 RETVAL_FALSE;
221 goto cleanup;
222 }
223 }
224
225 //must be in this order (or have the cal adopt the tz)
226 df->adoptCalendar(cal);
227 cal = NULL;
228 df->adoptTimeZone(timeZone);
229 timeZone = NULL;
230
231 {
232 zend_string *u8str;
233 UnicodeString result = UnicodeString();
234 df->format(date, result);
235
236 u8str = intl_charFromString(result, &status);
237 if (!u8str) {
238 intl_error_set(NULL, status,
239 "datefmt_format_object: error converting result to UTF-8",
240 0);
241 RETVAL_FALSE;
242 goto cleanup;
243 }
244 RETVAL_STR(u8str);
245 }
246
247
248 cleanup:
249 delete df;
250 delete timeZone;
251 delete cal;
252 }
253