fix formatting/parsing

This commit is contained in:
Stanislav Malyshev 2008-09-04 00:15:25 +00:00
parent 95d9565902
commit d8c0c2203b
2 changed files with 61 additions and 67 deletions

View file

@ -33,17 +33,18 @@
* if set to 1 - store any error encountered in the parameter parse_error
* if set to 0 - no need to store any error encountered in the parameter parse_error
*/
static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse , int32_t text_len, int32_t *parse_pos , zval *return_value TSRMLS_DC){
static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value TSRMLS_DC)
{
long result = 0;
UDate timestamp =0;
UChar* text_utf16 = NULL;
int32_t text_utf16_len = 0;
/* Convert timezone to UTF-16. */
intl_convert_utf8_to_utf16(&text_utf16 , &text_utf16_len , text_to_parse , text_len, &INTL_DATA_ERROR_CODE(dfo));
intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16 , text_utf16_len , parse_pos , &INTL_DATA_ERROR_CODE(dfo));
timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if( text_utf16 ){
efree(text_utf16);
}
@ -60,36 +61,38 @@ static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* tex
}
/* }}} */
static void add_to_localtime_arr( IntlDateFormatter_object *dfo, zval* return_value ,UCalendar parsed_calendar , long calendar_field , char* key_name TSRMLS_DC){
long calendar_field_val = ucal_get( parsed_calendar , calendar_field , &INTL_DATA_ERROR_CODE(dfo));
static void add_to_localtime_arr( IntlDateFormatter_object *dfo, zval* return_value, UCalendar parsed_calendar, long calendar_field, char* key_name TSRMLS_DC)
{
long calendar_field_val = ucal_get( parsed_calendar, calendar_field, &INTL_DATA_ERROR_CODE(dfo));
INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : could not get a field from calendar" );
if( strcmp(key_name , CALENDAR_YEAR )==0 ){
if( strcmp(key_name, CALENDAR_YEAR )==0 ){
/* since tm_year is years from 1900 */
add_assoc_long( return_value, key_name ,( calendar_field_val-1900) );
}else if( strcmp(key_name , CALENDAR_WDAY )==0 ){
add_assoc_long( return_value, key_name,( calendar_field_val-1900) );
}else if( strcmp(key_name, CALENDAR_WDAY )==0 ){
/* since tm_wday starts from 0 whereas ICU WDAY start from 1 */
add_assoc_long( return_value, key_name ,( calendar_field_val-1) );
add_assoc_long( return_value, key_name,( calendar_field_val-1) );
}else{
add_assoc_long( return_value, key_name , calendar_field_val );
add_assoc_long( return_value, key_name, calendar_field_val );
}
}
/* {{{
* Internal function which calls the udat_parseCalendar
*/
static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* text_to_parse , int32_t text_len, int32_t *parse_pos , zval *return_value TSRMLS_DC){
static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value TSRMLS_DC)
{
UCalendar* parsed_calendar = NULL;
UChar* text_utf16 = NULL;
int32_t text_utf16_len = 0;
long isInDST = 0;
/* Convert timezone to UTF-16. */
intl_convert_utf8_to_utf16(&text_utf16 , &text_utf16_len , text_to_parse , text_len, &INTL_DATA_ERROR_CODE(dfo));
intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
parsed_calendar = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &INTL_DATA_ERROR_CODE(dfo));
udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar , text_utf16 , text_utf16_len , parse_pos , &INTL_DATA_ERROR_CODE(dfo));
parsed_calendar = udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if (text_utf16) {
efree(text_utf16);
@ -100,19 +103,19 @@ static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* tex
array_init( return_value );
/* Add entries from various fields of the obtained parsed_calendar */
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_SECOND , CALENDAR_SEC TSRMLS_CC);
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_MINUTE , CALENDAR_MIN TSRMLS_CC);
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_HOUR_OF_DAY , CALENDAR_HOUR TSRMLS_CC);
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_YEAR , CALENDAR_YEAR TSRMLS_CC);
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_DAY_OF_MONTH , CALENDAR_MDAY TSRMLS_CC);
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_DAY_OF_WEEK , CALENDAR_WDAY TSRMLS_CC);
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_DAY_OF_YEAR , CALENDAR_YDAY TSRMLS_CC);
add_to_localtime_arr( dfo , return_value , parsed_calendar , UCAL_MONTH , CALENDAR_MON TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_SECOND, CALENDAR_SEC TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MINUTE, CALENDAR_MIN TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_HOUR_OF_DAY, CALENDAR_HOUR TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_YEAR, CALENDAR_YEAR TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_MONTH, CALENDAR_MDAY TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_WEEK, CALENDAR_WDAY TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_YEAR, CALENDAR_YDAY TSRMLS_CC);
add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MONTH, CALENDAR_MON TSRMLS_CC);
/* Is in DST? */
isInDST = ucal_inDaylightTime(parsed_calendar , &INTL_DATA_ERROR_CODE(dfo));
isInDST = ucal_inDaylightTime(parsed_calendar , &INTL_DATA_ERROR_CODE(dfo));
INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : while checking if currently in DST." );
add_assoc_long( return_value, CALENDAR_ISDST ,(isInDST==1?1:0));
add_assoc_long( return_value, CALENDAR_ISDST,(isInDST==1?1:0));
}
/* }}} */
@ -132,7 +135,7 @@ PHP_FUNCTION(datefmt_parse)
/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|z!",
&object, IntlDateFormatter_ce_ptr, &text_to_parse , &text_len , &z_parse_pos ) == FAILURE ){
&object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_parse: unable to parse input params", 0 TSRMLS_CC );
RETURN_FALSE;
}
@ -147,7 +150,7 @@ PHP_FUNCTION(datefmt_parse)
RETURN_FALSE;
}
}
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
if(z_parse_pos) {
zval_dtor(z_parse_pos);
ZVAL_LONG(z_parse_pos, parse_pos);
@ -170,7 +173,7 @@ PHP_FUNCTION(datefmt_localtime)
/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|z!",
&object, IntlDateFormatter_ce_ptr, &text_to_parse , &text_len , &z_parse_pos ) == FAILURE ){
&object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_parse_to_localtime: unable to parse input params", 0 TSRMLS_CC );
RETURN_FALSE;
}
@ -185,7 +188,7 @@ PHP_FUNCTION(datefmt_localtime)
RETURN_FALSE;
}
}
internal_parse_to_localtime( dfo, text_to_parse , text_len , z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
internal_parse_to_localtime( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
if(z_parse_pos) {
zval_dtor(z_parse_pos);
ZVAL_LONG(z_parse_pos, parse_pos);