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: 84 162 51.9 %
Date: 2014-12-13 Functions: 14 19 73.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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             : #define HOUR(a) (int)(a * 60)
      34             : 
      35      725974 : timelib_time* timelib_time_ctor(void)
      36             : {
      37             :         timelib_time *t;
      38      725974 :         t = calloc(1, sizeof(timelib_time));
      39             : 
      40      725974 :         return t;
      41             : }
      42             : 
      43      349121 : timelib_rel_time* timelib_rel_time_ctor(void)
      44             : {
      45             :         timelib_rel_time *t;
      46      349121 :         t = calloc(1, sizeof(timelib_rel_time));
      47             : 
      48      349121 :         return t;
      49             : }
      50             : 
      51      175003 : timelib_time* timelib_time_clone(timelib_time *orig)
      52             : {
      53      175003 :         timelib_time *tmp = timelib_time_ctor();
      54      175003 :         memcpy(tmp, orig, sizeof(timelib_time));
      55      175003 :         if (orig->tz_abbr) {
      56      175001 :                 tmp->tz_abbr = strdup(orig->tz_abbr);
      57             :         }
      58      175003 :         if (orig->tz_info) {
      59      175001 :                 tmp->tz_info = orig->tz_info;
      60             :         }
      61      175003 :         return tmp;
      62             : }
      63             : 
      64          32 : timelib_rel_time* timelib_rel_time_clone(timelib_rel_time *rel)
      65             : {
      66          32 :         timelib_rel_time *tmp = timelib_rel_time_ctor();
      67          32 :         memcpy(tmp, rel, sizeof(timelib_rel_time));
      68          32 :         return tmp;
      69             : }
      70             : 
      71       15109 : void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
      72             : {
      73             :         unsigned int i;
      74       15109 :         size_t tz_abbr_len = strlen(tz_abbr);
      75             :         
      76       15109 :         TIMELIB_TIME_FREE(tm->tz_abbr);
      77       15109 :         tm->tz_abbr = strdup(tz_abbr);
      78       60831 :         for (i = 0; i < tz_abbr_len; i++) {
      79       45722 :                 tm->tz_abbr[i] = toupper(tz_abbr[i]);
      80             :         }
      81       15109 : }
      82             : 
      83      725965 : void timelib_time_dtor(timelib_time* t)
      84             : {
      85      725965 :         TIMELIB_TIME_FREE(t->tz_abbr);
      86      725965 :         TIMELIB_TIME_FREE(t);
      87      725965 : }
      88             : 
      89      349129 : void timelib_rel_time_dtor(timelib_rel_time* t)
      90             : {
      91      349129 :         TIMELIB_TIME_FREE(t);
      92      349129 : }
      93             : 
      94     2323960 : timelib_time_offset* timelib_time_offset_ctor(void)
      95             : {
      96             :         timelib_time_offset *t;
      97     2323960 :         t = calloc(1, sizeof(timelib_time_offset));
      98             : 
      99     2323960 :         return t;
     100             : }
     101             : 
     102     2323960 : void timelib_time_offset_dtor(timelib_time_offset* t)
     103             : {
     104     2323960 :         TIMELIB_TIME_FREE(t->abbr);
     105     2323960 :         TIMELIB_TIME_FREE(t);
     106     2323960 : }
     107             : 
     108        1754 : timelib_tzinfo* timelib_tzinfo_ctor(char *name)
     109             : {
     110             :         timelib_tzinfo *t;
     111        1754 :         t = calloc(1, sizeof(timelib_tzinfo));
     112        1754 :         t->name = strdup(name);
     113             : 
     114        1754 :         return t;
     115             : }
     116             : 
     117           0 : timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
     118             : {
     119           0 :         timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
     120           0 :         tmp->ttisgmtcnt = tz->ttisgmtcnt;
     121           0 :         tmp->ttisstdcnt = tz->ttisstdcnt;
     122           0 :         tmp->leapcnt = tz->leapcnt;
     123           0 :         tmp->timecnt = tz->timecnt;
     124           0 :         tmp->typecnt = tz->typecnt;
     125           0 :         tmp->charcnt = tz->charcnt;
     126             :         
     127           0 :         tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t));
     128           0 :         tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
     129           0 :         memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t));
     130           0 :         memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char));
     131             : 
     132           0 :         tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
     133           0 :         memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo));
     134             : 
     135           0 :         tmp->timezone_abbr = (char*) malloc(tz->charcnt);
     136           0 :         memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt);
     137             : 
     138           0 :         tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
     139           0 :         memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo));
     140             : 
     141           0 :         return tmp;
     142             : }
     143             : 
     144        1754 : void timelib_tzinfo_dtor(timelib_tzinfo *tz)
     145             : {
     146        1754 :         TIMELIB_TIME_FREE(tz->name);
     147        1754 :         TIMELIB_TIME_FREE(tz->trans);
     148        1754 :         TIMELIB_TIME_FREE(tz->trans_idx);
     149        1754 :         TIMELIB_TIME_FREE(tz->type);
     150        1754 :         TIMELIB_TIME_FREE(tz->timezone_abbr);
     151        1754 :         TIMELIB_TIME_FREE(tz->leap_times);
     152        1754 :         TIMELIB_TIME_FREE(tz->location.comments);
     153        1754 :         TIMELIB_TIME_FREE(tz);
     154        1754 :         tz = NULL;
     155        1754 : }
     156             : 
     157           0 : char *timelib_get_tz_abbr_ptr(timelib_time *t)
     158             : {
     159           0 :         if (!t->sse_uptodate) {
     160           0 :                 timelib_update_ts(t, NULL);
     161             :         };
     162           0 :         return t->tz_abbr;
     163             : }
     164             : 
     165      185148 : void timelib_error_container_dtor(timelib_error_container *errors)
     166             : {
     167             :         int i;
     168             : 
     169      185392 :         for (i = 0; i < errors->warning_count; i++) {
     170         244 :                 free(errors->warning_messages[i].message);
     171             :         }
     172      185148 :         free(errors->warning_messages);
     173      185365 :         for (i = 0; i < errors->error_count; i++) {
     174         217 :                 free(errors->error_messages[i].message);
     175             :         }
     176      185148 :         free(errors->error_messages);
     177      185148 :         free(errors);
     178      185148 : }
     179             : 
     180        5441 : timelib_long timelib_date_to_int(timelib_time *d, int *error)
     181             : {
     182             :         timelib_sll ts;
     183             : 
     184        5441 :         ts = d->sse;
     185             : 
     186        5441 :         if (ts < TIMELIB_LONG_MIN || ts > TIMELIB_LONG_MAX) {
     187             :                 if (error) {
     188             :                         *error = 1;
     189             :                 }
     190             :                 return 0;
     191             :         }
     192        5441 :         if (error) {
     193        5441 :                 *error = 0;
     194             :         }
     195        5441 :         return (timelib_long) d->sse;
     196             : }
     197             : 
     198           0 : void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec)
     199             : {
     200           0 :         *hour = floor(h);
     201           0 :         *min =  floor((h - *hour) * 60);
     202           0 :         *sec =  (h - *hour - ((float) *min / 60)) * 3600;
     203           0 : }
     204             : 
     205           0 : void timelib_dump_date(timelib_time *d, int options)
     206             : {
     207           0 :         if ((options & 2) == 2) {
     208           0 :                 printf("TYPE: %d ", d->zone_type);
     209             :         }
     210           0 :         printf("TS: %lld | %s%04lld-%02lld-%02lld %02lld:%02lld:%02lld",
     211           0 :                 d->sse, d->y < 0 ? "-" : "", TIMELIB_LLABS(d->y), d->m, d->d, d->h, d->i, d->s);
     212           0 :         if (d->f > +0.0) {
     213           0 :                 printf(" %.5f", d->f);
     214             :         }
     215             : 
     216           0 :         if (d->is_localtime) {
     217           0 :                 switch (d->zone_type) {
     218             :                         case TIMELIB_ZONETYPE_OFFSET: /* Only offset */
     219           0 :                                 printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
     220           0 :                                 break;
     221             :                         case TIMELIB_ZONETYPE_ID: /* Timezone struct */
     222             :                                 /* Show abbreviation if wanted */
     223           0 :                                 if (d->tz_abbr) {
     224           0 :                                         printf(" %s", d->tz_abbr);
     225             :                                 }
     226             :                                 /* Do we have a TimeZone struct? */
     227           0 :                                 if (d->tz_info) {
     228           0 :                                         printf(" %s", d->tz_info->name);
     229             :                                 }
     230           0 :                                 break;
     231             :                         case TIMELIB_ZONETYPE_ABBR:
     232           0 :                                 printf(" %s", d->tz_abbr);
     233           0 :                                 printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
     234             :                                 break;
     235             :                 }
     236             :         }
     237             : 
     238           0 :         if ((options & 1) == 1) {
     239           0 :                 if (d->have_relative) {
     240           0 :                         printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS", 
     241             :                                 d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s);
     242           0 :                         if (d->relative.first_last_day_of != 0) {
     243           0 :                                 switch (d->relative.first_last_day_of) {
     244             :                                         case 1:
     245           0 :                                                 printf(" / first day of");
     246           0 :                                                 break;
     247             :                                         case 2:
     248           0 :                                                 printf(" / last day of");
     249             :                                                 break;
     250             :                                 }
     251             :                         }
     252           0 :                         if (d->relative.have_weekday_relative) {
     253           0 :                                 printf(" / %d.%d", d->relative.weekday, d->relative.weekday_behavior);
     254             :                         }
     255           0 :                         if (d->relative.have_special_relative) {
     256           0 :                                 switch (d->relative.special.type) {
     257             :                                         case TIMELIB_SPECIAL_WEEKDAY:
     258           0 :                                                 printf(" / %lld weekday", d->relative.special.amount);
     259           0 :                                                 break;
     260             :                                         case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
     261           0 :                                                 printf(" / x y of z month");
     262           0 :                                                 break;
     263             :                                         case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
     264           0 :                                                 printf(" / last y of z month");
     265             :                                                 break;
     266             :                                 }
     267             :                         }
     268             :                 }
     269             :         }
     270           0 :         printf("\n");
     271           0 : }
     272             : 
     273           0 : void timelib_dump_rel_time(timelib_rel_time *d)
     274             : {
     275           0 :         printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS (days: %lld)%s", 
     276           0 :                 d->y, d->m, d->d, d->h, d->i, d->s, d->days, d->invert ? " inverted" : "");
     277           0 :         if (d->first_last_day_of != 0) {
     278           0 :                 switch (d->first_last_day_of) {
     279             :                         case 1:
     280           0 :                                 printf(" / first day of");
     281           0 :                                 break;
     282             :                         case 2:
     283           0 :                                 printf(" / last day of");
     284             :                                 break;
     285             :                 }
     286             :         }
     287           0 :         printf("\n");
     288           0 : }
     289             : 
     290         244 : timelib_long timelib_parse_tz_cor(char **ptr)
     291             : {
     292         244 :         char *begin = *ptr, *end;
     293             :         timelib_long  tmp;
     294             : 
     295        1451 :         while (isdigit(**ptr) || **ptr == ':') {
     296         963 :                 ++*ptr;
     297             :         }
     298         244 :         end = *ptr;
     299         244 :         switch (end - begin) {
     300             :                 case 1:
     301             :                 case 2:
     302          28 :                         return HOUR(strtol(begin, NULL, 10));
     303             :                         break;
     304             :                 case 3:
     305             :                 case 4:
     306         166 :                         if (begin[1] == ':') {
     307           2 :                                 tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 2, NULL, 10);
     308           2 :                                 return tmp;
     309         164 :                         } else if (begin[2] == ':') {
     310           0 :                                 tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
     311           0 :                                 return tmp;
     312             :                         } else {
     313         164 :                                 tmp = strtol(begin, NULL, 10);
     314         164 :                                 return HOUR(tmp / 100) + tmp % 100;
     315             :                         }
     316             :                 case 5:
     317          50 :                         tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
     318          50 :                         return tmp;
     319             :         }
     320           0 :         return 0;
     321             : }

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:11 +0000 (7 days ago)

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