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    | https://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    +----------------------------------------------------------------------+
13 */
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include "../php_intl.h"
19 #include "dateformat_class.h"
20 #include "../intl_convert.h"
21 #include "dateformat_class.h"
22 
23 #include <unicode/ustring.h>
24 #include <unicode/udat.h>
25 
26 /* {{{ Get formatter datetype. */
PHP_FUNCTION(datefmt_get_datetype)27 PHP_FUNCTION( datefmt_get_datetype )
28 {
29 	DATE_FORMAT_METHOD_INIT_VARS;
30 
31 	/* Parse parameters. */
32 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
33 	{
34 		RETURN_THROWS();
35 	}
36 
37 	/* Fetch the object. */
38 	DATE_FORMAT_METHOD_FETCH_OBJECT;
39 
40 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter datetype." );
41 
42 	RETURN_LONG(dfo->date_type );
43 }
44 /* }}} */
45 
46 /* {{{ Get formatter timetype. */
PHP_FUNCTION(datefmt_get_timetype)47 PHP_FUNCTION( datefmt_get_timetype )
48 {
49 	DATE_FORMAT_METHOD_INIT_VARS;
50 
51 	/* Parse parameters. */
52 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
53 	{
54 		RETURN_THROWS();
55 	}
56 
57 	/* Fetch the object. */
58 	DATE_FORMAT_METHOD_FETCH_OBJECT;
59 
60 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timetype." );
61 
62 	RETURN_LONG(dfo->time_type );
63 }
64 /* }}} */
65 
66 /* {{{ Get formatter pattern. */
PHP_FUNCTION(datefmt_get_pattern)67 PHP_FUNCTION( datefmt_get_pattern )
68 {
69 	UChar  value_buf[64];
70 	uint32_t    length = USIZE( value_buf );
71 	UChar* value  = value_buf;
72 	bool   is_pattern_localized = false;
73 
74 	DATE_FORMAT_METHOD_INIT_VARS;
75 
76 	/* Parse parameters. */
77 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
78 	{
79 		RETURN_THROWS();
80 	}
81 
82 	/* Fetch the object. */
83 	DATE_FORMAT_METHOD_FETCH_OBJECT;
84 
85 	length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo));
86 	if(INTL_DATA_ERROR_CODE(dfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
87 		++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
88 		INTL_DATA_ERROR_CODE(dfo) = U_ZERO_ERROR;
89 		value = eumalloc(length);
90 		length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo) );
91 		if(U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
92 			efree(value);
93 			value = value_buf;
94 		}
95 	}
96 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter pattern" );
97 
98 	INTL_METHOD_RETVAL_UTF8( dfo, value, length, ( value != value_buf ) );
99 }
100 /* }}} */
101 
102 /* {{{ Set formatter pattern. */
PHP_FUNCTION(datefmt_set_pattern)103 PHP_FUNCTION( datefmt_set_pattern )
104 {
105 	char*       value = NULL;
106 	size_t      value_len = 0;
107 	int32_t     slength = 0;
108 	UChar*	    svalue  = NULL;
109 	bool   is_pattern_localized = false;
110 
111 
112 	DATE_FORMAT_METHOD_INIT_VARS;
113 
114 	/* Parse parameters. */
115 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os",
116 		&object, IntlDateFormatter_ce_ptr,  &value, &value_len ) == FAILURE )
117 	{
118 		RETURN_THROWS();
119 	}
120 
121 	DATE_FORMAT_METHOD_FETCH_OBJECT;
122 
123 	/* Convert given pattern to UTF-16. */
124 	intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(dfo));
125 	INTL_METHOD_CHECK_STATUS(dfo, "Error converting pattern to UTF-16" );
126 
127 	udat_applyPattern(DATE_FORMAT_OBJECT(dfo), (UBool)is_pattern_localized, svalue, slength);
128 
129 	if (svalue) {
130 		efree(svalue);
131 	}
132 	INTL_METHOD_CHECK_STATUS(dfo, "Error setting symbol value");
133 
134 	RETURN_TRUE;
135 }
136 /* }}} */
137 
138 /* {{{ Get formatter locale. */
PHP_FUNCTION(datefmt_get_locale)139 PHP_FUNCTION( datefmt_get_locale )
140 {
141 	char *loc;
142 	zend_long  loc_type =ULOC_ACTUAL_LOCALE;
143 
144 	DATE_FORMAT_METHOD_INIT_VARS;
145 
146 	/* Parse parameters. */
147 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O|l",
148 		&object, IntlDateFormatter_ce_ptr,&loc_type) == FAILURE )
149 	{
150 
151 		RETURN_THROWS();
152 	}
153 
154 	/* Fetch the object. */
155 	DATE_FORMAT_METHOD_FETCH_OBJECT;
156 
157 	loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), loc_type,&INTL_DATA_ERROR_CODE(dfo));
158 	INTL_METHOD_CHECK_STATUS(dfo, "Error getting locale");
159 	RETURN_STRING(loc);
160 }
161 /* }}} */
162 
163 /* {{{ Get formatter isLenient. */
PHP_FUNCTION(datefmt_is_lenient)164 PHP_FUNCTION( datefmt_is_lenient )
165 {
166 
167 	DATE_FORMAT_METHOD_INIT_VARS;
168 
169 	/* Parse parameters. */
170 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
171 		&object, IntlDateFormatter_ce_ptr ) == FAILURE )
172 	{
173 
174 		RETURN_THROWS();
175 	}
176 
177 	/* Fetch the object. */
178 	DATE_FORMAT_METHOD_FETCH_OBJECT;
179 
180 	RETVAL_BOOL(udat_isLenient(DATE_FORMAT_OBJECT(dfo)));
181 }
182 /* }}} */
183 
184 /* {{{ Set formatter lenient. */
PHP_FUNCTION(datefmt_set_lenient)185 PHP_FUNCTION( datefmt_set_lenient )
186 {
187 	bool isLenient = false;
188 
189 	DATE_FORMAT_METHOD_INIT_VARS;
190 
191 	/* Parse parameters. */
192 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Ob",
193 	&object, IntlDateFormatter_ce_ptr,&isLenient ) == FAILURE )
194 	{
195 		RETURN_THROWS();
196 	}
197 
198 	/* Fetch the object. */
199 	DATE_FORMAT_METHOD_FETCH_OBJECT;
200 
201 	udat_setLenient(DATE_FORMAT_OBJECT(dfo), (UBool)isLenient );
202 }
203 /* }}} */
204