Annotation of embedaddon/php/ext/intl/dateformat/dateformat_attr.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:    +----------------------------------------------------------------------+
                      3:    | PHP Version 5                                                        |
                      4:    +----------------------------------------------------------------------+
                      5:    | This source file is subject to version 3.01 of the PHP license,      |
                      6:    | that is bundled with this package in the file LICENSE, and is        |
                      7:    | available through the world-wide-web at the following url:           |
                      8:    | http://www.php.net/license/3_01.txt                                  |
                      9:    | If you did not receive a copy of the PHP license and are unable to   |
                     10:    | obtain it through the world-wide-web, please send a note to          |
                     11:    | license@php.net so we can mail you a copy immediately.               |
                     12:    +----------------------------------------------------------------------+
                     13:    | Authors: Kirti Velankar <kirtig@yahoo-inc.com>                       |
                     14:    +----------------------------------------------------------------------+
                     15: */
                     16: #ifdef HAVE_CONFIG_H
                     17: #include "config.h"
                     18: #endif
                     19: 
                     20: #include "php_intl.h"
                     21: #include "intl_convert.h"
                     22: #include "dateformat_class.h"
                     23: #include "dateformat_attr.h"
                     24: 
                     25: #include <unicode/ustring.h>
                     26: #include <unicode/udat.h>
                     27: #include <unicode/ucal.h>
                     28: 
                     29: static void internal_set_calendar(IntlDateFormatter_object *dfo, char* timezone_id, int timezone_id_len, int calendar, zval* return_value TSRMLS_DC){
                     30:        int         timezone_utf16_len = 0;
                     31:        UChar*      timezone_utf16  = NULL; /* timezone_id in UTF-16 */
                     32:        char*       locale = NULL;
                     33: 
                     34:        UCalendar*   ucal_obj = NULL;
                     35: 
                     36:        /* check for the validity  of value of calendar passed */
                     37:        intl_error_reset( NULL TSRMLS_CC );
                     38:        if( calendar > 1){
                     39:                intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
                     40:                        "datefmt_set_calendar: calendar value specified is out of valid range", 0 TSRMLS_CC);
                     41:                RETURN_FALSE;
                     42:        }
                     43: 
                     44:        /* Convert timezone to UTF-16. */
                     45:        intl_convert_utf8_to_utf16(&timezone_utf16, &timezone_utf16_len, timezone_id, timezone_id_len, &INTL_DATA_ERROR_CODE(dfo));
                     46:        INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
                     47: 
                     48:        /* Get the locale for the dateformatter */
                     49:        locale = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(dfo));
                     50: 
                     51:        /* Set the calendar if passed */
                     52:        ucal_obj = ucal_open(timezone_utf16, timezone_utf16_len, locale, calendar, &INTL_DATA_ERROR_CODE(dfo) );
                     53:        udat_setCalendar( DATE_FORMAT_OBJECT(dfo), ucal_obj );
                     54:        INTL_METHOD_CHECK_STATUS(dfo, "Error setting the calendar.");
                     55: 
                     56:        if( timezone_utf16){
                     57:                efree(timezone_utf16);
                     58:        }
                     59: }
                     60: 
                     61: /* {{{ proto unicode IntlDateFormatter::getDateType( )
                     62:  * Get formatter datetype. }}} */
                     63: /* {{{ proto string datefmt_get_datetype( IntlDateFormatter $mf )
                     64:  * Get formatter datetype.
                     65:  */
                     66: PHP_FUNCTION( datefmt_get_datetype )
                     67: {
                     68:        DATE_FORMAT_METHOD_INIT_VARS;
                     69: 
                     70:        /* Parse parameters. */
                     71:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
                     72:        {
                     73:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, 
                     74:                        "datefmt_get_datetype: unable to parse input params", 0 TSRMLS_CC );
                     75:                RETURN_FALSE;
                     76:        }
                     77: 
                     78:        /* Fetch the object. */
                     79:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                     80: 
                     81:        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter datetype." );
                     82: 
                     83:        RETURN_LONG(dfo->date_type );
                     84: }
                     85: /* }}} */
                     86: 
                     87: /* {{{ proto unicode IntlDateFormatter::getTimeType( )
                     88:  * Get formatter timetype. }}} */
                     89: /* {{{ proto string datefmt_get_timetype( IntlDateFormatter $mf )
                     90:  * Get formatter timetype.
                     91:  */
                     92: PHP_FUNCTION( datefmt_get_timetype )
                     93: {
                     94:        DATE_FORMAT_METHOD_INIT_VARS;
                     95: 
                     96:        /* Parse parameters. */
                     97:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
                     98:        {
                     99:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, 
                    100:                        "datefmt_get_timetype: unable to parse input params", 0 TSRMLS_CC );
                    101:                RETURN_FALSE;
                    102:        }
                    103: 
                    104:        /* Fetch the object. */
                    105:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    106: 
                    107:        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timetype." );
                    108: 
                    109:        RETURN_LONG(dfo->time_type );
                    110: }
                    111: /* }}} */
                    112: 
                    113: 
                    114: /* {{{ proto unicode IntlDateFormatter::getCalendar( )
                    115:  * Get formatter calendar. }}} */
                    116: /* {{{ proto string datefmt_get_calendar( IntlDateFormatter $mf )
                    117:  * Get formatter calendar.
                    118:  */
                    119: PHP_FUNCTION( datefmt_get_calendar )
                    120: {
                    121:        DATE_FORMAT_METHOD_INIT_VARS;
                    122: 
                    123:        /* Parse parameters. */
                    124:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
                    125:        {
                    126:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, 
                    127:                        "datefmt_get_calendar: unable to parse input params", 0 TSRMLS_CC );
                    128:                RETURN_FALSE;
                    129:        }
                    130: 
                    131:        /* Fetch the object. */
                    132:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    133: 
                    134:        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter calendar." );
                    135: 
                    136:        RETURN_LONG(dfo->calendar);
                    137: }
                    138: /* }}} */
                    139: 
                    140: /* {{{ proto unicode IntlDateFormatter::getTimeZoneId( )
                    141:  * Get formatter timezone_id. }}} */
                    142: /* {{{ proto string datefmt_get_timezone_id( IntlDateFormatter $mf )
                    143:  * Get formatter timezone_id.
                    144:  */
                    145: PHP_FUNCTION( datefmt_get_timezone_id )
                    146: {
                    147:        DATE_FORMAT_METHOD_INIT_VARS;
                    148: 
                    149:        /* Parse parameters. */
                    150:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
                    151:        {
                    152:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, 
                    153:                        "datefmt_get_timezone_id: unable to parse input params", 0 TSRMLS_CC );
                    154:                RETURN_FALSE;
                    155:        }
                    156: 
                    157:        /* Fetch the object. */
                    158:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    159: 
                    160:        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter timezone_id." );
                    161: 
                    162:        if( dfo->timezone_id ){
                    163:                RETURN_STRING((char*)dfo->timezone_id, TRUE );
                    164:        }else{
                    165:                RETURN_NULL();
                    166:        }
                    167: }
                    168: 
                    169: /* {{{ proto boolean IntlDateFormatter::setTimeZoneId( $timezone_id)
                    170:  * Set formatter timezone_id. }}} */
                    171: /* {{{ proto boolean datefmt_set_timezone_id( IntlDateFormatter $mf,$timezone_id)
                    172:  * Set formatter timezone_id.
                    173:  */
                    174: PHP_FUNCTION( datefmt_set_timezone_id )
                    175: {
                    176:        char*           timezone_id             = NULL;
                    177:        int             timezone_id_len         = 0;
                    178: 
                    179:        DATE_FORMAT_METHOD_INIT_VARS;
                    180: 
                    181:        /* Parse parameters. */
                    182:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &object, IntlDateFormatter_ce_ptr,&timezone_id, &timezone_id_len) == FAILURE )
                    183:        {
                    184:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
                    185:                        "datefmt_set_timezone_id: unable to parse input params", 0 TSRMLS_CC );
                    186:                RETURN_FALSE;
                    187:        }
                    188: 
                    189:        /* Fetch the object. */
                    190:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    191: 
                    192:        /* set the timezone for the calendar */
                    193:        internal_set_calendar( dfo, timezone_id, timezone_id_len, dfo->calendar, return_value TSRMLS_CC );
                    194: 
                    195:        /* Set the IntlDateFormatter variable */
                    196:         if( dfo->timezone_id ){
                    197:                efree(dfo->timezone_id);
                    198:        }
                    199:        dfo->timezone_id = estrndup(timezone_id, timezone_id_len);
                    200: 
                    201:        RETURN_TRUE;
                    202: }
                    203: 
                    204: /* {{{ proto string IntlDateFormatter::getPattern( )
                    205:  * Get formatter pattern. }}} */
                    206: /* {{{ proto string datefmt_get_pattern( IntlDateFormatter $mf )
                    207:  * Get formatter pattern.
                    208:  */
                    209: PHP_FUNCTION( datefmt_get_pattern )
                    210: {
                    211:        UChar  value_buf[64];
                    212:        int    length = USIZE( value_buf );
                    213:        UChar* value  = value_buf;
                    214:        zend_bool   is_pattern_localized =FALSE;
                    215: 
                    216:        DATE_FORMAT_METHOD_INIT_VARS;
                    217: 
                    218:        /* Parse parameters. */
                    219:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, IntlDateFormatter_ce_ptr ) == FAILURE )
                    220:        {
                    221:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, 
                    222:                        "datefmt_get_pattern: unable to parse input params", 0 TSRMLS_CC );
                    223:                RETURN_FALSE;
                    224:        }
                    225: 
                    226:        /* Fetch the object. */
                    227:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    228: 
                    229:        length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo));
                    230:        if(INTL_DATA_ERROR_CODE(dfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
                    231:                ++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
                    232:                INTL_DATA_ERROR_CODE(dfo) = U_ZERO_ERROR;
                    233:                value = eumalloc(length);
                    234:                length = udat_toPattern(DATE_FORMAT_OBJECT(dfo), is_pattern_localized, value, length, &INTL_DATA_ERROR_CODE(dfo) );
                    235:                if(U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
                    236:                        efree(value);
                    237:                        value = value_buf;
                    238:                }
                    239:        }
                    240:        INTL_METHOD_CHECK_STATUS(dfo, "Error getting formatter pattern" );
                    241: 
                    242:        INTL_METHOD_RETVAL_UTF8( dfo, value, length, ( value != value_buf ) );
                    243: }
                    244: /* }}} */
                    245: 
                    246: /* {{{ proto bool IntlDateFormatter::setPattern( string $pattern )
                    247:  * Set formatter pattern. }}} */
                    248: /* {{{ proto bool datefmt_set_pattern( IntlDateFormatter $mf, string $pattern )
                    249:  * Set formatter pattern.
                    250:  */
                    251: PHP_FUNCTION( datefmt_set_pattern )
                    252: {
                    253:        char*       value = NULL;
                    254:        int         value_len = 0;
                    255:        int         slength = 0;
                    256:        UChar*      svalue  = NULL;
                    257:        zend_bool   is_pattern_localized =FALSE;
                    258: 
                    259: 
                    260:        DATE_FORMAT_METHOD_INIT_VARS;
                    261: 
                    262:        /* Parse parameters. */
                    263:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
                    264:                &object, IntlDateFormatter_ce_ptr,  &value, &value_len ) == FAILURE )
                    265:        {
                    266:                intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,  
                    267:                        "datefmt_set_pattern: unable to parse input params", 0 TSRMLS_CC);
                    268:                RETURN_FALSE;
                    269:        }
                    270: 
                    271:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    272: 
                    273:        /* Convert given pattern to UTF-16. */
                    274:        intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(dfo));
                    275:        INTL_METHOD_CHECK_STATUS(dfo, "Error converting pattern to UTF-16" );
                    276: 
                    277:        udat_applyPattern(DATE_FORMAT_OBJECT(dfo), (UBool)is_pattern_localized, svalue, slength);
                    278: 
                    279:        if (svalue) {
                    280:                efree(svalue);
                    281:        }
                    282:        INTL_METHOD_CHECK_STATUS(dfo, "Error setting symbol value");
                    283: 
                    284:        RETURN_TRUE;
                    285: }
                    286: /* }}} */
                    287: 
                    288: /* {{{ proto string IntlDateFormatter::getLocale()
                    289:  * Get formatter locale. }}} */
                    290: /* {{{ proto string datefmt_get_locale(IntlDateFormatter $mf)
                    291:  * Get formatter locale.
                    292:  */
                    293: PHP_FUNCTION( datefmt_get_locale )
                    294: {
                    295:        char *loc;
                    296:        long  loc_type =ULOC_ACTUAL_LOCALE;
                    297: 
                    298:        DATE_FORMAT_METHOD_INIT_VARS;
                    299: 
                    300:        /* Parse parameters. */
                    301:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l",
                    302:                &object, IntlDateFormatter_ce_ptr,&loc_type) == FAILURE )
                    303:        {
                    304:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
                    305:                        "datefmt_get_locale: unable to parse input params", 0 TSRMLS_CC );
                    306: 
                    307:                RETURN_FALSE;
                    308:        }
                    309: 
                    310:        /* Fetch the object. */
                    311:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    312: 
                    313:        loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), loc_type,&INTL_DATA_ERROR_CODE(dfo));
                    314:        INTL_METHOD_CHECK_STATUS(dfo, "Error getting locale");
                    315:        RETURN_STRING(loc, 1);
                    316: }
                    317: /* }}} */
                    318: 
                    319: /* {{{ proto string IntlDateFormatter::isLenient()
                    320:  * Get formatter isLenient. }}} */
                    321: /* {{{ proto string datefmt_isLenient(IntlDateFormatter $mf)
                    322:  * Get formatter locale.
                    323:  */
                    324: PHP_FUNCTION( datefmt_is_lenient )
                    325: {
                    326:        
                    327:        DATE_FORMAT_METHOD_INIT_VARS;
                    328: 
                    329:        /* Parse parameters. */
                    330:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
                    331:                &object, IntlDateFormatter_ce_ptr ) == FAILURE )
                    332:        {
                    333:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
                    334:                        "datefmt_is_lenient: unable to parse input params", 0 TSRMLS_CC );
                    335: 
                    336:                RETURN_FALSE;
                    337:        }
                    338: 
                    339:        /* Fetch the object. */
                    340:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    341: 
                    342:        RETVAL_BOOL(udat_isLenient(DATE_FORMAT_OBJECT(dfo)));
                    343: }
                    344: /* }}} */
                    345: 
                    346: /* {{{ proto string IntlDateFormatter::setLenient()
                    347:  * Set formatter lenient. }}} */
                    348: /* {{{ proto string datefmt_setLenient(IntlDateFormatter $mf)
                    349:  * Set formatter lenient.
                    350:  */
                    351: PHP_FUNCTION( datefmt_set_lenient )
                    352: {
                    353:        zend_bool isLenient  = FALSE;
                    354: 
                    355:        DATE_FORMAT_METHOD_INIT_VARS;
                    356: 
                    357:        /* Parse parameters. */
                    358:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ob",
                    359:        &object, IntlDateFormatter_ce_ptr,&isLenient ) == FAILURE )
                    360:        {
                    361:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
                    362:                        "datefmt_set_lenient: unable to parse input params", 0 TSRMLS_CC );
                    363:                RETURN_FALSE;
                    364:        }
                    365: 
                    366:        /* Fetch the object. */
                    367:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    368: 
                    369:        udat_setLenient(DATE_FORMAT_OBJECT(dfo), (UBool)isLenient );
                    370: }
                    371: /* }}} */
                    372: 
                    373: /* {{{ proto bool IntlDateFormatter::setPattern( int $calendar )
                    374:  * Set formatter calendar. }}} */
                    375: /* {{{ proto bool datefmt_set_calendar( IntlDateFormatter $mf, int $calendar )
                    376:  * Set formatter calendar.
                    377:  */
                    378: PHP_FUNCTION( datefmt_set_calendar )
                    379: {
                    380:        long    calendar = 0;
                    381: 
                    382:        DATE_FORMAT_METHOD_INIT_VARS;
                    383: 
                    384:        /* Parse parameters. */
                    385:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
                    386:                &object, IntlDateFormatter_ce_ptr, &calendar ) == FAILURE ) {
                    387:                intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
                    388:                        "datefmt_set_calendar: unable to parse input params", 0 TSRMLS_CC);
                    389:                RETURN_FALSE;
                    390:        }
                    391: 
                    392:        /* check for the validity  of value of calendar passed */
                    393:        intl_error_reset( NULL TSRMLS_CC );
                    394:        if (calendar > 1) {
                    395:                intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
                    396:                        "datefmt_set_calendar: calendar value specified is out of valid range", 0 TSRMLS_CC);
                    397:                RETURN_FALSE;
                    398:        }
                    399: 
                    400:        DATE_FORMAT_METHOD_FETCH_OBJECT;
                    401: 
                    402:        internal_set_calendar( dfo, dfo->timezone_id, strlen(dfo->timezone_id), calendar, return_value TSRMLS_CC );
                    403: 
                    404:        /* Set the calendar  value in the IntlDateFormatter object */
                    405:        dfo->calendar = calendar;
                    406: 
                    407:        RETURN_TRUE;
                    408: }
                    409: /* }}} */
                    410: 
                    411: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>