PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/dateformat - dateformat_format.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 74 85 87.1 %
Date: 2014-11-15 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include <unicode/ustring.h>
      22             : #include <unicode/ucal.h>
      23             : 
      24             : #include "php_intl.h"
      25             : #include "intl_convert.h"
      26             : #include "dateformat.h"
      27             : #include "dateformat_class.h"
      28             : #include "dateformat_format.h"
      29             : #include "dateformat_data.h"
      30             : #include "ext/date/php_date.h"
      31             : 
      32             : /* {{{ 
      33             :  * Internal function which calls the udat_format
      34             : */
      35         228 : static void internal_format(IntlDateFormatter_object *dfo, UDate timestamp, zval *return_value TSRMLS_DC)
      36             : {
      37         228 :         UChar*  formatted =  NULL;
      38         228 :         int32_t resultlengthneeded =0 ;
      39             :         
      40         228 :         resultlengthneeded=udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, NULL, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
      41         228 :         if(INTL_DATA_ERROR_CODE(dfo)==U_BUFFER_OVERFLOW_ERROR)
      42             :         {
      43         228 :                 INTL_DATA_ERROR_CODE(dfo)=U_ZERO_ERROR;
      44         228 :                 formatted=(UChar*)emalloc(sizeof(UChar) * resultlengthneeded); 
      45         228 :                 udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, formatted, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
      46             :         }
      47             : 
      48         228 :         if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(dfo) ) ) {
      49           0 :                         efree(formatted);
      50             :         }
      51             : 
      52         228 :         INTL_METHOD_CHECK_STATUS( dfo, "Date formatting failed" );
      53         228 :         INTL_METHOD_RETVAL_UTF8( dfo, formatted, resultlengthneeded, 1 );
      54             : 
      55             : }
      56             : /* }}} */
      57             : 
      58             : 
      59             : /* {{{ 
      60             :  * Internal function which fetches an element from the passed array for the key_name passed 
      61             : */
      62         432 : static double internal_get_arr_ele(IntlDateFormatter_object *dfo, HashTable* hash_arr, char* key_name TSRMLS_DC)
      63             : {
      64         432 :         zval**  ele_value       = NULL;
      65         432 :         UDate result = -1;
      66             : 
      67         432 :         if( zend_hash_find( hash_arr, key_name, strlen(key_name) + 1, (void **)&ele_value ) == SUCCESS ){
      68         288 :                 if( Z_TYPE_PP(ele_value)!= IS_LONG ){
      69           0 :                         intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
      70             :                                 "datefmt_format: parameter array does not contain a long element.", 0 TSRMLS_CC );
      71             :                 }else{
      72         288 :                         result =  Z_LVAL_PP(ele_value);
      73             :                 }
      74             :         }
      75             :         /* printf("\n Inside internal_get_arr_ele key_name= %s, result = %g \n", key_name, result); */
      76         432 :         return result;
      77             : }
      78             : /* }}} */
      79             : 
      80             : /* {{{ 
      81             :  * Internal function which sets UCalendar  from the passed array and retrieves timestamp
      82             : */
      83          48 : static UDate internal_get_timestamp(IntlDateFormatter_object *dfo, HashTable* hash_arr  TSRMLS_DC)
      84             : {
      85          48 :         long year =0;
      86          48 :         long month =0;
      87          48 :         long hour =0;
      88          48 :         long minute =0;
      89          48 :         long second =0;
      90          48 :         long wday =0;
      91          48 :         long yday =0;
      92          48 :         long mday =0;
      93          48 :         UBool isInDST = FALSE;
      94             :         UCalendar *pcal;
      95             : 
      96             :         /* Fetch  values from the incoming array */
      97          48 :         year = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YEAR TSRMLS_CC) + 1900; /* tm_year is years since 1900 */
      98             :         /* Month in ICU and PHP starts from January =0 */
      99          48 :         month = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MON TSRMLS_CC);
     100          48 :         hour = internal_get_arr_ele( dfo, hash_arr, CALENDAR_HOUR TSRMLS_CC);
     101          48 :         minute = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MIN TSRMLS_CC);
     102          48 :         second = internal_get_arr_ele( dfo, hash_arr, CALENDAR_SEC TSRMLS_CC);
     103          48 :         wday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_WDAY TSRMLS_CC);
     104          48 :         yday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YDAY TSRMLS_CC);
     105          48 :         isInDST = internal_get_arr_ele( dfo, hash_arr, CALENDAR_ISDST TSRMLS_CC);
     106             :         /* For the ucal_setDateTime() function, this is the 'date'  value */
     107          48 :         mday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MDAY TSRMLS_CC);
     108             : 
     109          48 :         pcal = udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
     110             :         /* set the incoming values for the calendar */
     111          48 :         ucal_setDateTime( pcal, year, month, mday, hour, minute, second, &INTL_DATA_ERROR_CODE(dfo));
     112          48 :         if( INTL_DATA_ERROR_CODE(dfo) != U_ZERO_ERROR){
     113           0 :                 return 0;
     114             :         }
     115             :         
     116             :         /* Fetch the timestamp from the UCalendar */
     117          48 :         return ucal_getMillis(pcal, &INTL_DATA_ERROR_CODE(dfo) );
     118             : }
     119             : 
     120             : 
     121             : /* {{{ proto string IntlDateFormatter::format( [mixed]int $args or array $args )
     122             :  * Format the time value as a string. }}}*/
     123             : /* {{{ proto string datefmt_format( [mixed]int $args or array $args )
     124             :  * Format the time value as a string. }}}*/
     125         238 : PHP_FUNCTION(datefmt_format) 
     126             : {
     127         238 :         UDate           timestamp =0;
     128         238 :         UDate           p_timestamp =0;
     129         238 :         HashTable*      hash_arr        = NULL;
     130         238 :         zval*           zarg    = NULL;
     131             : 
     132         238 :         DATE_FORMAT_METHOD_INIT_VARS;
     133             : 
     134             :         /* Parse parameters. */
     135         238 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz", &object, IntlDateFormatter_ce_ptr,&zarg ) == FAILURE )
     136             :         {
     137           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: unable to parse input params", 0 TSRMLS_CC );
     138           0 :                 RETURN_FALSE;
     139             :         }
     140             : 
     141             :         /* Fetch the object. */
     142         238 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     143             : 
     144         238 :         switch(Z_TYPE_P(zarg) ){
     145             :                 case IS_LONG:
     146         140 :                         p_timestamp = Z_LVAL_P(zarg) ;
     147         140 :                         timestamp = p_timestamp * 1000;
     148         140 :                         break;
     149             :                 case IS_DOUBLE:
     150             :                         /* timestamp*1000 since ICU expects it in milliseconds */
     151          20 :                         p_timestamp = Z_DVAL_P(zarg) ;
     152          20 :                         timestamp = p_timestamp * 1000;
     153          20 :                         break;
     154             :                 case IS_ARRAY:
     155          48 :                         hash_arr = Z_ARRVAL_P(zarg);
     156          48 :                         if( !hash_arr || zend_hash_num_elements( hash_arr ) == 0 )
     157           0 :                                 RETURN_FALSE;
     158             : 
     159          48 :                         timestamp = internal_get_timestamp(dfo, hash_arr TSRMLS_CC);
     160          48 :                         INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed" )
     161          48 :                         break;
     162             :                 case IS_OBJECT: {
     163          30 :                         zend_class_entry *date_ce = php_date_get_date_ce();
     164             :                         zval retval;
     165             :                         zval *zfuncname;
     166          30 :                         if(!instanceof_function(Z_OBJCE_P(zarg), date_ce TSRMLS_CC)) {
     167          10 :                                 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: object must be an instance of DateTime", 0 TSRMLS_CC );
     168          10 :                                 RETURN_FALSE;
     169             :                         }
     170          20 :                         INIT_ZVAL(retval);
     171          20 :                         MAKE_STD_ZVAL(zfuncname);
     172          20 :                         ZVAL_STRING(zfuncname, "getTimestamp", 1);
     173          20 :                         if(call_user_function(NULL, &zarg, zfuncname, &retval, 0, NULL TSRMLS_CC) != SUCCESS || Z_TYPE(retval) != IS_LONG) {
     174           0 :                                 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: cannot get timestamp", 0 TSRMLS_CC );
     175           0 :                                 zval_ptr_dtor(&zfuncname);
     176           0 :                                 RETURN_FALSE;
     177             :                         }
     178          20 :                         zval_ptr_dtor(&zfuncname);
     179          20 :                         p_timestamp = Z_LVAL(retval);
     180          20 :                         timestamp = p_timestamp*1000;
     181             :                 }
     182          20 :                         break;
     183             :                 default:
     184           0 :                         intl_errors_set( INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR,
     185             :                                 "datefmt_format: takes either an array or an integer timestamp value or a DateTime object", 0 TSRMLS_CC );
     186           0 :                         RETURN_FALSE;
     187             :         }
     188             : 
     189         228 :         internal_format( dfo, timestamp, return_value TSRMLS_CC);
     190             :         
     191             : }
     192             : 
     193             : /* }}} */
     194             : 

Generated by: LCOV version 1.10

Generated at Sat, 15 Nov 2014 06:19:32 +0000 (9 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.