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 | http://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: Kirti Velankar <kirtig@yahoo-inc.com> |
12 | Gustavo Lopes <cataphract@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 #include "../intl_cppshims.h"
17
18 #include <unicode/timezone.h>
19 #include <unicode/calendar.h>
20 #include <unicode/datefmt.h>
21
22 extern "C" {
23 #include <unicode/ustring.h>
24 #include <unicode/udat.h>
25
26 #include "php_intl.h"
27 #include "dateformat_create.h"
28 #include "dateformat_class.h"
29 #define USE_CALENDAR_POINTER 1
30 #include "../calendar/calendar_class.h"
31 #define USE_TIMEZONE_POINTER 1
32 #include "../timezone/timezone_class.h"
33 #include "../intl_convert.h"
34 }
35
36 #include "dateformat_helpers.h"
37 #include "zend_exceptions.h"
38
39 #define INTL_UDATE_FMT_OK(i) \
40 (UDAT_FULL == (i) || UDAT_LONG == (i) || \
41 UDAT_MEDIUM == (i) || UDAT_SHORT == (i) || \
42 UDAT_RELATIVE == (i) || UDAT_FULL_RELATIVE == (i) || \
43 UDAT_LONG_RELATIVE == (i) || UDAT_MEDIUM_RELATIVE == (i) || \
44 UDAT_SHORT_RELATIVE == (i) || UDAT_NONE == (i) || \
45 UDAT_PATTERN == (i))
46
47 /* {{{ */
datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)48 static zend_result datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
49 {
50 zval *object;
51 char *locale_str;
52 size_t locale_len = 0;
53 Locale locale;
54 zend_long date_type = 0;
55 zend_long time_type = 0;
56 zend_object *calendar_obj = NULL;
57 zend_long calendar_long = 0;
58 zend_bool calendar_is_null = 1;
59 Calendar *cal = NULL;
60 zend_long calendar_type;
61 bool calendar_owned;
62 zval *timezone_zv = NULL;
63 TimeZone *timezone = NULL;
64 bool explicit_tz;
65 char* pattern_str = NULL;
66 size_t pattern_str_len = 0;
67 UChar* svalue = NULL; /* UTF-16 pattern_str */
68 int32_t slength = 0;
69 IntlDateFormatter_object* dfo;
70
71 intl_error_reset(NULL);
72 object = return_value;
73
74 ZEND_PARSE_PARAMETERS_START(3, 6)
75 Z_PARAM_STRING_OR_NULL(locale_str, locale_len)
76 Z_PARAM_LONG(date_type)
77 Z_PARAM_LONG(time_type)
78 Z_PARAM_OPTIONAL
79 Z_PARAM_ZVAL(timezone_zv)
80 Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL(calendar_obj, Calendar_ce_ptr, calendar_long, calendar_is_null)
81 Z_PARAM_STRING_OR_NULL(pattern_str, pattern_str_len)
82 ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
83
84 DATE_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
85
86 if (DATE_FORMAT_OBJECT(dfo) != NULL) {
87 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: cannot call constructor twice", 0);
88 return FAILURE;
89 }
90
91 if (!INTL_UDATE_FMT_OK(date_type)) {
92 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: invalid date format style", 0);
93 return FAILURE;
94 }
95 if (!INTL_UDATE_FMT_OK(time_type)) {
96 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: invalid time format style", 0);
97 return FAILURE;
98 }
99
100 INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
101 if (locale_len == 0) {
102 locale_str = (char *) intl_locale_get_default();
103 }
104 locale = Locale::createFromName(locale_str);
105
106 /* process calendar */
107 if (datefmt_process_calendar_arg(calendar_obj, calendar_long, calendar_is_null, locale, "datefmt_create",
108 INTL_DATA_ERROR_P(dfo), cal, calendar_type, calendar_owned) == FAILURE
109 ) {
110 goto error;
111 }
112
113 /* process timezone */
114 explicit_tz = timezone_zv != NULL && Z_TYPE_P(timezone_zv) != IS_NULL;
115
116 if (explicit_tz || calendar_owned ) {
117 //we have an explicit time zone or a non-object calendar
118 timezone = timezone_process_timezone_argument(timezone_zv,
119 INTL_DATA_ERROR_P(dfo), "datefmt_create");
120 if (timezone == NULL) {
121 goto error;
122 }
123 }
124
125 /* Convert pattern (if specified) to UTF-16. */
126 if (pattern_str && pattern_str_len > 0) {
127 intl_convert_utf8_to_utf16(&svalue, &slength,
128 pattern_str, pattern_str_len, &INTL_DATA_ERROR_CODE(dfo));
129 if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
130 /* object construction -> only set global error */
131 intl_error_set(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: "
132 "error converting pattern to UTF-16", 0);
133 goto error;
134 }
135 }
136
137 DATE_FORMAT_OBJECT(dfo) = udat_open((UDateFormatStyle)time_type,
138 (UDateFormatStyle)date_type, locale_str, NULL, 0, svalue,
139 slength, &INTL_DATA_ERROR_CODE(dfo));
140
141 if (pattern_str && pattern_str_len > 0) {
142 udat_applyPattern(DATE_FORMAT_OBJECT(dfo), true, svalue, slength);
143 if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
144 intl_error_set(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: error applying pattern", 0);
145 goto error;
146 }
147 }
148
149 if (!U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
150 DateFormat *df = (DateFormat*)DATE_FORMAT_OBJECT(dfo);
151 if (calendar_owned) {
152 df->adoptCalendar(cal);
153 calendar_owned = false;
154 } else {
155 df->setCalendar(*cal);
156 }
157
158 if (timezone != NULL) {
159 df->adoptTimeZone(timezone);
160 }
161 } else {
162 intl_error_set(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: date "
163 "formatter creation failed", 0);
164 goto error;
165 }
166
167 /* Set the class variables */
168 dfo->date_type = date_type;
169 dfo->time_type = time_type;
170 dfo->calendar = calendar_type;
171 dfo->requested_locale = estrdup(locale_str);
172
173 error:
174 if (svalue) {
175 efree(svalue);
176 }
177 if (timezone != NULL && DATE_FORMAT_OBJECT(dfo) == NULL) {
178 delete timezone;
179 }
180 if (cal != NULL && calendar_owned) {
181 delete cal;
182 }
183
184 return U_FAILURE(intl_error_get_code(NULL)) ? FAILURE : SUCCESS;
185 }
186 /* }}} */
187
188 /* {{{ Create formatter. */
PHP_FUNCTION(datefmt_create)189 U_CFUNC PHP_FUNCTION( datefmt_create )
190 {
191 object_init_ex( return_value, IntlDateFormatter_ce_ptr );
192 if (datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
193 zval_ptr_dtor(return_value);
194 RETURN_NULL();
195 }
196 }
197 /* }}} */
198
199 /* {{{ IntlDateFormatter object constructor. */
PHP_METHOD(IntlDateFormatter,__construct)200 U_CFUNC PHP_METHOD( IntlDateFormatter, __construct )
201 {
202 zend_error_handling error_handling;
203
204 zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
205 /* return_value param is being changed, therefore we will always return
206 * NULL here */
207 return_value = ZEND_THIS;
208 if (datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
209 if (!EG(exception)) {
210 zend_string *err = intl_error_get_message(NULL);
211 zend_throw_exception(IntlException_ce_ptr, ZSTR_VAL(err), intl_error_get_code(NULL));
212 zend_string_release_ex(err, 0);
213 }
214 }
215 zend_restore_error_handling(&error_handling);
216 }
217 /* }}} */
218