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 <unicode/ustring.h>
21 : #include <unicode/udat.h>
22 : #include <unicode/ucal.h>
23 :
24 : #include "php_intl.h"
25 : #include "intl_convert.h"
26 : #include "dateformat_class.h"
27 : #include "dateformat.h"
28 :
29 : /* {{{ dateformat_register_constants
30 : * Register constants common for the both (OO and procedural)
31 : * APIs.
32 : */
33 : void dateformat_register_constants( INIT_FUNC_ARGS )
34 17633 : {
35 17633 : if( IntlDateFormatter_ce_ptr == NULL) {
36 0 : zend_error(E_ERROR, "DateFormat class not defined");
37 0 : return;
38 : }
39 :
40 : #define DATEFORMATTER_EXPOSE_CONST(x) REGISTER_LONG_CONSTANT(#x, x, CONST_CS)
41 : #define DATEFORMATTER_EXPOSE_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UDAT_##x TSRMLS_CC );
42 : #define DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST(name, value) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( name ) - 1, value TSRMLS_CC );
43 :
44 : #define DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UCAL_##x TSRMLS_CC );
45 :
46 : /* UDateFormatStyle constants */
47 17633 : DATEFORMATTER_EXPOSE_CLASS_CONST( FULL );
48 17633 : DATEFORMATTER_EXPOSE_CLASS_CONST( LONG );
49 17633 : DATEFORMATTER_EXPOSE_CLASS_CONST( MEDIUM );
50 17633 : DATEFORMATTER_EXPOSE_CLASS_CONST( SHORT );
51 17633 : DATEFORMATTER_EXPOSE_CLASS_CONST( NONE );
52 :
53 : /*
54 : DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "GREGORIAN", DATEF_GREGORIAN );
55 : DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "CUSTOMARY", DATEF_CUSTOMARY );
56 : DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "BUDDHIST", DATEF_BUDDHIST );
57 : DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "JAPANESE_IMPERIAL", DATEF_JAPANESE_IMPERIAL );
58 : */
59 :
60 17633 : DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( GREGORIAN );
61 17633 : DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( TRADITIONAL );
62 :
63 : #undef DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST
64 : #undef DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST
65 : #undef DATEFORMATTER_EXPOSE_CLASS_CONST
66 : #undef DATEFORMATTER_EXPOSE_CONST
67 : }
68 : /* }}} */
69 :
70 : /* {{{ */
71 : static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
72 272 : {
73 : char* locale;
74 272 : int locale_len = 0;
75 : zval* object;
76 272 : long date_type = 0;
77 272 : long time_type = 0;
78 272 : long calendar = UCAL_GREGORIAN;
79 272 : char* timezone_str = NULL;
80 272 : int timezone_str_len = 0;
81 272 : char* pattern_str = NULL;
82 272 : int pattern_str_len = 0;
83 272 : UChar* svalue = NULL; /* UTF-16 pattern_str */
84 272 : int slength = 0;
85 272 : UChar* timezone_utf16 = NULL; /* UTF-16 timezone_str */
86 272 : int timezone_utf16_len = 0;
87 272 : UCalendar ucal_obj = NULL;
88 : IntlDateFormatter_object* dfo;
89 :
90 272 : intl_error_reset( NULL TSRMLS_CC );
91 272 : object = return_value;
92 : /* Parse parameters. */
93 272 : if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sll|sls",
94 : &locale, &locale_len, &date_type, &time_type, &timezone_str, &timezone_str_len, &calendar,&pattern_str, &pattern_str_len ) == FAILURE )
95 : {
96 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: unable to parse input parameters", 0 TSRMLS_CC );
97 0 : zval_dtor(return_value);
98 0 : RETURN_NULL();
99 : }
100 :
101 272 : INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
102 272 : DATE_FORMAT_METHOD_FETCH_OBJECT;
103 : /* Convert pattern (if specified) to UTF-16. */
104 272 : if( pattern_str && pattern_str_len>0 ){
105 2 : intl_convert_utf8_to_utf16(&svalue, &slength, pattern_str, pattern_str_len, &INTL_DATA_ERROR_CODE(dfo));
106 2 : INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: error converting pattern to UTF-16");
107 : }
108 :
109 : /* Convert pattern (if specified) to UTF-16. */
110 272 : if( timezone_str && timezone_str_len >0 ){
111 218 : intl_convert_utf8_to_utf16(&timezone_utf16, &timezone_utf16_len, timezone_str, timezone_str_len, &INTL_DATA_ERROR_CODE(dfo));
112 218 : INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: error converting timezone_str to UTF-16" );
113 : }
114 :
115 272 : if(locale_len == 0) {
116 0 : locale = INTL_G(default_locale);
117 : }
118 :
119 274 : if( pattern_str && pattern_str_len>0 ){
120 2 : DATE_FORMAT_OBJECT(dfo) = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, timezone_utf16, timezone_utf16_len, svalue, slength, &INTL_DATA_ERROR_CODE(dfo));
121 : } else {
122 270 : DATE_FORMAT_OBJECT(dfo) = udat_open(time_type, date_type, locale, timezone_utf16, timezone_utf16_len, svalue, slength, &INTL_DATA_ERROR_CODE(dfo));
123 : }
124 :
125 : /* Set the calendar if passed */
126 272 : if(!U_FAILURE(INTL_DATA_ERROR_CODE(dfo)) && calendar) {
127 152 : ucal_obj = ucal_open( timezone_utf16, timezone_utf16_len, locale, calendar, &INTL_DATA_ERROR_CODE(dfo) );
128 152 : if(!U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
129 152 : udat_setCalendar( DATE_FORMAT_OBJECT(dfo), ucal_obj );
130 : }
131 : }
132 :
133 272 : if(svalue)
134 : {
135 2 : efree(svalue);
136 : }
137 272 : if(timezone_utf16)
138 : {
139 218 : efree(timezone_utf16);
140 : }
141 :
142 272 : INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: date formatter creation failed");
143 :
144 : /* Set the class variables */
145 272 : dfo->date_type = date_type;
146 272 : dfo->time_type = time_type;
147 272 : dfo->calendar = calendar;
148 272 : if( timezone_str && timezone_str_len > 0){
149 218 : dfo->timezone_id = estrndup( timezone_str, timezone_str_len);
150 : }
151 : }
152 : /* }}} */
153 :
154 : /* {{{ proto IntlDateFormatter IntlDateFormatter::create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern] )
155 : * Create formatter. }}} */
156 : /* {{{ proto IntlDateFormatter datefmt_create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern] )
157 :
158 : * Create formatter.
159 : */
160 : PHP_FUNCTION( datefmt_create )
161 272 : {
162 272 : object_init_ex( return_value, IntlDateFormatter_ce_ptr );
163 272 : datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
164 272 : }
165 : /* }}} */
166 :
167 : /* {{{ proto void IntlDateFormatter::__construct(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern])
168 : * IntlDateFormatter object constructor.
169 : */
170 : PHP_METHOD( IntlDateFormatter, __construct )
171 0 : {
172 0 : return_value = getThis();
173 0 : datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
174 0 : }
175 : /* }}} */
176 :
177 : /* {{{ proto int IntlDateFormatter::getErrorCode()
178 : * Get formatter's last error code. }}} */
179 : /* {{{ proto int datefmt_get_error_code( IntlDateFormatter $nf )
180 : * Get formatter's last error code.
181 : */
182 : PHP_FUNCTION( datefmt_get_error_code )
183 0 : {
184 0 : zval* object = NULL;
185 0 : IntlDateFormatter_object* dfo = NULL;
186 :
187 : /* Parse parameters. */
188 0 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
189 : &object, IntlDateFormatter_ce_ptr ) == FAILURE )
190 : {
191 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
192 : "datefmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
193 0 : RETURN_FALSE;
194 : }
195 :
196 0 : dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
197 :
198 : /* Return formatter's last error code. */
199 0 : RETURN_LONG( INTL_DATA_ERROR_CODE(dfo) );
200 : }
201 : /* }}} */
202 :
203 : /* {{{ proto string IntlDateFormatter::getErrorMessage( )
204 : * Get text description for formatter's last error code. }}} */
205 : /* {{{ proto string datefmt_get_error_message( IntlDateFormatter $coll )
206 : * Get text description for formatter's last error code.
207 : */
208 : PHP_FUNCTION( datefmt_get_error_message )
209 0 : {
210 0 : char* message = NULL;
211 0 : zval* object = NULL;
212 0 : IntlDateFormatter_object* dfo = NULL;
213 :
214 : /* Parse parameters. */
215 0 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
216 : &object, IntlDateFormatter_ce_ptr ) == FAILURE )
217 : {
218 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
219 : "datefmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
220 :
221 0 : RETURN_FALSE;
222 : }
223 :
224 0 : dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
225 :
226 : /* Return last error message. */
227 0 : message = intl_error_get_message( &dfo->datef_data.error TSRMLS_CC );
228 0 : RETURN_STRING( message, 0);
229 : }
230 : /* }}} */
|