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_parse.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 85 0.0 %
Date: 2014-04-16 Functions: 0 5 0.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 <math.h>
      23             : 
      24             : #include "php_intl.h"
      25             : #include "intl_convert.h"
      26             : #include "dateformat.h"
      27             : #include "dateformat_class.h"
      28             : #include "dateformat_parse.h"
      29             : #include "dateformat_data.h"
      30             : 
      31             : /* {{{ 
      32             :  * Internal function which calls the udat_parse
      33             :  * param int store_error acts like a boolean 
      34             :  *      if set to 1 - store any error encountered  in the parameter parse_error  
      35             :  *      if set to 0 - no need to store any error encountered  in the parameter parse_error  
      36             : */
      37           0 : static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value TSRMLS_DC)
      38             : {
      39           0 :         double  result =  0;
      40           0 :         UDate   timestamp   =0;
      41           0 :         UChar*  text_utf16  = NULL;
      42           0 :         int32_t text_utf16_len = 0;
      43             : 
      44             :         /* Convert timezone to UTF-16. */
      45           0 :         intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
      46           0 :         INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
      47             : 
      48           0 :         timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
      49           0 :         if( text_utf16 ){
      50           0 :                 efree(text_utf16);
      51             :         }
      52             : 
      53           0 :         INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" );
      54             :         
      55             :         /* Since return is in  sec. */
      56           0 :         result = (double)timestamp / U_MILLIS_PER_SECOND;
      57           0 :         if(result > LONG_MAX || result < -LONG_MAX) {
      58           0 :                 ZVAL_DOUBLE(return_value, result<0?ceil(result):floor(result));
      59             :         } else {
      60           0 :                 ZVAL_LONG(return_value, (long)result);
      61             :         }
      62             : }
      63             : /* }}} */
      64             : 
      65           0 : static void add_to_localtime_arr( IntlDateFormatter_object *dfo, zval* return_value, const UCalendar *parsed_calendar, long calendar_field, char* key_name TSRMLS_DC)
      66             : {
      67           0 :         long calendar_field_val = ucal_get( parsed_calendar, calendar_field, &INTL_DATA_ERROR_CODE(dfo));   
      68           0 :         INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : could not get a field from calendar" );
      69             : 
      70           0 :         if( strcmp(key_name, CALENDAR_YEAR )==0 ){
      71             :                 /* since tm_year is years from 1900 */
      72           0 :                 add_assoc_long( return_value, key_name,( calendar_field_val-1900) ); 
      73           0 :         }else if( strcmp(key_name, CALENDAR_WDAY )==0 ){
      74             :                 /* since tm_wday starts from 0 whereas ICU WDAY start from 1 */
      75           0 :                 add_assoc_long( return_value, key_name,( calendar_field_val-1) ); 
      76             :         }else{
      77           0 :                 add_assoc_long( return_value, key_name, calendar_field_val ); 
      78             :         }
      79             : }
      80             : 
      81             : /* {{{
      82             :  * Internal function which calls the udat_parseCalendar
      83             : */
      84           0 : static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value TSRMLS_DC)
      85             : {
      86           0 :         UCalendar      *parsed_calendar = NULL;
      87           0 :         UChar*          text_utf16  = NULL;
      88           0 :         int32_t         text_utf16_len = 0;
      89           0 :         long            isInDST = 0;
      90             : 
      91             :         /* Convert timezone to UTF-16. */
      92           0 :         intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
      93           0 :         INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
      94             : 
      95           0 :         parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
      96           0 :         udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
      97             :         
      98           0 :         if (text_utf16) {
      99           0 :                 efree(text_utf16);
     100             :         }
     101             : 
     102           0 :         INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" );
     103             : 
     104             : 
     105           0 :         array_init( return_value );
     106             :         /* Add  entries from various fields of the obtained parsed_calendar */
     107           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_SECOND, CALENDAR_SEC TSRMLS_CC);
     108           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MINUTE, CALENDAR_MIN TSRMLS_CC);
     109           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_HOUR_OF_DAY, CALENDAR_HOUR TSRMLS_CC);
     110           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_YEAR, CALENDAR_YEAR TSRMLS_CC); 
     111           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_MONTH, CALENDAR_MDAY TSRMLS_CC);
     112           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_WEEK, CALENDAR_WDAY TSRMLS_CC);
     113           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_YEAR, CALENDAR_YDAY TSRMLS_CC);
     114           0 :         add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MONTH, CALENDAR_MON TSRMLS_CC);
     115             : 
     116             :         /* Is in DST? */
     117           0 :         isInDST = ucal_inDaylightTime(parsed_calendar   , &INTL_DATA_ERROR_CODE(dfo));
     118           0 :         INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : while checking if currently in DST." );
     119           0 :         add_assoc_long( return_value, CALENDAR_ISDST,(isInDST==1?1:0)); 
     120             : }
     121             : /* }}} */
     122             : 
     123             : 
     124             : /* {{{ proto integer IntlDateFormatter::parse( string $text_to_parse  [, int $parse_pos] )
     125             :  * Parse the string $value starting at parse_pos to a Unix timestamp -int }}}*/
     126             : /* {{{ proto integer datefmt_parse( IntlDateFormatter $fmt, string $text_to_parse [, int $parse_pos] )
     127             :  * Parse the string $value starting at parse_pos to a Unix timestamp -int }}}*/
     128           0 : PHP_FUNCTION(datefmt_parse)
     129             : {
     130           0 :         char*           text_to_parse = NULL;
     131           0 :         int32_t         text_len =0;
     132           0 :         zval*           z_parse_pos = NULL;
     133           0 :         int32_t                 parse_pos = -1;
     134             : 
     135           0 :         DATE_FORMAT_METHOD_INIT_VARS;
     136             : 
     137             :         /* Parse parameters. */
     138           0 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|z!",
     139             :                 &object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
     140           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_parse: unable to parse input params", 0 TSRMLS_CC );
     141           0 :                 RETURN_FALSE;
     142             :         }
     143             : 
     144             :         /* Fetch the object. */
     145           0 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     146             : 
     147           0 :         if (z_parse_pos) {
     148           0 :                 convert_to_long(z_parse_pos);
     149           0 :                 parse_pos = (int32_t)Z_LVAL_P(z_parse_pos);
     150           0 :                 if(parse_pos > text_len) {
     151           0 :                         RETURN_FALSE;
     152             :                 }
     153             :         }
     154           0 :         internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
     155           0 :         if(z_parse_pos) {
     156           0 :                 zval_dtor(z_parse_pos);
     157           0 :                 ZVAL_LONG(z_parse_pos, parse_pos);
     158             :         }
     159             : }
     160             : /* }}} */
     161             : 
     162             : /* {{{ proto integer IntlDateFormatter::localtime( string $text_to_parse[, int $parse_pos] )
     163             :  * Parse the string $value to a localtime array  }}}*/
     164             : /* {{{ proto integer datefmt_localtime( IntlDateFormatter $fmt, string $text_to_parse[, int $parse_pos ])
     165             :  * Parse the string $value to a localtime array  }}}*/
     166           0 : PHP_FUNCTION(datefmt_localtime)
     167             : {
     168           0 :         char*           text_to_parse = NULL;
     169           0 :         int32_t         text_len =0;
     170           0 :         zval*           z_parse_pos = NULL;
     171           0 :         int32_t                 parse_pos = -1;
     172             : 
     173           0 :         DATE_FORMAT_METHOD_INIT_VARS;
     174             : 
     175             :         /* Parse parameters. */
     176           0 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|z!",
     177             :                 &object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
     178           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_parse_to_localtime: unable to parse input params", 0 TSRMLS_CC );
     179           0 :                 RETURN_FALSE;
     180             :         }
     181             : 
     182             :     /* Fetch the object. */
     183           0 :         DATE_FORMAT_METHOD_FETCH_OBJECT;
     184             : 
     185           0 :         if(z_parse_pos) {
     186           0 :                 convert_to_long(z_parse_pos);
     187           0 :                 parse_pos = (int32_t)Z_LVAL_P(z_parse_pos);
     188           0 :                 if(parse_pos > text_len) {
     189           0 :                         RETURN_FALSE;
     190             :                 }
     191             :         }
     192           0 :         internal_parse_to_localtime( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value TSRMLS_CC);
     193           0 :         if(z_parse_pos) {
     194           0 :                 zval_dtor(z_parse_pos);
     195           0 :                 ZVAL_LONG(z_parse_pos, parse_pos);
     196             :         }
     197             : }
     198             : /* }}} */
     199             : 

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:49 +0000 (43 hours ago)

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