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/common - common_date.cpp (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 108 0.0 %
Date: 2014-04-06 Functions: 0 3 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: Gustavo Lopes <cataphract@php.net>                          |
      14             :    +----------------------------------------------------------------------+
      15             : */
      16             : 
      17             : #include "../intl_cppshims.h"
      18             : 
      19             : #include <unicode/calendar.h>
      20             : 
      21             : extern "C" {
      22             : #include "../php_intl.h"
      23             : #define USE_CALENDAR_POINTER 1
      24             : #include "../calendar/calendar_class.h"
      25             : #include <ext/date/php_date.h>
      26             : }
      27             : 
      28             : #ifndef INFINITY
      29             : #define INFINITY (DBL_MAX+DBL_MAX)
      30             : #endif
      31             : 
      32             : #ifndef NAN
      33             : #define NAN (INFINITY-INFINITY)
      34             : #endif
      35             : 
      36             : /* {{{ timezone_convert_datetimezone
      37             :  *      The timezone in DateTime and DateTimeZone is not unified. */
      38           0 : U_CFUNC TimeZone *timezone_convert_datetimezone(int type,
      39             :                                                                                                 void *object,
      40             :                                                                                                 int is_datetime,
      41             :                                                                                                 intl_error *outside_error,
      42             :                                                                                                 const char *func TSRMLS_DC)
      43             : {
      44           0 :         char            *id = NULL,
      45           0 :                                 offset_id[] = "GMT+00:00";
      46           0 :         int                     id_len = 0;
      47             :         char            *message;
      48             :         TimeZone        *timeZone;
      49             : 
      50           0 :         switch (type) {
      51             :                 case TIMELIB_ZONETYPE_ID:
      52             :                         id = is_datetime
      53             :                                 ? ((php_date_obj*)object)->time->tz_info->name
      54           0 :                                 : ((php_timezone_obj*)object)->tzi.tz->name;
      55           0 :                         id_len = strlen(id);
      56           0 :                         break;
      57             :                 case TIMELIB_ZONETYPE_OFFSET: {
      58             :                         int offset_mins = is_datetime
      59             :                                 ? -((php_date_obj*)object)->time->z
      60           0 :                                 : -(int)((php_timezone_obj*)object)->tzi.utc_offset,
      61           0 :                                 hours = offset_mins / 60,
      62           0 :                                 minutes = offset_mins - hours * 60;
      63           0 :                         minutes *= minutes > 0 ? 1 : -1;
      64             : 
      65           0 :                         if (offset_mins <= -24 * 60 || offset_mins >= 24 * 60) {
      66             :                                 spprintf(&message, 0, "%s: object has an time zone offset "
      67           0 :                                         "that's too large", func);
      68             :                                 intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR,
      69           0 :                                         message, 1 TSRMLS_CC);
      70           0 :                                 efree(message);
      71           0 :                                 return NULL;
      72             :                         }
      73             : 
      74           0 :                         id = offset_id;
      75             :                         id_len = slprintf(id, sizeof(offset_id), "GMT%+03d:%02d",
      76           0 :                                 hours, minutes);
      77           0 :                         break;
      78             :                 }
      79             :                 case TIMELIB_ZONETYPE_ABBR:
      80             :                         id = is_datetime
      81             :                                 ? ((php_date_obj*)object)->time->tz_abbr
      82           0 :                                 : ((php_timezone_obj*)object)->tzi.z.abbr;
      83           0 :                         id_len = strlen(id);
      84             :                         break;
      85             :         }
      86             : 
      87           0 :         UnicodeString s = UnicodeString(id, id_len, US_INV);
      88           0 :         timeZone = TimeZone::createTimeZone(s);
      89             : #if U_ICU_VERSION_MAJOR_NUM >= 49
      90             :         if (*timeZone == TimeZone::getUnknown()) {
      91             : #else
      92           0 :         UnicodeString resultingId;
      93           0 :         timeZone->getID(resultingId);
      94           0 :         if (resultingId == UnicodeString("Etc/Unknown", -1, US_INV)
      95             :                         || resultingId == UnicodeString("GMT", -1, US_INV)) {
      96             : #endif
      97             :                 spprintf(&message, 0, "%s: time zone id '%s' "
      98           0 :                         "extracted from ext/date DateTimeZone not recognized", func, id);
      99             :                 intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR,
     100           0 :                         message, 1 TSRMLS_CC);
     101           0 :                 efree(message);
     102           0 :                 delete timeZone;
     103           0 :                 return NULL;
     104             :         }
     105           0 :         return timeZone;
     106             : }
     107             : /* }}} */
     108             : 
     109           0 : U_CFUNC int intl_datetime_decompose(zval *z, double *millis, TimeZone **tz,
     110             :                 intl_error *err, const char *func TSRMLS_DC)
     111             : {
     112             :         zval    retval;
     113             :         zval    *zfuncname;
     114             :         char    *message;
     115             : 
     116           0 :         if (err && U_FAILURE(err->code)) {
     117           0 :                 return FAILURE;
     118             :         }
     119             : 
     120           0 :         if (millis) {
     121           0 :                 *millis = NAN;
     122             :         }
     123           0 :         if (tz) {
     124           0 :                 *tz = NULL;
     125             :         }
     126             : 
     127           0 :         if (millis) {
     128           0 :                 INIT_ZVAL(retval);
     129           0 :                 MAKE_STD_ZVAL(zfuncname);
     130           0 :                 ZVAL_STRING(zfuncname, "getTimestamp", 1);
     131           0 :                 if (call_user_function(NULL, &(z), zfuncname, &retval, 0, NULL TSRMLS_CC)
     132             :                                 != SUCCESS || Z_TYPE(retval) != IS_LONG) {
     133             :                         spprintf(&message, 0, "%s: error calling ::getTimeStamp() on the "
     134           0 :                                         "object", func);
     135             :                         intl_errors_set(err, U_INTERNAL_PROGRAM_ERROR,
     136           0 :                                 message, 1 TSRMLS_CC);
     137           0 :                         efree(message);
     138           0 :                         zval_ptr_dtor(&zfuncname);
     139           0 :                         return FAILURE;
     140             :                 }
     141             : 
     142           0 :                 *millis = U_MILLIS_PER_SECOND * (double)Z_LVAL(retval);
     143           0 :                 zval_ptr_dtor(&zfuncname);
     144             :         }
     145             : 
     146           0 :         if (tz) {
     147             :                 php_date_obj *datetime;
     148           0 :                 datetime = (php_date_obj*)zend_object_store_get_object(z TSRMLS_CC);
     149           0 :                 if (!datetime->time) {
     150             :                         spprintf(&message, 0, "%s: the DateTime object is not properly "
     151           0 :                                         "initialized", func);
     152             :                         intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
     153           0 :                                 message, 1 TSRMLS_CC);
     154           0 :                         efree(message);
     155           0 :                         return FAILURE;
     156             :                 }
     157           0 :                 if (!datetime->time->is_localtime) {
     158           0 :                         *tz = TimeZone::getGMT()->clone();
     159             :                 } else {
     160             :                         *tz = timezone_convert_datetimezone(datetime->time->zone_type,
     161           0 :                                 datetime, 1, NULL, func TSRMLS_CC);
     162           0 :                         if (*tz == NULL) {
     163             :                                 spprintf(&message, 0, "%s: could not convert DateTime's "
     164           0 :                                                 "time zone", func);
     165             :                                 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
     166           0 :                                         message, 1 TSRMLS_CC);
     167           0 :                                 efree(message);
     168           0 :                                 return FAILURE;
     169             :                         }
     170             :                 }
     171             :         }
     172             : 
     173           0 :         return SUCCESS;
     174             : }
     175             : 
     176           0 : U_CFUNC double intl_zval_to_millis(zval *z, intl_error *err, const char *func TSRMLS_DC)
     177             : {
     178           0 :         double  rv = NAN;
     179             :         long    lv;
     180             :         int             type;
     181             :         char    *message;
     182             : 
     183           0 :         if (err && U_FAILURE(err->code)) {
     184           0 :                 return NAN;
     185             :         }
     186             : 
     187           0 :         switch (Z_TYPE_P(z)) {
     188             :         case IS_STRING:
     189           0 :                 type = is_numeric_string(Z_STRVAL_P(z), Z_STRLEN_P(z), &lv, &rv, 0);
     190           0 :                 if (type == IS_DOUBLE) {
     191           0 :                         rv *= U_MILLIS_PER_SECOND;
     192           0 :                 } else if (type == IS_LONG) {
     193           0 :                         rv = U_MILLIS_PER_SECOND * (double)lv;
     194             :                 } else {
     195             :                         spprintf(&message, 0, "%s: string '%s' is not numeric, "
     196             :                                         "which would be required for it to be a valid date", func,
     197           0 :                                         Z_STRVAL_P(z));
     198             :                         intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
     199           0 :                                 message, 1 TSRMLS_CC);
     200           0 :                         efree(message);
     201             :                 }
     202           0 :                 break;
     203             :         case IS_LONG:
     204           0 :                 rv = U_MILLIS_PER_SECOND * (double)Z_LVAL_P(z);
     205           0 :                 break;
     206             :         case IS_DOUBLE:
     207           0 :                 rv = U_MILLIS_PER_SECOND * Z_DVAL_P(z);
     208           0 :                 break;
     209             :         case IS_OBJECT:
     210           0 :                 if (instanceof_function(Z_OBJCE_P(z), php_date_get_date_ce() TSRMLS_CC)) {
     211           0 :                         intl_datetime_decompose(z, &rv, NULL, err, func TSRMLS_CC);
     212           0 :                 } else if (instanceof_function(Z_OBJCE_P(z), Calendar_ce_ptr TSRMLS_CC)) {
     213             :                         Calendar_object *co = (Calendar_object *)
     214           0 :                                 zend_object_store_get_object(z TSRMLS_CC );
     215           0 :                         if (co->ucal == NULL) {
     216             :                                 spprintf(&message, 0, "%s: IntlCalendar object is not properly "
     217           0 :                                                 "constructed", func);
     218             :                                 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
     219           0 :                                         message, 1 TSRMLS_CC);
     220           0 :                                 efree(message);
     221             :                         } else {
     222           0 :                                 UErrorCode status = UErrorCode();
     223           0 :                                 rv = (double)co->ucal->getTime(status);
     224           0 :                                 if (U_FAILURE(status)) {
     225             :                                         spprintf(&message, 0, "%s: call to internal "
     226           0 :                                                         "Calendar::getTime() has failed", func);
     227           0 :                                         intl_errors_set(err, status, message, 1 TSRMLS_CC);
     228           0 :                                         efree(message);
     229             :                                 }
     230             :                         }
     231             :                 } else {
     232             :                         /* TODO: try with cast(), get() to obtain a number */
     233             :                         spprintf(&message, 0, "%s: invalid object type for date/time "
     234           0 :                                         "(only IntlCalendar and DateTime permitted)", func);
     235             :                         intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
     236           0 :                                 message, 1 TSRMLS_CC);
     237           0 :                         efree(message);
     238             :                 }
     239           0 :                 break;
     240             :         default:
     241           0 :                 spprintf(&message, 0, "%s: invalid PHP type for date", func);
     242             :                 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
     243           0 :                         message, 1 TSRMLS_CC);
     244           0 :                 efree(message);
     245             :                 break;
     246             :         }
     247             : 
     248           0 :         return rv;
     249             : }
     250             : 

Generated by: LCOV version 1.10

Generated at Sun, 06 Apr 2014 17:31:02 +0000 (9 days ago)

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