diff options
author | Stanislav Malyshev <stas@php.net> | 2008-09-04 00:15:25 +0000 |
---|---|---|
committer | Stanislav Malyshev <stas@php.net> | 2008-09-04 00:15:25 +0000 |
commit | d8c0c2203b71d4bc6cc13a256915fa4c1a98fd2c (patch) | |
tree | 17bb8643cce8c404a8bb6a4ee2b869647bea32f8 /ext/intl/dateformat | |
parent | 95d9565902659368d14a35289843a0a79db9b61d (diff) | |
download | php-git-d8c0c2203b71d4bc6cc13a256915fa4c1a98fd2c.tar.gz |
fix formatting/parsing
Diffstat (limited to 'ext/intl/dateformat')
-rwxr-xr-x | ext/intl/dateformat/dateformat_format.c | 71 | ||||
-rwxr-xr-x | ext/intl/dateformat/dateformat_parse.c | 59 |
2 files changed, 62 insertions, 68 deletions
diff --git a/ext/intl/dateformat/dateformat_format.c b/ext/intl/dateformat/dateformat_format.c index f56a397ea0..e41b38beb1 100755 --- a/ext/intl/dateformat/dateformat_format.c +++ b/ext/intl/dateformat/dateformat_format.c @@ -31,7 +31,8 @@ /* {{{ * Internal function which calls the udat_format */ -static void internal_format(IntlDateFormatter_object *dfo, UDate timestamp , zval *return_value TSRMLS_DC){ +static void internal_format(IntlDateFormatter_object *dfo, UDate timestamp, zval *return_value TSRMLS_DC) +{ UChar* formatted = NULL; int32_t resultlengthneeded =0 ; @@ -57,11 +58,12 @@ static void internal_format(IntlDateFormatter_object *dfo, UDate timestamp , zva /* {{{ * Internal function which fetches an element from the passed array for the key_name passed */ -static double internal_get_arr_ele(IntlDateFormatter_object *dfo , HashTable* hash_arr ,char* key_name TSRMLS_DC){ +static double internal_get_arr_ele(IntlDateFormatter_object *dfo, HashTable* hash_arr, char* key_name TSRMLS_DC) +{ zval** ele_value = NULL; UDate result = -1; - if( zend_hash_find( hash_arr , key_name , strlen(key_name) + 1 ,(void **)&ele_value ) == SUCCESS ){ + if( zend_hash_find( hash_arr, key_name, strlen(key_name) + 1, (void **)&ele_value ) == SUCCESS ){ if( Z_TYPE_PP(ele_value)!= IS_LONG ){ intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: parameter array does not contain a long element.", 0 TSRMLS_CC ); @@ -69,15 +71,16 @@ static double internal_get_arr_ele(IntlDateFormatter_object *dfo , HashTable* h result = Z_LVAL_PP(ele_value); } } - /* printf("\n Inside internal_get_arr_ele key_name= %s , result = %g \n" , key_name, result); */ + /* printf("\n Inside internal_get_arr_ele key_name= %s, result = %g \n", key_name, result); */ return result; } /* }}} */ /* {{{ - * Internal function which creates a UCalendar from the passed array + * Internal function which sets UCalendar from the passed array and retrieves timestamp */ -static void internal_create_ucal(IntlDateFormatter_object *dfo, HashTable* hash_arr , UCalendar* pcal TSRMLS_DC){ +static UDate internal_get_timestamp(IntlDateFormatter_object *dfo, HashTable* hash_arr TSRMLS_DC) +{ long year =0; long month =0; long hour =0; @@ -87,31 +90,30 @@ static void internal_create_ucal(IntlDateFormatter_object *dfo, HashTable* hash_ long yday =0; long mday =0; UBool isInDST = FALSE; + UCalendar *pcal; /* Fetch values from the incoming array */ - year = internal_get_arr_ele( dfo , hash_arr , CALENDAR_YEAR TSRMLS_CC) + 1900; /* tm_year is years since 1900 */ + year = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YEAR TSRMLS_CC) + 1900; /* tm_year is years since 1900 */ /* Month in ICU and PHP starts from January =0 */ - month = internal_get_arr_ele( dfo , hash_arr , CALENDAR_MON TSRMLS_CC); - hour = internal_get_arr_ele( dfo , hash_arr , CALENDAR_HOUR TSRMLS_CC); - minute = internal_get_arr_ele( dfo , hash_arr , CALENDAR_MIN TSRMLS_CC); - second = internal_get_arr_ele( dfo , hash_arr , CALENDAR_SEC TSRMLS_CC); - wday = internal_get_arr_ele( dfo , hash_arr , CALENDAR_WDAY TSRMLS_CC); - yday = internal_get_arr_ele( dfo , hash_arr , CALENDAR_YDAY TSRMLS_CC); - isInDST = internal_get_arr_ele( dfo , hash_arr , CALENDAR_ISDST TSRMLS_CC); - /* For the ucal_setDateTime() function , this is the 'date' value */ - mday = internal_get_arr_ele( dfo , hash_arr , CALENDAR_MDAY TSRMLS_CC); - + month = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MON TSRMLS_CC); + hour = internal_get_arr_ele( dfo, hash_arr, CALENDAR_HOUR TSRMLS_CC); + minute = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MIN TSRMLS_CC); + second = internal_get_arr_ele( dfo, hash_arr, CALENDAR_SEC TSRMLS_CC); + wday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_WDAY TSRMLS_CC); + yday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YDAY TSRMLS_CC); + isInDST = internal_get_arr_ele( dfo, hash_arr, CALENDAR_ISDST TSRMLS_CC); + /* For the ucal_setDateTime() function, this is the 'date' value */ + mday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MDAY TSRMLS_CC); + + pcal = udat_getCalendar(DATE_FORMAT_OBJECT(dfo)); /* set the incoming values for the calendar */ - ucal_setDateTime( pcal, year, month , mday , hour , minute , second , &INTL_DATA_ERROR_CODE(dfo)); + ucal_setDateTime( pcal, year, month, mday, hour, minute, second, &INTL_DATA_ERROR_CODE(dfo)); if( INTL_DATA_ERROR_CODE(dfo) != U_ZERO_ERROR){ - return; + return 0; } - /* ICU UCAL_DAY_OF_WEEK starts from SUNDAY=1 thru SATURDAY=7 - * whereas PHP localtime has tm_wday SUNDAY=0 thru SATURDAY=6 */ - ucal_set( pcal, UCAL_DAY_OF_WEEK , (wday+1)); - ucal_set( pcal, UCAL_DAY_OF_YEAR , yday); - /* TO DO: How to set the isInDST field?Is it required to set */ + /* Fetch the timestamp from the UCalendar */ + return ucal_getMillis(pcal, &INTL_DATA_ERROR_CODE(dfo) ); } @@ -123,14 +125,13 @@ PHP_FUNCTION(datefmt_format) { UDate timestamp =0; UDate p_timestamp =0; - UCalendar* temp_cal ; HashTable* hash_arr = NULL; zval* zarg = NULL; DATE_FORMAT_METHOD_INIT_VARS; /* Parse parameters. */ - if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz", &object, IntlDateFormatter_ce_ptr ,&zarg ) == FAILURE ) + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz", &object, IntlDateFormatter_ce_ptr,&zarg ) == FAILURE ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: unable to parse input params", 0 TSRMLS_CC ); RETURN_FALSE; @@ -153,27 +154,17 @@ PHP_FUNCTION(datefmt_format) hash_arr = Z_ARRVAL_P(zarg); if( !hash_arr || zend_hash_num_elements( hash_arr ) == 0 ) RETURN_FALSE; - /* Create a UCalendar object from the array and then format it */ - temp_cal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &INTL_DATA_ERROR_CODE(dfo)); - ucal_clear(temp_cal); - INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed while creating calendar from the array" ) - internal_create_ucal( dfo , hash_arr , temp_cal TSRMLS_CC); - INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed while creating calendar from the array" ) - /* Fetch the timestamp from the created UCalendar */ - timestamp = ucal_getMillis(temp_cal , &INTL_DATA_ERROR_CODE(dfo) ); + + timestamp = internal_get_timestamp(dfo, hash_arr TSRMLS_CC); INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed" ) break; -/* - case IS_OBJECT: - break; -*/ default: intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, - "datefmt_format: takes either an array or an integer TimeStamp value ", 0 TSRMLS_CC ); + "datefmt_format: takes either an array or an integer timestamp value ", 0 TSRMLS_CC ); RETURN_FALSE; } - internal_format( dfo, timestamp ,return_value TSRMLS_CC); + internal_format( dfo, timestamp, return_value TSRMLS_CC); } diff --git a/ext/intl/dateformat/dateformat_parse.c b/ext/intl/dateformat/dateformat_parse.c index 3e05050760..c17089f1ca 100755 --- a/ext/intl/dateformat/dateformat_parse.c +++ b/ext/intl/dateformat/dateformat_parse.c @@ -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); |