Back to index

php5  5.3.10
dateformat_attr.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 #ifdef HAVE_CONFIG_H
00017 #include "config.h"
00018 #endif
00019 
00020 #include "php_intl.h"
00021 #include "intl_convert.h"
00022 #include "dateformat_class.h"
00023 #include "dateformat_attr.h"
00024 
00025 #include <unicode/ustring.h>
00026 #include <unicode/udat.h>
00027 #include <unicode/ucal.h>
00028 
00029 static void internal_set_calendar(IntlDateFormatter_object *dfo, char* timezone_id, int timezone_id_len, int calendar, zval* return_value TSRMLS_DC){
00030        int         timezone_utf16_len = 0;
00031        UChar*      timezone_utf16  = NULL; /* timezone_id in UTF-16 */
00032        char*       locale = NULL;
00033 
00034        UCalendar*   ucal_obj = NULL;
00035 
00036        /* check for the validity  of value of calendar passed */
00037        intl_error_reset( NULL TSRMLS_CC );
00038        if( calendar > 1){
00039               intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
00040                      "datefmt_set_calendar: calendar value specified is out of valid range", 0 TSRMLS_CC);
00041               RETURN_FALSE;
00042        }
00043 
00044        /* Convert timezone to UTF-16. */
00045        intl_convert_utf8_to_utf16(&timezone_utf16, &timezone_utf16_len, timezone_id, timezone_id_len, &INTL_DATA_ERROR_CODE(dfo));
00046        INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
00047 
00048        /* Get the locale for the dateformatter */
00049        locale = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(dfo));
00050 
00051        /* Set the calendar if passed */
00052        ucal_obj = ucal_open(timezone_utf16, timezone_utf16_len, locale, calendar, &INTL_DATA_ERROR_CODE(dfo) );
00053        udat_setCalendar( DATE_FORMAT_OBJECT(dfo), ucal_obj );
00054        INTL_METHOD_CHECK_STATUS(dfo, "Error setting the calendar.");
00055 
00056        if( timezone_utf16){
00057               efree(timezone_utf16);
00058        }
00059 }
00060 
00061 /* {{{ proto unicode IntlDateFormatter::getDateType( )
00062  * Get formatter datetype. }}} */
00063 /* {{{ proto string datefmt_get_datetype( IntlDateFormatter $mf )
00064  * Get formatter datetype.
00065  */
00066 PHP_FUNCTION( datefmt_get_datetype )
00067 {
00068        DATE_FORMAT_METHOD_INIT_VARS;
00069 
00070        /* Parse parameters. */
00071        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00072        {
00073               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  
00074                      "datefmt_get_datetype: unable to parse input params", 0 TSRMLS_CC );
00075               RETURN_FALSE;
00076        }
00077 
00078        /* Fetch the object. */
00079        DATE_FORMAT_METHOD_FETCH_OBJECT;
00080 
00081        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter datetype." );
00082 
00083        RETURN_LONG(dfo->date_type );
00084 }
00085 /* }}} */
00086 
00087 /* {{{ proto unicode IntlDateFormatter::getTimeType( )
00088  * Get formatter timetype. }}} */
00089 /* {{{ proto string datefmt_get_timetype( IntlDateFormatter $mf )
00090  * Get formatter timetype.
00091  */
00092 PHP_FUNCTION( datefmt_get_timetype )
00093 {
00094        DATE_FORMAT_METHOD_INIT_VARS;
00095 
00096        /* Parse parameters. */
00097        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00098        {
00099               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  
00100                      "datefmt_get_timetype: unable to parse input params", 0 TSRMLS_CC );
00101               RETURN_FALSE;
00102        }
00103 
00104        /* Fetch the object. */
00105        DATE_FORMAT_METHOD_FETCH_OBJECT;
00106 
00107        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timetype." );
00108 
00109        RETURN_LONG(dfo->time_type );
00110 }
00111 /* }}} */
00112 
00113 
00114 /* {{{ proto unicode IntlDateFormatter::getCalendar( )
00115  * Get formatter calendar. }}} */
00116 /* {{{ proto string datefmt_get_calendar( IntlDateFormatter $mf )
00117  * Get formatter calendar.
00118  */
00119 PHP_FUNCTION( datefmt_get_calendar )
00120 {
00121        DATE_FORMAT_METHOD_INIT_VARS;
00122 
00123        /* Parse parameters. */
00124        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00125        {
00126               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  
00127                      "datefmt_get_calendar: unable to parse input params", 0 TSRMLS_CC );
00128               RETURN_FALSE;
00129        }
00130 
00131        /* Fetch the object. */
00132        DATE_FORMAT_METHOD_FETCH_OBJECT;
00133 
00134        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter calendar." );
00135 
00136        RETURN_LONG(dfo->calendar);
00137 }
00138 /* }}} */
00139 
00140 /* {{{ proto unicode IntlDateFormatter::getTimeZoneId( )
00141  * Get formatter timezone_id. }}} */
00142 /* {{{ proto string datefmt_get_timezone_id( IntlDateFormatter $mf )
00143  * Get formatter timezone_id.
00144  */
00145 PHP_FUNCTION( datefmt_get_timezone_id )
00146 {
00147        DATE_FORMAT_METHOD_INIT_VARS;
00148 
00149        /* Parse parameters. */
00150        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00151        {
00152               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  
00153                      "datefmt_get_timezone_id: unable to parse input params", 0 TSRMLS_CC );
00154               RETURN_FALSE;
00155        }
00156 
00157        /* Fetch the object. */
00158        DATE_FORMAT_METHOD_FETCH_OBJECT;
00159 
00160        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timezone_id." );
00161 
00162        if( dfo->timezone_id ){
00163               RETURN_STRING((char*)dfo->timezone_id, TRUE );
00164        }else{
00165               RETURN_NULL();
00166        }
00167 }
00168 
00169 /* {{{ proto boolean IntlDateFormatter::setTimeZoneId( $timezone_id)
00170  * Set formatter timezone_id. }}} */
00171 /* {{{ proto boolean datefmt_set_timezone_id( IntlDateFormatter $mf,$timezone_id)
00172  * Set formatter timezone_id.
00173  */
00174 PHP_FUNCTION( datefmt_set_timezone_id )
00175 {
00176        char*         timezone_id          = NULL;
00177        int           timezone_id_len      = 0;
00178 
00179        DATE_FORMAT_METHOD_INIT_VARS;
00180 
00181        /* Parse parameters. */
00182        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &object, IntlDateFormatter_ce_ptr,&timezone_id, &timezone_id_len) == FAILURE )
00183        {
00184               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00185                      "datefmt_set_timezone_id: unable to parse input params", 0 TSRMLS_CC );
00186               RETURN_FALSE;
00187        }
00188 
00189        /* Fetch the object. */
00190        DATE_FORMAT_METHOD_FETCH_OBJECT;
00191 
00192        /* set the timezone for the calendar */
00193        internal_set_calendar( dfo, timezone_id, timezone_id_len, dfo->calendar, return_value TSRMLS_CC );
00194 
00195        /* Set the IntlDateFormatter variable */
00196         if( dfo->timezone_id ){
00197               efree(dfo->timezone_id);
00198        }
00199        dfo->timezone_id = estrndup(timezone_id, timezone_id_len);
00200 
00201        RETURN_TRUE;
00202 }
00203 
00204 /* {{{ proto string IntlDateFormatter::getPattern( )
00205  * Get formatter pattern. }}} */
00206 /* {{{ proto string datefmt_get_pattern( IntlDateFormatter $mf )
00207  * Get formatter pattern.
00208  */
00209 PHP_FUNCTION( datefmt_get_pattern )
00210 {
00211        UChar  value_buf[64];
00212        int    length = USIZE( value_buf );
00213        UChar* value  = value_buf;
00214        zend_bool   is_pattern_localized =FALSE;
00215 
00216        DATE_FORMAT_METHOD_INIT_VARS;
00217 
00218        /* Parse parameters. */
00219        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00220        {
00221               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  
00222                      "datefmt_get_pattern: unable to parse input params", 0 TSRMLS_CC );
00223               RETURN_FALSE;
00224        }
00225 
00226        /* Fetch the object. */
00227        DATE_FORMAT_METHOD_FETCH_OBJECT;
00228 
00229        length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo));
00230        if(INTL_DATA_ERROR_CODE(dfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
00231               ++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
00232               INTL_DATA_ERROR_CODE(dfo) = U_ZERO_ERROR;
00233               value = eumalloc(length);
00234               length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo) );
00235               if(U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
00236                      efree(value);
00237                      value = value_buf;
00238               }
00239        }
00240        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter pattern" );
00241 
00242        INTL_METHOD_RETVAL_UTF8( dfo, value, length, ( value != value_buf ) );
00243 }
00244 /* }}} */
00245 
00246 /* {{{ proto bool IntlDateFormatter::setPattern( string $pattern )
00247  * Set formatter pattern. }}} */
00248 /* {{{ proto bool datefmt_set_pattern( IntlDateFormatter $mf, string $pattern )
00249  * Set formatter pattern.
00250  */
00251 PHP_FUNCTION( datefmt_set_pattern )
00252 {
00253        char*       value = NULL;
00254        int         value_len = 0;
00255        int         slength = 0;
00256        UChar*     svalue  = NULL;
00257        zend_bool   is_pattern_localized =FALSE;
00258 
00259 
00260        DATE_FORMAT_METHOD_INIT_VARS;
00261 
00262        /* Parse parameters. */
00263        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
00264               &object, IntlDateFormatter_ce_ptr,  &value, &value_len ) == FAILURE )
00265        {
00266               intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,   
00267                      "datefmt_set_pattern: unable to parse input params", 0 TSRMLS_CC);
00268               RETURN_FALSE;
00269        }
00270 
00271        DATE_FORMAT_METHOD_FETCH_OBJECT;
00272 
00273        /* Convert given pattern to UTF-16. */
00274        intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(dfo));
00275        INTL_METHOD_CHECK_STATUS(dfo, "Error converting pattern to UTF-16" );
00276 
00277        udat_applyPattern(DATE_FORMAT_OBJECT(dfo), (UBool)is_pattern_localized, svalue, slength);
00278 
00279        if (svalue) {
00280               efree(svalue);
00281        }
00282        INTL_METHOD_CHECK_STATUS(dfo, "Error setting symbol value");
00283 
00284        RETURN_TRUE;
00285 }
00286 /* }}} */
00287 
00288 /* {{{ proto string IntlDateFormatter::getLocale()
00289  * Get formatter locale. }}} */
00290 /* {{{ proto string datefmt_get_locale(IntlDateFormatter $mf)
00291  * Get formatter locale.
00292  */
00293 PHP_FUNCTION( datefmt_get_locale )
00294 {
00295        char *loc;
00296        long  loc_type =ULOC_ACTUAL_LOCALE;
00297 
00298        DATE_FORMAT_METHOD_INIT_VARS;
00299 
00300        /* Parse parameters. */
00301        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l",
00302               &object, IntlDateFormatter_ce_ptr,&loc_type) == FAILURE )
00303        {
00304               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00305                      "datefmt_get_locale: unable to parse input params", 0 TSRMLS_CC );
00306 
00307               RETURN_FALSE;
00308        }
00309 
00310        /* Fetch the object. */
00311        DATE_FORMAT_METHOD_FETCH_OBJECT;
00312 
00313        loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), loc_type,&INTL_DATA_ERROR_CODE(dfo));
00314        INTL_METHOD_CHECK_STATUS(dfo, "Error getting locale");
00315        RETURN_STRING(loc, 1);
00316 }
00317 /* }}} */
00318 
00319 /* {{{ proto string IntlDateFormatter::isLenient()
00320  * Get formatter isLenient. }}} */
00321 /* {{{ proto string datefmt_isLenient(IntlDateFormatter $mf)
00322  * Get formatter locale.
00323  */
00324 PHP_FUNCTION( datefmt_is_lenient )
00325 {
00326        
00327        DATE_FORMAT_METHOD_INIT_VARS;
00328 
00329        /* Parse parameters. */
00330        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00331               &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00332        {
00333               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00334                      "datefmt_is_lenient: unable to parse input params", 0 TSRMLS_CC );
00335 
00336               RETURN_FALSE;
00337        }
00338 
00339        /* Fetch the object. */
00340        DATE_FORMAT_METHOD_FETCH_OBJECT;
00341 
00342        RETVAL_BOOL(udat_isLenient(DATE_FORMAT_OBJECT(dfo)));
00343 }
00344 /* }}} */
00345 
00346 /* {{{ proto string IntlDateFormatter::setLenient()
00347  * Set formatter lenient. }}} */
00348 /* {{{ proto string datefmt_setLenient(IntlDateFormatter $mf)
00349  * Set formatter lenient.
00350  */
00351 PHP_FUNCTION( datefmt_set_lenient )
00352 {
00353        zend_bool isLenient  = FALSE;
00354 
00355        DATE_FORMAT_METHOD_INIT_VARS;
00356 
00357        /* Parse parameters. */
00358        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ob",
00359        &object, IntlDateFormatter_ce_ptr,&isLenient ) == FAILURE )
00360        {
00361               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00362                      "datefmt_set_lenient: unable to parse input params", 0 TSRMLS_CC );
00363               RETURN_FALSE;
00364        }
00365 
00366        /* Fetch the object. */
00367        DATE_FORMAT_METHOD_FETCH_OBJECT;
00368 
00369        udat_setLenient(DATE_FORMAT_OBJECT(dfo), (UBool)isLenient );
00370 }
00371 /* }}} */
00372 
00373 /* {{{ proto bool IntlDateFormatter::setPattern( int $calendar )
00374  * Set formatter calendar. }}} */
00375 /* {{{ proto bool datefmt_set_calendar( IntlDateFormatter $mf, int $calendar )
00376  * Set formatter calendar.
00377  */
00378 PHP_FUNCTION( datefmt_set_calendar )
00379 {
00380        long   calendar = 0;
00381 
00382        DATE_FORMAT_METHOD_INIT_VARS;
00383 
00384        /* Parse parameters. */
00385        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
00386               &object, IntlDateFormatter_ce_ptr, &calendar ) == FAILURE ) {
00387               intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
00388                      "datefmt_set_calendar: unable to parse input params", 0 TSRMLS_CC);
00389               RETURN_FALSE;
00390        }
00391 
00392        /* check for the validity  of value of calendar passed */
00393        intl_error_reset( NULL TSRMLS_CC );
00394        if (calendar > 1) {
00395               intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
00396                      "datefmt_set_calendar: calendar value specified is out of valid range", 0 TSRMLS_CC);
00397               RETURN_FALSE;
00398        }
00399 
00400        DATE_FORMAT_METHOD_FETCH_OBJECT;
00401 
00402        internal_set_calendar( dfo, dfo->timezone_id, strlen(dfo->timezone_id), calendar, return_value TSRMLS_CC );
00403 
00404        /* Set the calendar  value in the IntlDateFormatter object */
00405        dfo->calendar = calendar;
00406 
00407        RETURN_TRUE;
00408 }
00409 /* }}} */
00410 
00411