31
31
* if set to 1 - store any error encountered in the parameter parse_error
32
32
* if set to 0 - no need to store any error encountered in the parameter parse_error
33
33
*/
34
- static void internal_parse_to_timestamp (IntlDateFormatter_object * dfo , char * text_to_parse , size_t text_len , int32_t * parse_pos , zval * return_value )
34
+ static void internal_parse_to_timestamp (IntlDateFormatter_object * dfo , char * text_to_parse , size_t text_len , int32_t * parse_pos , bool update_calendar , zval * return_value )
35
35
{
36
36
double result = 0 ;
37
37
UDate timestamp = 0 ;
@@ -42,13 +42,22 @@ static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* tex
42
42
intl_convert_utf8_to_utf16 (& text_utf16 , & text_utf16_len , text_to_parse , text_len , & INTL_DATA_ERROR_CODE (dfo ));
43
43
INTL_METHOD_CHECK_STATUS (dfo , "Error converting timezone to UTF-16" );
44
44
45
- timestamp = udat_parse ( DATE_FORMAT_OBJECT (dfo ), text_utf16 , text_utf16_len , parse_pos , & INTL_DATA_ERROR_CODE (dfo ));
46
- if ( text_utf16 ){
47
- efree (text_utf16 );
45
+ if (UNEXPECTED (update_calendar )) {
46
+ UCalendar * parsed_calendar = (UCalendar * )udat_getCalendar (DATE_FORMAT_OBJECT (dfo ));
47
+ udat_parseCalendar (DATE_FORMAT_OBJECT (dfo ), parsed_calendar , text_utf16 , text_utf16_len , parse_pos , & INTL_DATA_ERROR_CODE (dfo ));
48
+ if (text_utf16 ) {
49
+ efree (text_utf16 );
50
+ }
51
+ INTL_METHOD_CHECK_STATUS ( dfo , "Calendar parsing failed" );
52
+ timestamp = ucal_getMillis ( parsed_calendar , & INTL_DATA_ERROR_CODE (dfo ));
53
+ } else {
54
+ timestamp = udat_parse (DATE_FORMAT_OBJECT (dfo ), text_utf16 , text_utf16_len , parse_pos , & INTL_DATA_ERROR_CODE (dfo ));
55
+ if (text_utf16 ) {
56
+ efree (text_utf16 );
57
+ }
48
58
}
49
59
50
60
INTL_METHOD_CHECK_STATUS ( dfo , "Date parsing failed" );
51
-
52
61
/* Since return is in sec. */
53
62
result = (double )timestamp / U_MILLIS_PER_SECOND ;
54
63
if (result > (double )LONG_MAX || result < (double )LONG_MIN ) {
@@ -145,18 +154,63 @@ PHP_FUNCTION(datefmt_parse)
145
154
RETURN_FALSE ;
146
155
}
147
156
parse_pos = (int32_t )long_parse_pos ;
148
- if ((size_t )parse_pos > text_len ) {
157
+ if ((size_t )parse_pos > text_len ) {
149
158
RETURN_FALSE ;
150
159
}
151
160
}
152
- internal_parse_to_timestamp ( dfo , text_to_parse , text_len , z_parse_pos ? & parse_pos : NULL , return_value );
153
- if (z_parse_pos ) {
161
+ internal_parse_to_timestamp ( dfo , text_to_parse , text_len , z_parse_pos ? & parse_pos : NULL , false , return_value );
162
+ if (z_parse_pos ) {
154
163
zval_ptr_dtor (z_parse_pos );
155
164
ZVAL_LONG (z_parse_pos , parse_pos );
156
165
}
157
166
}
158
167
/* }}} */
159
168
169
+ PHP_METHOD (IntlDateFormatter , parseToCalendar )
170
+ {
171
+ zend_string * text_to_parse = NULL ;
172
+ zval * z_parse_pos = NULL ;
173
+ int32_t parse_pos = -1 ;
174
+
175
+ DATE_FORMAT_METHOD_INIT_VARS ;
176
+
177
+ ZEND_PARSE_PARAMETERS_START (1 , 2 )
178
+ Z_PARAM_STR (text_to_parse )
179
+ Z_PARAM_OPTIONAL
180
+ Z_PARAM_ZVAL (z_parse_pos )
181
+ ZEND_PARSE_PARAMETERS_END ();
182
+
183
+ object = ZEND_THIS ;
184
+
185
+ /* Fetch the object. */
186
+ DATE_FORMAT_METHOD_FETCH_OBJECT ;
187
+
188
+ if (z_parse_pos ) {
189
+ zend_long long_parse_pos ;
190
+ ZVAL_DEREF (z_parse_pos );
191
+ bool failed = false;
192
+ long_parse_pos = zval_try_get_long (z_parse_pos , & failed );
193
+ if (failed ) {
194
+ zend_argument_type_error (2 , "must be of type int, %s given" , zend_zval_value_name (z_parse_pos ));
195
+ RETURN_THROWS ();
196
+ }
197
+ if (ZEND_LONG_INT_OVFL (long_parse_pos )) {
198
+ intl_error_set_code (NULL , U_ILLEGAL_ARGUMENT_ERROR );
199
+ intl_error_set_custom_msg (NULL , "String index is out of valid range." , 0 );
200
+ RETURN_FALSE ;
201
+ }
202
+ parse_pos = (int32_t )long_parse_pos ;
203
+ if (parse_pos != -1 && (size_t )parse_pos > ZSTR_LEN (text_to_parse )) {
204
+ RETURN_FALSE ;
205
+ }
206
+ }
207
+ internal_parse_to_timestamp ( dfo , ZSTR_VAL (text_to_parse ), ZSTR_LEN (text_to_parse ), z_parse_pos ? & parse_pos : NULL , true, return_value );
208
+ if (z_parse_pos ) {
209
+ zval_ptr_dtor (z_parse_pos );
210
+ ZVAL_LONG (z_parse_pos , parse_pos );
211
+ }
212
+ }
213
+
160
214
/* {{{ Parse the string $value to a localtime array */
161
215
PHP_FUNCTION (datefmt_localtime )
162
216
{
0 commit comments