Back to index

php5  5.3.10
dateformat.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 <unicode/ustring.h>
00021 #include <unicode/udat.h>
00022 #include <unicode/ucal.h>
00023 
00024 #include "php_intl.h"
00025 #include "intl_convert.h"
00026 #include "dateformat_class.h"
00027 #include "dateformat.h"
00028 
00029 /* {{{ dateformat_register_constants
00030  * Register constants common for the both (OO and procedural)
00031  * APIs.
00032  */
00033 void dateformat_register_constants( INIT_FUNC_ARGS )
00034 {
00035        if( IntlDateFormatter_ce_ptr == NULL) {
00036               zend_error(E_ERROR, "DateFormat class not defined");
00037               return;
00038        }
00039 
00040        #define DATEFORMATTER_EXPOSE_CONST(x) REGISTER_LONG_CONSTANT(#x, x, CONST_CS)
00041        #define DATEFORMATTER_EXPOSE_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UDAT_##x TSRMLS_CC );
00042        #define DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST(name, value) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( name ) - 1, value TSRMLS_CC );
00043 
00044        #define DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UCAL_##x TSRMLS_CC );
00045 
00046        /* UDateFormatStyle constants */
00047        DATEFORMATTER_EXPOSE_CLASS_CONST( FULL );
00048        DATEFORMATTER_EXPOSE_CLASS_CONST( LONG );
00049        DATEFORMATTER_EXPOSE_CLASS_CONST( MEDIUM );
00050        DATEFORMATTER_EXPOSE_CLASS_CONST( SHORT );
00051        DATEFORMATTER_EXPOSE_CLASS_CONST( NONE );
00052 
00053 /*
00054        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "GREGORIAN", DATEF_GREGORIAN );
00055        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "CUSTOMARY", DATEF_CUSTOMARY );
00056        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "BUDDHIST", DATEF_BUDDHIST );
00057        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "JAPANESE_IMPERIAL", DATEF_JAPANESE_IMPERIAL );
00058 */
00059 
00060        DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( GREGORIAN );
00061        DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( TRADITIONAL );
00062 
00063        #undef DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST
00064        #undef DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST
00065        #undef DATEFORMATTER_EXPOSE_CLASS_CONST
00066        #undef DATEFORMATTER_EXPOSE_CONST
00067 }
00068 /* }}} */
00069 
00070 /* {{{ */
00071 static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
00072 {
00073     char*       locale;
00074        int         locale_len = 0;
00075        zval*       object;
00076     long        date_type = 0;
00077     long        time_type = 0;
00078     long        calendar = UCAL_GREGORIAN;
00079     char*       timezone_str = NULL;
00080     int         timezone_str_len = 0;
00081     char*       pattern_str = NULL;
00082     int         pattern_str_len = 0;
00083     UChar*      svalue = NULL;            /* UTF-16 pattern_str */
00084     int         slength = 0;
00085     UChar*      timezone_utf16 = NULL;           /* UTF-16 timezone_str */
00086     int         timezone_utf16_len = 0;
00087        UCalendar   ucal_obj = NULL;
00088        IntlDateFormatter_object* dfo;
00089        
00090        intl_error_reset( NULL TSRMLS_CC );
00091        object = return_value;
00092        /* Parse parameters. */
00093     if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sll|sls",
00094               &locale, &locale_len, &date_type, &time_type, &timezone_str, &timezone_str_len, &calendar,&pattern_str, &pattern_str_len ) == FAILURE )
00095     {
00096               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  "datefmt_create: unable to parse input parameters", 0 TSRMLS_CC );
00097               zval_dtor(return_value);
00098               RETURN_NULL();
00099     }
00100 
00101        INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
00102        DATE_FORMAT_METHOD_FETCH_OBJECT;
00103        /* Convert pattern (if specified) to UTF-16. */
00104        if( pattern_str && pattern_str_len>0 ){
00105               intl_convert_utf8_to_utf16(&svalue, &slength, pattern_str, pattern_str_len, &INTL_DATA_ERROR_CODE(dfo));
00106               INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: error converting pattern to UTF-16");
00107        }
00108 
00109        /* Convert pattern (if specified) to UTF-16. */
00110        if( timezone_str && timezone_str_len >0 ){
00111               intl_convert_utf8_to_utf16(&timezone_utf16, &timezone_utf16_len, timezone_str, timezone_str_len, &INTL_DATA_ERROR_CODE(dfo));
00112               INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: error converting timezone_str to UTF-16" );
00113        }
00114 
00115        if(locale_len == 0) {
00116               locale = INTL_G(default_locale);
00117        }
00118 
00119        if( pattern_str && pattern_str_len>0 ){
00120               DATE_FORMAT_OBJECT(dfo) = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, timezone_utf16, timezone_utf16_len, svalue, slength, &INTL_DATA_ERROR_CODE(dfo));
00121        } else {
00122               DATE_FORMAT_OBJECT(dfo) = udat_open(time_type, date_type, locale, timezone_utf16, timezone_utf16_len, svalue, slength, &INTL_DATA_ERROR_CODE(dfo));
00123        }
00124 
00125     /* Set the calendar if passed */
00126     if(!U_FAILURE(INTL_DATA_ERROR_CODE(dfo)) && calendar) {
00127               ucal_obj = ucal_open( timezone_utf16, timezone_utf16_len, locale, calendar, &INTL_DATA_ERROR_CODE(dfo) );
00128               if(!U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
00129                      udat_setCalendar( DATE_FORMAT_OBJECT(dfo), ucal_obj );
00130               }
00131     }
00132 
00133        if(svalue)
00134        {
00135               efree(svalue);
00136        }
00137        if(timezone_utf16)
00138        {
00139               efree(timezone_utf16);
00140        }
00141 
00142        INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: date formatter creation failed");
00143 
00144        /* Set the class variables */
00145        dfo->date_type = date_type;
00146        dfo->time_type = time_type;
00147        dfo->calendar  = calendar;
00148        if( timezone_str && timezone_str_len > 0){
00149               dfo->timezone_id = estrndup( timezone_str, timezone_str_len);
00150        }
00151 }
00152 /* }}} */
00153 
00154 /* {{{ proto IntlDateFormatter IntlDateFormatter::create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern] )
00155  * Create formatter. }}} */
00156 /* {{{ proto IntlDateFormatter datefmt_create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern] )
00157  
00158  * Create formatter.
00159  */
00160 PHP_FUNCTION( datefmt_create )
00161 {
00162     object_init_ex( return_value, IntlDateFormatter_ce_ptr );
00163        datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
00164 }
00165 /* }}} */
00166 
00167 /* {{{ proto void IntlDateFormatter::__construct(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern])
00168  * IntlDateFormatter object constructor.
00169  */
00170 PHP_METHOD( IntlDateFormatter, __construct )
00171 {
00172        return_value = getThis();
00173        datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
00174 }
00175 /* }}} */
00176 
00177 /* {{{ proto int IntlDateFormatter::getErrorCode()
00178  * Get formatter's last error code. }}} */
00179 /* {{{ proto int datefmt_get_error_code( IntlDateFormatter $nf )
00180  * Get formatter's last error code.
00181  */
00182 PHP_FUNCTION( datefmt_get_error_code )
00183 {
00184        DATE_FORMAT_METHOD_INIT_VARS;
00185 
00186        /* Parse parameters. */
00187        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00188               &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00189        {
00190               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00191                      "datefmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
00192               RETURN_FALSE;
00193        }
00194 
00195        dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
00196 
00197        /* Return formatter's last error code. */
00198        RETURN_LONG( INTL_DATA_ERROR_CODE(dfo) );
00199 }
00200 /* }}} */
00201 
00202 /* {{{ proto string IntlDateFormatter::getErrorMessage( )
00203  * Get text description for formatter's last error code. }}} */
00204 /* {{{ proto string datefmt_get_error_message( IntlDateFormatter $coll )
00205  * Get text description for formatter's last error code.
00206  */
00207 PHP_FUNCTION( datefmt_get_error_message )
00208 {
00209        char*                    message = NULL;
00210        DATE_FORMAT_METHOD_INIT_VARS;
00211 
00212        /* Parse parameters. */
00213        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00214               &object, IntlDateFormatter_ce_ptr ) == FAILURE )
00215        {
00216               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00217                      "datefmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
00218 
00219               RETURN_FALSE;
00220        }
00221 
00222        dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
00223 
00224        /* Return last error message. */
00225        message = intl_error_get_message( INTL_DATA_ERROR_P(dfo) TSRMLS_CC );
00226        RETURN_STRING( message, 0);
00227 }
00228 /* }}} */