1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
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: Kirti Velankar <kirtig@yahoo-inc.com> |
14 : +----------------------------------------------------------------------+
15 : */
16 : #ifdef HAVE_CONFIG_H
17 : #include "config.h"
18 : #endif
19 :
20 : #include "php_intl.h"
21 : #include "intl_convert.h"
22 : #include "dateformat_class.h"
23 : #include "dateformat_attr.h"
24 :
25 : #include <unicode/ustring.h>
26 : #include <unicode/udat.h>
27 : #include <unicode/ucal.h>
28 :
29 12 : static void internal_set_calendar(IntlDateFormatter_object *dfo, char* timezone_id, int timezone_id_len, int calendar, zval* return_value TSRMLS_DC){
30 12 : int timezone_utf16_len = 0;
31 12 : UChar* timezone_utf16 = NULL; /* timezone_id in UTF-16 */
32 12 : char* locale = NULL;
33 :
34 12 : UCalendar* ucal_obj = NULL;
35 :
36 : /* check for the validity of value of calendar passed */
37 12 : intl_error_reset( NULL TSRMLS_CC );
38 12 : if( calendar > 1){
39 0 : intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
40 : "datefmt_set_calendar: calendar value specified is out of valid range", 0 TSRMLS_CC);
41 0 : RETURN_FALSE;
42 : }
43 :
44 : /* Convert timezone to UTF-16. */
45 12 : intl_convert_utf8_to_utf16(&timezone_utf16, &timezone_utf16_len, timezone_id, timezone_id_len, &INTL_DATA_ERROR_CODE(dfo));
46 12 : INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
47 :
48 : /* Get the locale for the dateformatter */
49 12 : locale = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(dfo));
50 :
51 : /* Set the calendar if passed */
52 12 : ucal_obj = ucal_open(timezone_utf16, timezone_utf16_len, locale, calendar, &INTL_DATA_ERROR_CODE(dfo) );
53 12 : udat_setCalendar( DATE_FORMAT_OBJECT(dfo), ucal_obj );
54 12 : INTL_METHOD_CHECK_STATUS(dfo, "Error setting the calendar.");
55 :
56 12 : if( timezone_utf16){
57 12 : efree(timezone_utf16);
58 : }
59 : }
60 :
61 : /* {{{ proto unicode IntlDateFormatter::getDateType( )
62 : * Get formatter datetype. }}} */
63 : /* {{{ proto string datefmt_get_datetype( IntlDateFormatter $mf )
64 : * Get formatter datetype.
65 : */
66 : PHP_FUNCTION( datefmt_get_datetype )
67 10 : {
68 10 : DATE_FORMAT_METHOD_INIT_VARS;
69 :
70 : /* Parse parameters. */
71 10 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
72 : {
73 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
74 : "datefmt_get_datetype: unable to parse input params", 0 TSRMLS_CC );
75 0 : RETURN_FALSE;
76 : }
77 :
78 : /* Fetch the object. */
79 10 : DATE_FORMAT_METHOD_FETCH_OBJECT;
80 :
81 10 : INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter datetype." );
82 :
83 10 : RETURN_LONG(dfo->date_type );
84 : }
85 : /* }}} */
86 :
87 : /* {{{ proto unicode IntlDateFormatter::getTimeType( )
88 : * Get formatter timetype. }}} */
89 : /* {{{ proto string datefmt_get_timetype( IntlDateFormatter $mf )
90 : * Get formatter timetype.
91 : */
92 : PHP_FUNCTION( datefmt_get_timetype )
93 10 : {
94 10 : DATE_FORMAT_METHOD_INIT_VARS;
95 :
96 : /* Parse parameters. */
97 10 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
98 : {
99 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
100 : "datefmt_get_timetype: unable to parse input params", 0 TSRMLS_CC );
101 0 : RETURN_FALSE;
102 : }
103 :
104 : /* Fetch the object. */
105 10 : DATE_FORMAT_METHOD_FETCH_OBJECT;
106 :
107 10 : INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timetype." );
108 :
109 10 : RETURN_LONG(dfo->time_type );
110 : }
111 : /* }}} */
112 :
113 :
114 : /* {{{ proto unicode IntlDateFormatter::getCalendar( )
115 : * Get formatter calendar. }}} */
116 : /* {{{ proto string datefmt_get_calendar( IntlDateFormatter $mf )
117 : * Get formatter calendar.
118 : */
119 : PHP_FUNCTION( datefmt_get_calendar )
120 8 : {
121 8 : DATE_FORMAT_METHOD_INIT_VARS;
122 :
123 : /* Parse parameters. */
124 8 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
125 : {
126 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
127 : "datefmt_get_calendar: unable to parse input params", 0 TSRMLS_CC );
128 0 : RETURN_FALSE;
129 : }
130 :
131 : /* Fetch the object. */
132 8 : DATE_FORMAT_METHOD_FETCH_OBJECT;
133 :
134 8 : INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter calendar." );
135 :
136 8 : RETURN_LONG(dfo->calendar);
137 : }
138 : /* }}} */
139 :
140 : /* {{{ proto unicode IntlDateFormatter::getTimeZoneId( )
141 : * Get formatter timezone_id. }}} */
142 : /* {{{ proto string datefmt_get_timezone_id( IntlDateFormatter $mf )
143 : * Get formatter timezone_id.
144 : */
145 : PHP_FUNCTION( datefmt_get_timezone_id )
146 16 : {
147 16 : DATE_FORMAT_METHOD_INIT_VARS;
148 :
149 : /* Parse parameters. */
150 16 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
151 : {
152 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
153 : "datefmt_get_timezone_id: unable to parse input params", 0 TSRMLS_CC );
154 0 : RETURN_FALSE;
155 : }
156 :
157 : /* Fetch the object. */
158 16 : DATE_FORMAT_METHOD_FETCH_OBJECT;
159 :
160 16 : INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timezone_id." );
161 :
162 16 : if( dfo->timezone_id ){
163 16 : RETURN_STRING((char*)dfo->timezone_id, TRUE );
164 : }else{
165 0 : RETURN_NULL();
166 : }
167 : }
168 :
169 : /* {{{ proto boolean IntlDateFormatter::setTimeZoneId( $timezone_id)
170 : * Set formatter timezone_id. }}} */
171 : /* {{{ proto boolean datefmt_set_timezone_id( IntlDateFormatter $mf,$timezone_id)
172 : * Set formatter timezone_id.
173 : */
174 : PHP_FUNCTION( datefmt_set_timezone_id )
175 8 : {
176 8 : char* timezone_id = NULL;
177 8 : int timezone_id_len = 0;
178 :
179 8 : DATE_FORMAT_METHOD_INIT_VARS;
180 :
181 : /* Parse parameters. */
182 8 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &object, IntlDateFormatter_ce_ptr,&timezone_id, &timezone_id_len) == FAILURE )
183 : {
184 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
185 : "datefmt_set_timezone_id: unable to parse input params", 0 TSRMLS_CC );
186 0 : RETURN_FALSE;
187 : }
188 :
189 : /* Fetch the object. */
190 8 : DATE_FORMAT_METHOD_FETCH_OBJECT;
191 :
192 : /* set the timezone for the calendar */
193 8 : internal_set_calendar( dfo, timezone_id, timezone_id_len, dfo->calendar, return_value TSRMLS_CC );
194 :
195 : /* Set the IntlDateFormatter variable */
196 8 : if( dfo->timezone_id ){
197 8 : efree(dfo->timezone_id);
198 : }
199 8 : dfo->timezone_id = estrndup(timezone_id, timezone_id_len);
200 :
201 8 : RETURN_TRUE;
202 : }
203 :
204 : /* {{{ proto string IntlDateFormatter::getPattern( )
205 : * Get formatter pattern. }}} */
206 : /* {{{ proto string datefmt_get_pattern( IntlDateFormatter $mf )
207 : * Get formatter pattern.
208 : */
209 : PHP_FUNCTION( datefmt_get_pattern )
210 10 : {
211 : UChar value_buf[64];
212 10 : int length = USIZE( value_buf );
213 10 : UChar* value = value_buf;
214 10 : zend_bool is_pattern_localized =FALSE;
215 :
216 10 : DATE_FORMAT_METHOD_INIT_VARS;
217 :
218 : /* Parse parameters. */
219 10 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
220 : {
221 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
222 : "datefmt_get_pattern: unable to parse input params", 0 TSRMLS_CC );
223 0 : RETURN_FALSE;
224 : }
225 :
226 : /* Fetch the object. */
227 10 : DATE_FORMAT_METHOD_FETCH_OBJECT;
228 :
229 10 : length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo));
230 10 : if(INTL_DATA_ERROR_CODE(dfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
231 0 : ++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
232 0 : INTL_DATA_ERROR_CODE(dfo) = U_ZERO_ERROR;
233 0 : value = eumalloc(length);
234 0 : length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo) );
235 0 : if(U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
236 0 : efree(value);
237 0 : value = value_buf;
238 : }
239 : }
240 10 : INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter pattern" );
241 :
242 10 : INTL_METHOD_RETVAL_UTF8( dfo, value, length, ( value != value_buf ) );
243 : }
244 : /* }}} */
245 :
246 : /* {{{ proto bool IntlDateFormatter::setPattern( string $pattern )
247 : * Set formatter pattern. }}} */
248 : /* {{{ proto bool datefmt_set_pattern( IntlDateFormatter $mf, string $pattern )
249 : * Set formatter pattern.
250 : */
251 : PHP_FUNCTION( datefmt_set_pattern )
252 8 : {
253 8 : char* value = NULL;
254 8 : int value_len = 0;
255 8 : int slength = 0;
256 8 : UChar* svalue = NULL;
257 8 : zend_bool is_pattern_localized =FALSE;
258 :
259 :
260 8 : DATE_FORMAT_METHOD_INIT_VARS;
261 :
262 : /* Parse parameters. */
263 8 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
264 : &object, IntlDateFormatter_ce_ptr, &value, &value_len ) == FAILURE )
265 : {
266 0 : intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
267 : "datefmt_set_pattern: unable to parse input params", 0 TSRMLS_CC);
268 0 : RETURN_FALSE;
269 : }
270 :
271 8 : DATE_FORMAT_METHOD_FETCH_OBJECT;
272 :
273 : /* Convert given pattern to UTF-16. */
274 8 : intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(dfo));
275 8 : INTL_METHOD_CHECK_STATUS(dfo, "Error converting pattern to UTF-16" );
276 :
277 8 : udat_applyPattern(DATE_FORMAT_OBJECT(dfo), (UBool)is_pattern_localized, svalue, slength);
278 :
279 8 : efree(svalue);
280 8 : INTL_METHOD_CHECK_STATUS(dfo, "Error setting symbol value");
281 :
282 8 : RETURN_TRUE;
283 : }
284 : /* }}} */
285 :
286 : /* {{{ proto string IntlDateFormatter::getLocale()
287 : * Get formatter locale. }}} */
288 : /* {{{ proto string datefmt_get_locale(IntlDateFormatter $mf)
289 : * Get formatter locale.
290 : */
291 : PHP_FUNCTION( datefmt_get_locale )
292 8 : {
293 : char *loc;
294 8 : long loc_type =ULOC_ACTUAL_LOCALE;
295 :
296 8 : DATE_FORMAT_METHOD_INIT_VARS;
297 :
298 : /* Parse parameters. */
299 8 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l",
300 : &object, IntlDateFormatter_ce_ptr,&loc_type) == FAILURE )
301 : {
302 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
303 : "datefmt_get_locale: unable to parse input params", 0 TSRMLS_CC );
304 :
305 0 : RETURN_FALSE;
306 : }
307 :
308 : /* Fetch the object. */
309 8 : DATE_FORMAT_METHOD_FETCH_OBJECT;
310 :
311 8 : loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), loc_type,&INTL_DATA_ERROR_CODE(dfo));
312 8 : RETURN_STRING(loc, 1);
313 : }
314 : /* }}} */
315 :
316 : /* {{{ proto string IntlDateFormatter::isLenient()
317 : * Get formatter isLenient. }}} */
318 : /* {{{ proto string datefmt_isLenient(IntlDateFormatter $mf)
319 : * Get formatter locale.
320 : */
321 : PHP_FUNCTION( datefmt_is_lenient )
322 6 : {
323 :
324 6 : DATE_FORMAT_METHOD_INIT_VARS;
325 :
326 : /* Parse parameters. */
327 6 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
328 : &object, IntlDateFormatter_ce_ptr ) == FAILURE )
329 : {
330 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
331 : "datefmt_is_lenient: unable to parse input params", 0 TSRMLS_CC );
332 :
333 0 : RETURN_FALSE;
334 : }
335 :
336 : /* Fetch the object. */
337 6 : DATE_FORMAT_METHOD_FETCH_OBJECT;
338 :
339 6 : RETVAL_BOOL(udat_isLenient(DATE_FORMAT_OBJECT(dfo)));
340 : }
341 : /* }}} */
342 :
343 : /* {{{ proto string IntlDateFormatter::setLenient()
344 : * Set formatter lenient. }}} */
345 : /* {{{ proto string datefmt_setLenient(IntlDateFormatter $mf)
346 : * Set formatter lenient.
347 : */
348 : PHP_FUNCTION( datefmt_set_lenient )
349 4 : {
350 4 : zend_bool isLenient = FALSE;
351 :
352 4 : DATE_FORMAT_METHOD_INIT_VARS;
353 :
354 : /* Parse parameters. */
355 4 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ob",
356 : &object, IntlDateFormatter_ce_ptr,&isLenient ) == FAILURE )
357 : {
358 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
359 : "datefmt_set_lenient: unable to parse input params", 0 TSRMLS_CC );
360 0 : RETURN_FALSE;
361 : }
362 :
363 : /* Fetch the object. */
364 4 : DATE_FORMAT_METHOD_FETCH_OBJECT;
365 :
366 4 : udat_setLenient(DATE_FORMAT_OBJECT(dfo), (UBool)isLenient );
367 : }
368 : /* }}} */
369 :
370 : /* {{{ proto bool IntlDateFormatter::setPattern( int $calendar )
371 : * Set formatter calendar. }}} */
372 : /* {{{ proto bool datefmt_set_calendar( IntlDateFormatter $mf, int $calendar )
373 : * Set formatter calendar.
374 : */
375 : PHP_FUNCTION( datefmt_set_calendar )
376 6 : {
377 6 : long calendar = 0;
378 :
379 6 : DATE_FORMAT_METHOD_INIT_VARS;
380 :
381 : /* Parse parameters. */
382 6 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
383 : &object, IntlDateFormatter_ce_ptr, &calendar ) == FAILURE ) {
384 0 : intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
385 : "datefmt_set_calendar: unable to parse input params", 0 TSRMLS_CC);
386 0 : RETURN_FALSE;
387 : }
388 :
389 : /* check for the validity of value of calendar passed */
390 6 : intl_error_reset( NULL TSRMLS_CC );
391 6 : if (calendar > 1) {
392 2 : intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
393 : "datefmt_set_calendar: calendar value specified is out of valid range", 0 TSRMLS_CC);
394 2 : RETURN_FALSE;
395 : }
396 :
397 4 : DATE_FORMAT_METHOD_FETCH_OBJECT;
398 :
399 4 : internal_set_calendar( dfo, dfo->timezone_id, strlen(dfo->timezone_id), calendar, return_value TSRMLS_CC );
400 :
401 : /* Set the calendar value in the IntlDateFormatter object */
402 4 : dfo->calendar = calendar;
403 :
404 4 : RETURN_TRUE;
405 : }
406 : /* }}} */
407 :
408 :
|