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 "dateformat_class.h"
22 #include "../intl_convert.h"
23 #include "dateformat_class.h"
24 #include "dateformat_attr.h"
25 
26 #include <unicode/ustring.h>
27 #include <unicode/udat.h>
28 
29 /* {{{ proto unicode IntlDateFormatter::getDateType( )
30  * Get formatter datetype. }}} */
31 /* {{{ proto string datefmt_get_datetype( IntlDateFormatter $mf )
32  * Get formatter datetype.
33  */
PHP_FUNCTION(datefmt_get_datetype)34 PHP_FUNCTION( datefmt_get_datetype )
35 {
36 	DATE_FORMAT_METHOD_INIT_VARS;
37 
38 	/* Parse parameters. */
39 	if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
40 	{
41 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
42 			"datefmt_get_datetype: unable to parse input params", 0 TSRMLS_CC );
43 		RETURN_FALSE;
44 	}
45 
46 	/* Fetch the object. */
47 	DATE_FORMAT_METHOD_FETCH_OBJECT;
48 
49 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter datetype." );
50 
51 	RETURN_LONG(dfo->date_type );
52 }
53 /* }}} */
54 
55 /* {{{ proto unicode IntlDateFormatter::getTimeType( )
56  * Get formatter timetype. }}} */
57 /* {{{ proto string datefmt_get_timetype( IntlDateFormatter $mf )
58  * Get formatter timetype.
59  */
PHP_FUNCTION(datefmt_get_timetype)60 PHP_FUNCTION( datefmt_get_timetype )
61 {
62 	DATE_FORMAT_METHOD_INIT_VARS;
63 
64 	/* Parse parameters. */
65 	if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
66 	{
67 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
68 			"datefmt_get_timetype: unable to parse input params", 0 TSRMLS_CC );
69 		RETURN_FALSE;
70 	}
71 
72 	/* Fetch the object. */
73 	DATE_FORMAT_METHOD_FETCH_OBJECT;
74 
75 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timetype." );
76 
77 	RETURN_LONG(dfo->time_type );
78 }
79 /* }}} */
80 
81 /* {{{ proto string IntlDateFormatter::getPattern( )
82  * Get formatter pattern. }}} */
83 /* {{{ proto string datefmt_get_pattern( IntlDateFormatter $mf )
84  * Get formatter pattern.
85  */
PHP_FUNCTION(datefmt_get_pattern)86 PHP_FUNCTION( datefmt_get_pattern )
87 {
88 	UChar  value_buf[64];
89 	int    length = USIZE( value_buf );
90 	UChar* value  = value_buf;
91 	zend_bool   is_pattern_localized =FALSE;
92 
93 	DATE_FORMAT_METHOD_INIT_VARS;
94 
95 	/* Parse parameters. */
96 	if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
97 	{
98 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
99 			"datefmt_get_pattern: unable to parse input params", 0 TSRMLS_CC );
100 		RETURN_FALSE;
101 	}
102 
103 	/* Fetch the object. */
104 	DATE_FORMAT_METHOD_FETCH_OBJECT;
105 
106 	length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo));
107 	if(INTL_DATA_ERROR_CODE(dfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
108 		++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
109 		INTL_DATA_ERROR_CODE(dfo) = U_ZERO_ERROR;
110 		value = eumalloc(length);
111 		length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo) );
112 		if(U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
113 			efree(value);
114 			value = value_buf;
115 		}
116 	}
117 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter pattern" );
118 
119 	INTL_METHOD_RETVAL_UTF8( dfo, value, length, ( value != value_buf ) );
120 }
121 /* }}} */
122 
123 /* {{{ proto bool IntlDateFormatter::setPattern( string $pattern )
124  * Set formatter pattern. }}} */
125 /* {{{ proto bool datefmt_set_pattern( IntlDateFormatter $mf, string $pattern )
126  * Set formatter pattern.
127  */
PHP_FUNCTION(datefmt_set_pattern)128 PHP_FUNCTION( datefmt_set_pattern )
129 {
130 	char*       value = NULL;
131 	int         value_len = 0;
132 	int         slength = 0;
133 	UChar*	    svalue  = NULL;
134 	zend_bool   is_pattern_localized =FALSE;
135 
136 
137 	DATE_FORMAT_METHOD_INIT_VARS;
138 
139 	/* Parse parameters. */
140 	if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
141 		&object, IntlDateFormatter_ce_ptr,  &value, &value_len ) == FAILURE )
142 	{
143 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
144 			"datefmt_set_pattern: unable to parse input params", 0 TSRMLS_CC);
145 		RETURN_FALSE;
146 	}
147 
148 	DATE_FORMAT_METHOD_FETCH_OBJECT;
149 
150 	/* Convert given pattern to UTF-16. */
151 	intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(dfo));
152 	INTL_METHOD_CHECK_STATUS(dfo, "Error converting pattern to UTF-16" );
153 
154 	udat_applyPattern(DATE_FORMAT_OBJECT(dfo), (UBool)is_pattern_localized, svalue, slength);
155 
156 	if (svalue) {
157 		efree(svalue);
158 	}
159 	INTL_METHOD_CHECK_STATUS(dfo, "Error setting symbol value");
160 
161 	RETURN_TRUE;
162 }
163 /* }}} */
164 
165 /* {{{ proto string IntlDateFormatter::getLocale()
166  * Get formatter locale. }}} */
167 /* {{{ proto string datefmt_get_locale(IntlDateFormatter $mf)
168  * Get formatter locale.
169  */
PHP_FUNCTION(datefmt_get_locale)170 PHP_FUNCTION( datefmt_get_locale )
171 {
172 	char *loc;
173 	long  loc_type =ULOC_ACTUAL_LOCALE;
174 
175 	DATE_FORMAT_METHOD_INIT_VARS;
176 
177 	/* Parse parameters. */
178 	if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l",
179 		&object, IntlDateFormatter_ce_ptr,&loc_type) == FAILURE )
180 	{
181 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
182 			"datefmt_get_locale: unable to parse input params", 0 TSRMLS_CC );
183 
184 		RETURN_FALSE;
185 	}
186 
187 	/* Fetch the object. */
188 	DATE_FORMAT_METHOD_FETCH_OBJECT;
189 
190 	loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), loc_type,&INTL_DATA_ERROR_CODE(dfo));
191 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting locale");
192 	RETURN_STRING(loc, 1);
193 }
194 /* }}} */
195 
196 /* {{{ proto string IntlDateFormatter::isLenient()
197  * Get formatter isLenient. }}} */
198 /* {{{ proto string datefmt_isLenient(IntlDateFormatter $mf)
199  * Get formatter locale.
200  */
PHP_FUNCTION(datefmt_is_lenient)201 PHP_FUNCTION( datefmt_is_lenient )
202 {
203 
204 	DATE_FORMAT_METHOD_INIT_VARS;
205 
206 	/* Parse parameters. */
207 	if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
208 		&object, IntlDateFormatter_ce_ptr ) == FAILURE )
209 	{
210 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
211 			"datefmt_is_lenient: unable to parse input params", 0 TSRMLS_CC );
212 
213 		RETURN_FALSE;
214 	}
215 
216 	/* Fetch the object. */
217 	DATE_FORMAT_METHOD_FETCH_OBJECT;
218 
219 	RETVAL_BOOL(udat_isLenient(DATE_FORMAT_OBJECT(dfo)));
220 }
221 /* }}} */
222 
223 /* {{{ proto string IntlDateFormatter::setLenient()
224  * Set formatter lenient. }}} */
225 /* {{{ proto string datefmt_setLenient(IntlDateFormatter $mf)
226  * Set formatter lenient.
227  */
PHP_FUNCTION(datefmt_set_lenient)228 PHP_FUNCTION( datefmt_set_lenient )
229 {
230 	zend_bool isLenient  = FALSE;
231 
232 	DATE_FORMAT_METHOD_INIT_VARS;
233 
234 	/* Parse parameters. */
235 	if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ob",
236 	&object, IntlDateFormatter_ce_ptr,&isLenient ) == FAILURE )
237 	{
238 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
239 			"datefmt_set_lenient: unable to parse input params", 0 TSRMLS_CC );
240 		RETURN_FALSE;
241 	}
242 
243 	/* Fetch the object. */
244 	DATE_FORMAT_METHOD_FETCH_OBJECT;
245 
246 	udat_setLenient(DATE_FORMAT_OBJECT(dfo), (UBool)isLenient );
247 }
248 /* }}} */
249