Back to index

php5  5.3.10
dateformat_format.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | This source file is subject to version 3.01 of the PHP license,      |
00006    | that is bundled with this package in the file LICENSE, and is        |
00007    | available through the world-wide-web at the following url:           |
00008    | http://www.php.net/license/3_01.txt                                  |
00009    | If you did not receive a copy of the PHP license and are unable to   |
00010    | obtain it through the world-wide-web, please send a note to          |
00011    | license@php.net so we can mail you a copy immediately.               |
00012    +----------------------------------------------------------------------+
00013    | Authors: Kirti Velankar <kirtig@yahoo-inc.com>                       |
00014    +----------------------------------------------------------------------+
00015 */
00016 
00017 #ifdef HAVE_CONFIG_H
00018 #include "config.h"
00019 #endif
00020 
00021 #include <unicode/ustring.h>
00022 #include <unicode/ucal.h>
00023 
00024 #include "php_intl.h"
00025 #include "intl_convert.h"
00026 #include "dateformat.h"
00027 #include "dateformat_class.h"
00028 #include "dateformat_format.h"
00029 #include "dateformat_data.h"
00030 #include "ext/date/php_date.h"
00031 
00032 /* {{{ 
00033  * Internal function which calls the udat_format
00034 */
00035 static void internal_format(IntlDateFormatter_object *dfo, UDate timestamp, zval *return_value TSRMLS_DC)
00036 {
00037        UChar*        formatted =  NULL;
00038        int32_t       resultlengthneeded =0 ;
00039        
00040        resultlengthneeded=udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, NULL, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
00041        if(INTL_DATA_ERROR_CODE(dfo)==U_BUFFER_OVERFLOW_ERROR)
00042        {
00043               INTL_DATA_ERROR_CODE(dfo)=U_ZERO_ERROR;
00044               formatted=(UChar*)emalloc(sizeof(UChar) * resultlengthneeded); 
00045               udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, formatted, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
00046        }
00047 
00048        if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(dfo) ) ) {
00049                      efree(formatted);
00050        }
00051 
00052        INTL_METHOD_CHECK_STATUS( dfo, "Date formatting failed" );
00053        INTL_METHOD_RETVAL_UTF8( dfo, formatted, resultlengthneeded, 1 );
00054 
00055 }
00056 /* }}} */
00057 
00058 
00059 /* {{{ 
00060  * Internal function which fetches an element from the passed array for the key_name passed 
00061 */
00062 static double internal_get_arr_ele(IntlDateFormatter_object *dfo, HashTable* hash_arr, char* key_name TSRMLS_DC)
00063 {
00064        zval**  ele_value       = NULL;
00065        UDate result = -1;
00066 
00067         if( zend_hash_find( hash_arr, key_name, strlen(key_name) + 1, (void **)&ele_value ) == SUCCESS ){
00068                 if( Z_TYPE_PP(ele_value)!= IS_LONG ){
00069                      intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00070                             "datefmt_format: parameter array does not contain a long element.", 0 TSRMLS_CC );
00071                 }else{
00072                      result =  Z_LVAL_PP(ele_value);
00073               }
00074        }
00075        /* printf("\n Inside internal_get_arr_ele key_name= %s, result = %g \n", key_name, result); */
00076        return result;
00077 }
00078 /* }}} */
00079 
00080 /* {{{ 
00081  * Internal function which sets UCalendar  from the passed array and retrieves timestamp
00082 */
00083 static UDate internal_get_timestamp(IntlDateFormatter_object *dfo, HashTable* hash_arr  TSRMLS_DC)
00084 {
00085        long year =0;
00086        long month =0;
00087        long hour =0;
00088        long minute =0;
00089        long second =0;
00090        long wday =0;
00091        long yday =0;
00092        long mday =0;
00093        UBool isInDST = FALSE;
00094        UCalendar *pcal;
00095 
00096        /* Fetch  values from the incoming array */
00097        year = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YEAR TSRMLS_CC) + 1900; /* tm_year is years since 1900 */
00098        /* Month in ICU and PHP starts from January =0 */
00099        month = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MON TSRMLS_CC);
00100        hour = internal_get_arr_ele( dfo, hash_arr, CALENDAR_HOUR TSRMLS_CC);
00101        minute = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MIN TSRMLS_CC);
00102        second = internal_get_arr_ele( dfo, hash_arr, CALENDAR_SEC TSRMLS_CC);
00103        wday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_WDAY TSRMLS_CC);
00104        yday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YDAY TSRMLS_CC);
00105        isInDST = internal_get_arr_ele( dfo, hash_arr, CALENDAR_ISDST TSRMLS_CC);
00106        /* For the ucal_setDateTime() function, this is the 'date'  value */
00107        mday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MDAY TSRMLS_CC);
00108 
00109        pcal = udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
00110        /* set the incoming values for the calendar */
00111        ucal_setDateTime( pcal, year, month, mday, hour, minute, second, &INTL_DATA_ERROR_CODE(dfo));
00112        if( INTL_DATA_ERROR_CODE(dfo) != U_ZERO_ERROR){
00113               return 0;
00114        }
00115        
00116        /* Fetch the timestamp from the UCalendar */
00117        return ucal_getMillis(pcal, &INTL_DATA_ERROR_CODE(dfo) );
00118 }
00119 
00120 
00121 /* {{{ proto string IntlDateFormatter::format( [mixed]int $args or array $args )
00122  * Format the time value as a string. }}}*/
00123 /* {{{ proto string datefmt_format( [mixed]int $args or array $args )
00124  * Format the time value as a string. }}}*/
00125 PHP_FUNCTION(datefmt_format) 
00126 {
00127        UDate         timestamp =0;
00128        UDate         p_timestamp =0;
00129        HashTable*      hash_arr        = NULL;
00130        zval*         zarg   = NULL;
00131 
00132        DATE_FORMAT_METHOD_INIT_VARS;
00133 
00134        /* Parse parameters. */
00135        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz", &object, IntlDateFormatter_ce_ptr,&zarg ) == FAILURE )
00136        {
00137               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: unable to parse input params", 0 TSRMLS_CC );
00138               RETURN_FALSE;
00139        }
00140 
00141        /* Fetch the object. */
00142        DATE_FORMAT_METHOD_FETCH_OBJECT;
00143 
00144        switch(Z_TYPE_P(zarg) ){
00145               case IS_LONG:
00146                      p_timestamp = Z_LVAL_P(zarg) ;
00147                      timestamp = p_timestamp * 1000;
00148                      break;
00149               case IS_DOUBLE:
00150                      /* timestamp*1000 since ICU expects it in milliseconds */
00151                      p_timestamp = Z_DVAL_P(zarg) ;
00152                      timestamp = p_timestamp * 1000;
00153                      break;
00154               case IS_ARRAY:
00155                      hash_arr = Z_ARRVAL_P(zarg);
00156                      if( !hash_arr || zend_hash_num_elements( hash_arr ) == 0 )
00157                             RETURN_FALSE;
00158 
00159                      timestamp = internal_get_timestamp(dfo, hash_arr TSRMLS_CC);
00160                      INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed" )
00161                      break;
00162               case IS_OBJECT: {
00163                      zend_class_entry *date_ce = php_date_get_date_ce();
00164                      zval retval;
00165                      zval *zfuncname;
00166                      if(!instanceof_function(Z_OBJCE_P(zarg), date_ce TSRMLS_CC)) {
00167                             intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: object must be an instance of DateTime", 0 TSRMLS_CC );
00168                             RETURN_FALSE;
00169                      }
00170                      INIT_ZVAL(retval);
00171                      MAKE_STD_ZVAL(zfuncname);
00172                      ZVAL_STRING(zfuncname, "getTimestamp", 1);
00173                      if(call_user_function(NULL, &zarg, zfuncname, &retval, 0, NULL TSRMLS_CC) != SUCCESS || Z_TYPE(retval) != IS_LONG) {
00174                             intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: cannot get timestamp", 0 TSRMLS_CC );
00175                             zval_ptr_dtor(&zfuncname);
00176                             RETURN_FALSE;
00177                      }
00178                      zval_ptr_dtor(&zfuncname);
00179                      p_timestamp = Z_LVAL(retval);
00180                      timestamp = p_timestamp*1000;
00181               }
00182                      break;
00183               default:
00184                      intl_errors_set( INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR,
00185                             "datefmt_format: takes either an array or an integer timestamp value or a DateTime object", 0 TSRMLS_CC );
00186                      RETURN_FALSE;
00187        }
00188 
00189        internal_format( dfo, timestamp, return_value TSRMLS_CC);
00190        
00191 }
00192 
00193 /* }}} */
00194