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/date/lib - timelib.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 68 143 47.6 %
Date: 2014-07-23 Functions: 13 18 72.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "timelib.h"
      22             : #include <ctype.h>
      23             : #include <math.h>
      24             : 
      25             : #define TIMELIB_TIME_FREE(m)    \
      26             :         if (m) {                \
      27             :                 free(m);        \
      28             :                 m = NULL;       \
      29             :         }                       \
      30             : 
      31             : #define TIMELIB_LLABS(y) (y < 0 ? (y * -1) : y)
      32             : 
      33      550081 : timelib_time* timelib_time_ctor(void)
      34             : {
      35             :         timelib_time *t;
      36      550081 :         t = calloc(1, sizeof(timelib_time));
      37             : 
      38      550081 :         return t;
      39             : }
      40             : 
      41      349098 : timelib_rel_time* timelib_rel_time_ctor(void)
      42             : {
      43             :         timelib_rel_time *t;
      44      349098 :         t = calloc(1, sizeof(timelib_rel_time));
      45             : 
      46      349098 :         return t;
      47             : }
      48             : 
      49          21 : timelib_time* timelib_time_clone(timelib_time *orig)
      50             : {
      51          21 :         timelib_time *tmp = timelib_time_ctor();
      52          21 :         memcpy(tmp, orig, sizeof(timelib_time));
      53          21 :         if (orig->tz_abbr) {
      54          19 :                 tmp->tz_abbr = strdup(orig->tz_abbr);
      55             :         }
      56          21 :         if (orig->tz_info) {
      57          19 :                 tmp->tz_info = orig->tz_info;
      58             :         }
      59          21 :         return tmp;
      60             : }
      61             : 
      62          20 : timelib_rel_time* timelib_rel_time_clone(timelib_rel_time *rel)
      63             : {
      64          20 :         timelib_rel_time *tmp = timelib_rel_time_ctor();
      65          20 :         memcpy(tmp, rel, sizeof(timelib_rel_time));
      66          20 :         return tmp;
      67             : }
      68             : 
      69       14708 : void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
      70             : {
      71             :         unsigned int i;
      72             :         
      73       14708 :         TIMELIB_TIME_FREE(tm->tz_abbr);
      74       14708 :         tm->tz_abbr = strdup(tz_abbr);
      75       59358 :         for (i = 0; i < strlen(tz_abbr); i++) {
      76       44650 :                 tm->tz_abbr[i] = toupper(tz_abbr[i]);
      77             :         }
      78       14708 : }
      79             : 
      80      550071 : void timelib_time_dtor(timelib_time* t)
      81             : {
      82      550071 :         TIMELIB_TIME_FREE(t->tz_abbr);
      83      550071 :         TIMELIB_TIME_FREE(t);
      84      550071 : }
      85             : 
      86      349106 : void timelib_rel_time_dtor(timelib_rel_time* t)
      87             : {
      88      349106 :         TIMELIB_TIME_FREE(t);
      89      349106 : }
      90             : 
      91     2317652 : timelib_time_offset* timelib_time_offset_ctor(void)
      92             : {
      93             :         timelib_time_offset *t;
      94     2317652 :         t = calloc(1, sizeof(timelib_time_offset));
      95             : 
      96     2317652 :         return t;
      97             : }
      98             : 
      99     2317652 : void timelib_time_offset_dtor(timelib_time_offset* t)
     100             : {
     101     2317652 :         TIMELIB_TIME_FREE(t->abbr);
     102     2317652 :         TIMELIB_TIME_FREE(t);
     103     2317652 : }
     104             : 
     105        1596 : timelib_tzinfo* timelib_tzinfo_ctor(char *name)
     106             : {
     107             :         timelib_tzinfo *t;
     108        1596 :         t = calloc(1, sizeof(timelib_tzinfo));
     109        1596 :         t->name = strdup(name);
     110             : 
     111        1596 :         return t;
     112             : }
     113             : 
     114           0 : timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
     115             : {
     116           0 :         timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
     117           0 :         tmp->ttisgmtcnt = tz->ttisgmtcnt;
     118           0 :         tmp->ttisstdcnt = tz->ttisstdcnt;
     119           0 :         tmp->leapcnt = tz->leapcnt;
     120           0 :         tmp->timecnt = tz->timecnt;
     121           0 :         tmp->typecnt = tz->typecnt;
     122           0 :         tmp->charcnt = tz->charcnt;
     123             :         
     124           0 :         tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t));
     125           0 :         tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
     126           0 :         memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t));
     127           0 :         memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char));
     128             : 
     129           0 :         tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
     130           0 :         memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo));
     131             : 
     132           0 :         tmp->timezone_abbr = (char*) malloc(tz->charcnt);
     133           0 :         memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt);
     134             : 
     135           0 :         tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
     136           0 :         memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo));
     137             : 
     138           0 :         return tmp;
     139             : }
     140             : 
     141        1596 : void timelib_tzinfo_dtor(timelib_tzinfo *tz)
     142             : {
     143        1596 :         TIMELIB_TIME_FREE(tz->name);
     144        1596 :         TIMELIB_TIME_FREE(tz->trans);
     145        1596 :         TIMELIB_TIME_FREE(tz->trans_idx);
     146        1596 :         TIMELIB_TIME_FREE(tz->type);
     147        1596 :         TIMELIB_TIME_FREE(tz->timezone_abbr);
     148        1596 :         TIMELIB_TIME_FREE(tz->leap_times);
     149        1596 :         TIMELIB_TIME_FREE(tz->location.comments);
     150        1596 :         TIMELIB_TIME_FREE(tz);
     151        1596 :         tz = NULL;
     152        1596 : }
     153             : 
     154           0 : char *timelib_get_tz_abbr_ptr(timelib_time *t)
     155             : {
     156           0 :         if (!t->sse_uptodate) {
     157           0 :                 timelib_update_ts(t, NULL);
     158             :         };
     159           0 :         return t->tz_abbr;
     160             : }
     161             : 
     162      184604 : void timelib_error_container_dtor(timelib_error_container *errors)
     163             : {
     164             :         int i;
     165             : 
     166      184836 :         for (i = 0; i < errors->warning_count; i++) {
     167         232 :                 free(errors->warning_messages[i].message);
     168             :         }
     169      184604 :         free(errors->warning_messages);
     170      184800 :         for (i = 0; i < errors->error_count; i++) {
     171         196 :                 free(errors->error_messages[i].message);
     172             :         }
     173      184604 :         free(errors->error_messages);
     174      184604 :         free(errors);
     175      184604 : }
     176             : 
     177        5392 : signed long timelib_date_to_int(timelib_time *d, int *error)
     178             : {
     179             :         timelib_sll ts;
     180             : 
     181        5392 :         ts = d->sse;
     182             : 
     183        5392 :         if (ts < LONG_MIN || ts > LONG_MAX) {
     184             :                 if (error) {
     185             :                         *error = 1;
     186             :                 }
     187             :                 return 0;
     188             :         }
     189        5392 :         if (error) {
     190        5392 :                 *error = 0;
     191             :         }
     192        5392 :         return (signed long) d->sse;
     193             : }
     194             : 
     195           0 : void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec)
     196             : {
     197           0 :         *hour = floor(h);
     198           0 :         *min =  floor((h - *hour) * 60);
     199           0 :         *sec =  (h - *hour - ((float) *min / 60)) * 3600;
     200           0 : }
     201             : 
     202           0 : void timelib_dump_date(timelib_time *d, int options)
     203             : {
     204           0 :         if ((options & 2) == 2) {
     205           0 :                 printf("TYPE: %d ", d->zone_type);
     206             :         }
     207           0 :         printf("TS: %lld | %s%04lld-%02lld-%02lld %02lld:%02lld:%02lld",
     208           0 :                 d->sse, d->y < 0 ? "-" : "", TIMELIB_LLABS(d->y), d->m, d->d, d->h, d->i, d->s);
     209           0 :         if (d->f > +0.0) {
     210           0 :                 printf(" %.5f", d->f);
     211             :         }
     212             : 
     213           0 :         if (d->is_localtime) {
     214           0 :                 switch (d->zone_type) {
     215             :                         case TIMELIB_ZONETYPE_OFFSET: /* Only offset */
     216           0 :                                 printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
     217           0 :                                 break;
     218             :                         case TIMELIB_ZONETYPE_ID: /* Timezone struct */
     219             :                                 /* Show abbreviation if wanted */
     220           0 :                                 if (d->tz_abbr) {
     221           0 :                                         printf(" %s", d->tz_abbr);
     222             :                                 }
     223             :                                 /* Do we have a TimeZone struct? */
     224           0 :                                 if (d->tz_info) {
     225           0 :                                         printf(" %s", d->tz_info->name);
     226             :                                 }
     227           0 :                                 break;
     228             :                         case TIMELIB_ZONETYPE_ABBR:
     229           0 :                                 printf(" %s", d->tz_abbr);
     230           0 :                                 printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
     231             :                                 break;
     232             :                 }
     233             :         }
     234             : 
     235           0 :         if ((options & 1) == 1) {
     236           0 :                 if (d->have_relative) {
     237           0 :                         printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS", 
     238             :                                 d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s);
     239           0 :                         if (d->relative.first_last_day_of != 0) {
     240           0 :                                 switch (d->relative.first_last_day_of) {
     241             :                                         case 1:
     242           0 :                                                 printf(" / first day of");
     243           0 :                                                 break;
     244             :                                         case 2:
     245           0 :                                                 printf(" / last day of");
     246             :                                                 break;
     247             :                                 }
     248             :                         }
     249           0 :                         if (d->relative.have_weekday_relative) {
     250           0 :                                 printf(" / %d.%d", d->relative.weekday, d->relative.weekday_behavior);
     251             :                         }
     252           0 :                         if (d->relative.have_special_relative) {
     253           0 :                                 switch (d->relative.special.type) {
     254             :                                         case TIMELIB_SPECIAL_WEEKDAY:
     255           0 :                                                 printf(" / %lld weekday", d->relative.special.amount);
     256           0 :                                                 break;
     257             :                                         case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
     258           0 :                                                 printf(" / x y of z month");
     259           0 :                                                 break;
     260             :                                         case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
     261           0 :                                                 printf(" / last y of z month");
     262             :                                                 break;
     263             :                                 }
     264             :                         }
     265             :                 }
     266             :         }
     267           0 :         printf("\n");
     268           0 : }
     269             : 
     270           0 : void timelib_dump_rel_time(timelib_rel_time *d)
     271             : {
     272           0 :         printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS (days: %lld)%s", 
     273           0 :                 d->y, d->m, d->d, d->h, d->i, d->s, d->days, d->invert ? " inverted" : "");
     274           0 :         if (d->first_last_day_of != 0) {
     275           0 :                 switch (d->first_last_day_of) {
     276             :                         case 1:
     277           0 :                                 printf(" / first day of");
     278           0 :                                 break;
     279             :                         case 2:
     280           0 :                                 printf(" / last day of");
     281             :                                 break;
     282             :                 }
     283             :         }
     284           0 :         printf("\n");
     285           0 : }
     286             : 

Generated by: LCOV version 1.10

Generated at Wed, 23 Jul 2014 19:58:30 +0000 (21 hours ago)

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