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 - tm2unixtime.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 228 230 99.1 %
Date: 2014-10-22 Functions: 18 18 100.0 %
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             : 
      23             : /*                                    jan  feb  mrt  apr  may  jun  jul  aug  sep  oct  nov  dec */
      24             : static int month_tab_leap[12]     = {  -1,  30,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
      25             : static int month_tab[12]          = {   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
      26             : 
      27             : /*                                    dec  jan  feb  mrt  apr  may  jun  jul  aug  sep  oct  nov  dec */
      28             : static int days_in_month_leap[13] = {  31,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
      29             : static int days_in_month[13]      = {  31,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
      30             : 
      31    14146516 : static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
      32             : {
      33    14146516 :         if (*a < start) {
      34       76389 :                 *b -= (start - *a - 1) / adj + 1;
      35       76389 :                 *a += adj * ((start - *a - 1) / adj + 1);
      36             :         }
      37    14146516 :         if (*a >= end) {
      38       79378 :                 *b += *a / adj;
      39       79378 :                 *a -= adj * (*a / adj);
      40             :         }
      41    14146516 : }
      42             : 
      43        3561 : static void inc_month(timelib_sll *y, timelib_sll *m)
      44             : {
      45        3561 :         (*m)++;
      46        3561 :         if (*m > 12) {
      47        1326 :                 *m -= 12;
      48        1326 :                 (*y)++;
      49             :         }
      50        3561 : }
      51             : 
      52       78741 : static void dec_month(timelib_sll *y, timelib_sll *m)
      53             : {
      54       78741 :         (*m)--;
      55       78741 :         if (*m < 1) {
      56        5958 :                 *m += 12;
      57        5958 :                 (*y)--;
      58             :         }
      59       78741 : }
      60             : 
      61      173479 : static void do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d, timelib_sll invert)
      62             : {
      63             :         timelib_sll leapyear;
      64             :         timelib_sll month, year;
      65             :         timelib_sll days;
      66             : 
      67      173479 :         do_range_limit(1, 13, 12, base_m, base_y);
      68             : 
      69      173479 :         year = *base_y;
      70      173479 :         month = *base_m;
      71             : 
      72             : /*
      73             :         printf( "S: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
      74             : */
      75      173479 :         if (!invert) {
      76      410871 :                 while (*d < 0) {
      77       78741 :                         dec_month(&year, &month);
      78       78741 :                         leapyear = timelib_is_leap(year);
      79       78741 :                         days = leapyear ? days_in_month_leap[month] : days_in_month[month];
      80             : 
      81             :                         /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
      82             : 
      83       78741 :                         *d += days;
      84       78741 :                         (*m)--;
      85             :                 }
      86             :         } else {
      87       18389 :                 while (*d < 0) {
      88        3561 :                         leapyear = timelib_is_leap(year);
      89        3561 :                         days = leapyear ? days_in_month_leap[month] : days_in_month[month];
      90             : 
      91             :                         /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
      92             : 
      93        3561 :                         *d += days;
      94        3561 :                         (*m)--;
      95        3561 :                         inc_month(&year, &month);
      96             :                 }
      97             :         }
      98             :         /*
      99             :         printf( "E: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
     100             :         */
     101      173479 : }
     102             : 
     103     2422760 : static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
     104             : {
     105             :         timelib_sll leapyear;
     106             :         timelib_sll days_this_month;
     107             :         timelib_sll last_month, last_year;
     108             :         timelib_sll days_last_month;
     109             :         
     110             :         /* can jump an entire leap year period quickly */
     111     2422760 :         if (*d >= DAYS_PER_LYEAR_PERIOD || *d <= -DAYS_PER_LYEAR_PERIOD) {
     112          19 :                 *y += YEARS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
     113          19 :                 *d -= DAYS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
     114             :         }
     115             : 
     116     2422760 :         do_range_limit(1, 13, 12, m, y);
     117             : 
     118     2422760 :         leapyear = timelib_is_leap(*y);
     119     2422760 :         days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
     120     2422760 :         last_month = (*m) - 1;
     121             : 
     122     2422760 :         if (last_month < 1) {
     123      394245 :                 last_month += 12;
     124      394245 :                 last_year = (*y) - 1;
     125             :         } else {
     126     2028515 :                 last_year = (*y);
     127             :         }
     128     2422760 :         leapyear = timelib_is_leap(last_year);
     129     2422760 :         days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];
     130             : 
     131     2422760 :         if (*d <= 0) {
     132       51653 :                 *d += days_last_month;
     133       51653 :                 (*m)--;
     134       51653 :                 return 1;
     135             :         }
     136     2371107 :         if (*d > days_this_month) {
     137      234530 :                 *d -= days_this_month;
     138      234530 :                 (*m)++;
     139      234530 :                 return 1;
     140             :         }
     141     2136577 :         return 0;
     142             : }
     143             : 
     144        1231 : static void do_adjust_for_weekday(timelib_time* time)
     145             : {
     146             :         timelib_sll current_dow, difference;
     147             : 
     148        1231 :         current_dow = timelib_day_of_week(time->y, time->m, time->d);
     149        1231 :         if (time->relative.weekday_behavior == 2)
     150             :         {
     151           3 :                 if (time->relative.weekday == 0) {
     152           0 :                         time->relative.weekday = 7;
     153             :                 }
     154           3 :                 time->d -= current_dow;
     155           3 :                 time->d += time->relative.weekday;
     156           3 :                 return;
     157             :         }
     158        1228 :         difference = time->relative.weekday - current_dow;
     159        1228 :         if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
     160         119 :                 difference += 7;
     161             :         }
     162        1228 :         if (time->relative.weekday >= 0) {
     163        1228 :                 time->d += difference;
     164             :         } else {
     165           0 :                 time->d -= (7 - (abs(time->relative.weekday) - current_dow));
     166             :         }
     167        1228 :         time->relative.have_weekday_relative = 0;
     168             : }
     169             : 
     170      173479 : void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
     171             : {
     172      173479 :         do_range_limit(0, 60, 60, &rt->s, &rt->i);
     173      173479 :         do_range_limit(0, 60, 60, &rt->i, &rt->h);
     174      173479 :         do_range_limit(0, 24, 24, &rt->h, &rt->d);
     175      173479 :         do_range_limit(0, 12, 12, &rt->m, &rt->y);
     176             : 
     177      173479 :         do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
     178      173479 :         do_range_limit(0, 12, 12, &rt->m, &rt->y);
     179      173479 : }
     180             : 
     181     2136577 : void timelib_do_normalize(timelib_time* time)
     182             : {
     183     2136577 :         if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->s, &time->i);
     184     2136577 :         if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->i, &time->h);
     185     2136577 :         if (time->s != TIMELIB_UNSET) do_range_limit(0, 24, 24, &time->h, &time->d);
     186     2136577 :         do_range_limit(1, 13, 12, &time->m, &time->y);
     187             : 
     188     2422760 :         do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
     189     2136577 :         do_range_limit(1, 13, 12, &time->m, &time->y);
     190     2136577 : }
     191             : 
     192      534144 : static void do_adjust_relative(timelib_time* time)
     193             : {
     194      534144 :         if (time->relative.have_weekday_relative) {
     195        1231 :                 do_adjust_for_weekday(time);
     196             :         }
     197      534144 :         timelib_do_normalize(time);
     198             : 
     199      534144 :         if (time->have_relative) {
     200      176982 :                 time->s += time->relative.s;
     201      176982 :                 time->i += time->relative.i;
     202      176982 :                 time->h += time->relative.h;
     203             : 
     204      176982 :                 time->d += time->relative.d;
     205      176982 :                 time->m += time->relative.m;
     206      176982 :                 time->y += time->relative.y;
     207             :         }
     208      534144 :         switch (time->relative.first_last_day_of) {
     209             :                 case 1: /* first */
     210          21 :                         time->d = 1;
     211          21 :                         break;
     212             :                 case 2: /* last */
     213           4 :                         time->d = 0;
     214           4 :                         time->m++;
     215             :                         break;
     216             :         }
     217      534144 :         timelib_do_normalize(time);
     218      534144 : }
     219             : 
     220         184 : static void do_adjust_special_weekday(timelib_time* time)
     221             : {
     222             :         timelib_sll count, dow, rem;
     223             : 
     224         184 :         count = time->relative.special.amount;
     225         184 :         dow = timelib_day_of_week(time->y, time->m, time->d);
     226             : 
     227             :         /* Add increments of 5 weekdays as a week, leaving the DOW unchanged. */
     228         184 :         time->d += (count / 5) * 7;
     229             : 
     230             :         /* Deal with the remainder. */
     231         184 :         rem = (count % 5);
     232             : 
     233         184 :         if (count > 0) {
     234          88 :                 if (rem == 0) {
     235             :                         /* Head back to Friday if we stop on the weekend. */
     236          16 :                         if (dow == 0) {
     237           2 :                                 time->d -= 2;
     238          14 :                         } else if (dow == 6) {
     239           2 :                                 time->d -= 1;
     240             :                         }
     241          72 :                 } else if (dow == 6) {
     242             :                         /* We ended up on Saturday, but there's still work to do, so move
     243             :                          * to Sunday and continue from there. */
     244           9 :                         time->d += 1;
     245          63 :                 } else if (dow + rem > 5) {
     246             :                         /* We're on a weekday, but we're going past Friday, so skip right
     247             :                          * over the weekend. */
     248          27 :                         time->d += 2;
     249             :                 }
     250             :         } else {
     251             :                 /* Completely mirror the forward direction. This also covers the 0
     252             :                  * case, since if we start on the weekend, we want to move forward as
     253             :                  * if we stopped there while going backwards. */
     254          96 :                 if (rem == 0) {
     255          24 :                         if (dow == 6) {
     256           3 :                                 time->d += 2;
     257          21 :                         } else if (dow == 0) {
     258           3 :                                 time->d += 1;
     259             :                         }
     260          72 :                 } else if (dow == 0) {
     261           9 :                         time->d -= 1;
     262          63 :                 } else if (dow + rem < 1) {
     263          23 :                         time->d -= 2;
     264             :                 }
     265             :         }
     266             : 
     267         184 :         time->d += rem;
     268         184 : }
     269             : 
     270      534144 : static void do_adjust_special(timelib_time* time)
     271             : {
     272      534144 :         if (time->relative.have_special_relative) {
     273         213 :                 switch (time->relative.special.type) {
     274             :                         case TIMELIB_SPECIAL_WEEKDAY:
     275         184 :                                 do_adjust_special_weekday(time);
     276             :                                 break;
     277             :                 }
     278             :         }
     279      534144 :         timelib_do_normalize(time);
     280      534144 :         memset(&(time->relative.special), 0, sizeof(time->relative.special));
     281      534144 : }
     282             : 
     283      534144 : static void do_adjust_special_early(timelib_time* time)
     284             : {
     285      534144 :         if (time->relative.have_special_relative) {
     286         213 :                 switch (time->relative.special.type) {
     287             :                         case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
     288           4 :                                 time->d = 1;
     289           4 :                                 time->m += time->relative.m;
     290           4 :                                 time->relative.m = 0;
     291           4 :                                 break;
     292             :                         case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
     293          25 :                                 time->d = 1;
     294          25 :                                 time->m += time->relative.m + 1;
     295          25 :                                 time->relative.m = 0;
     296             :                                 break;
     297             :                 }
     298             :         }
     299      534144 :         timelib_do_normalize(time);
     300      534144 : }
     301             : 
     302      534144 : static timelib_sll do_years(timelib_sll year)
     303             : {
     304             :         timelib_sll i;
     305      534144 :         timelib_sll res = 0;
     306             :         timelib_sll eras;
     307             : 
     308      534144 :         eras = (year - 1970) / 40000;
     309      534144 :         if (eras != 0) {
     310          65 :                 year = year - (eras * 40000);
     311          65 :                 res += (SECS_PER_ERA * eras * 100);
     312             :         }
     313             : 
     314      534144 :         if (year >= 1970) {
     315    22846117 :                 for (i = year - 1; i >= 1970; i--) {
     316    27854334 :                         if (timelib_is_leap(i)) {
     317     5542053 :                                 res += (DAYS_PER_LYEAR * SECS_PER_DAY);
     318             :                         } else {
     319    16770228 :                                 res += (DAYS_PER_YEAR * SECS_PER_DAY);
     320             :                         }
     321             :                 }
     322             :         } else {
     323      923462 :                 for (i = 1969; i >= year; i--) {
     324     1147031 :                         if (timelib_is_leap(i)) {
     325      223877 :                                 res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
     326             :                         } else {
     327      699277 :                                 res -= (DAYS_PER_YEAR * SECS_PER_DAY);
     328             :                         }
     329             :                 }
     330             :         }
     331      534144 :         return res;
     332             : }
     333             : 
     334      534144 : static timelib_sll do_months(timelib_ull month, timelib_ull year)
     335             : {
     336      534144 :         if (timelib_is_leap(year)) {
     337       91178 :                 return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
     338             :         } else {
     339      442966 :                 return ((month_tab[month - 1]) * SECS_PER_DAY);
     340             :         }
     341             : }
     342             : 
     343      534144 : static timelib_sll do_days(timelib_ull day)
     344             : {
     345      534144 :         return ((day - 1) * SECS_PER_DAY);
     346             : }
     347             : 
     348      534144 : static timelib_sll do_time(timelib_ull hour, timelib_ull minute, timelib_ull second)
     349             : {
     350      534144 :         timelib_sll res = 0;
     351             : 
     352      534144 :         res += hour * 3600;
     353      534144 :         res += minute * 60;
     354      534144 :         res += second;
     355      534144 :         return res;
     356             : }
     357             : 
     358      534144 : static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
     359             : {
     360      534144 :         switch (tz->zone_type) {
     361             :                 case TIMELIB_ZONETYPE_OFFSET:
     362             : 
     363         541 :                         tz->is_localtime = 1;
     364         541 :                         return tz->z * 60;
     365             :                         break;
     366             : 
     367             :                 case TIMELIB_ZONETYPE_ABBR: {
     368             :                         timelib_sll tmp;
     369             : 
     370        2025 :                         tz->is_localtime = 1;
     371        2025 :                         tmp = tz->z;
     372        2025 :                         tmp -= tz->dst * 60;
     373        2025 :                         tmp *= 60;
     374        2025 :                         return tmp;
     375             :                         }
     376             :                         break;
     377             : 
     378             :                 case TIMELIB_ZONETYPE_ID:
     379      530679 :                         tzi = tz->tz_info;
     380             :                         /* Break intentionally missing */
     381             : 
     382             :                 default:
     383             :                         /* No timezone in struct, fallback to reference if possible */
     384      531578 :                         if (tzi) {
     385             :                                 timelib_time_offset *before, *after;
     386             :                                 timelib_sll          tmp;
     387             :                                 int                  in_transistion;
     388             :                                 
     389      530679 :                                 tz->is_localtime = 1;
     390      530679 :                                 before = timelib_get_time_zone_info(tz->sse, tzi);
     391      530679 :                                 after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
     392      530679 :                                 timelib_set_timezone(tz, tzi);
     393             : 
     394     1061202 :                                 in_transistion = (
     395      530679 :                                         ((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
     396      530523 :                                         ((tz->sse - after->offset) < after->transistion_time)
     397             :                                 );
     398             :                                 
     399      531058 :                                 if ((before->offset != after->offset) && !in_transistion) {
     400         379 :                                         tmp = -after->offset;
     401             :                                 } else {
     402      530300 :                                         tmp = -tz->z;
     403             :                                 }
     404      530679 :                                 timelib_time_offset_dtor(before);
     405      530679 :                                 timelib_time_offset_dtor(after);
     406             : 
     407             :                                 {
     408             :                                         timelib_time_offset *gmt_offset;
     409             : 
     410      530679 :                                         gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
     411      530679 :                                         tz->z = gmt_offset->offset;
     412             : 
     413      530679 :                                         tz->dst = gmt_offset->is_dst;
     414      530679 :                                         if (tz->tz_abbr) {
     415      530679 :                                                 free(tz->tz_abbr);
     416             :                                         }
     417      530679 :                                         tz->tz_abbr = strdup(gmt_offset->abbr);
     418      530679 :                                         timelib_time_offset_dtor(gmt_offset);
     419             :                                 }
     420      530679 :                                 return tmp;
     421             :                         }
     422             :         }
     423         899 :         return 0;
     424             : }
     425             : 
     426      534144 : void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
     427             : {
     428      534144 :         timelib_sll res = 0;
     429             : 
     430      534144 :         do_adjust_special_early(time);
     431      534144 :         do_adjust_relative(time);
     432      534144 :         do_adjust_special(time);
     433      534144 :         res += do_years(time->y);
     434      534144 :         res += do_months(time->m, time->y);
     435      534144 :         res += do_days(time->d);
     436      534144 :         res += do_time(time->h, time->i, time->s);
     437      534144 :         time->sse = res;
     438             : 
     439      534144 :         res += do_adjust_timezone(time, tzi);
     440      534144 :         time->sse = res;
     441             : 
     442      534144 :         time->sse_uptodate = 1;
     443      534144 :         time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = 0;
     444      534144 : }
     445             : 
     446             : #if 0
     447             : int main(void)
     448             : {
     449             :         timelib_sll res;
     450             :         timelib_time time;
     451             : 
     452             :         time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
     453             :         printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
     454             :                 time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
     455             :         if (time.have_relative) {
     456             :                 printf ("%3dY %3dM %3dD / %3dH %3dM %3dS", 
     457             :                         time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
     458             :         }
     459             :         if (time.have_weekday_relative) {
     460             :                 printf (" / %d", time.relative.weekday);
     461             :         }
     462             :         res = time2unixtime(&time);
     463             :         printf("%Ld\n", res);
     464             : 
     465             :         return 0;
     466             : }
     467             : #endif

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:47 +0000 (3 days ago)

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