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: 201 233 86.3 %
Date: 2014-10-16 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       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    14132284 : static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
      32             : {
      33    14132284 :         if (*a < start) {
      34       76235 :                 *b -= (start - *a - 1) / adj + 1;
      35       76235 :                 *a += adj * ((start - *a - 1) / adj + 1);
      36             :         }
      37    14132284 :         if (*a >= end) {
      38       79373 :                 *b += *a / adj;
      39       79373 :                 *a -= adj * (*a / adj);
      40             :         }
      41    14132284 : }
      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      173476 : 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      173476 :         do_range_limit(1, 13, 12, base_m, base_y);
      68             : 
      69      173476 :         year = *base_y;
      70      173476 :         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      173476 :         if (!invert) {
      76      410865 :                 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      173476 : }
     102             : 
     103     2418806 : 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     2418806 :         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     2418806 :         do_range_limit(1, 13, 12, m, y);
     117             : 
     118     2418806 :         leapyear = timelib_is_leap(*y);
     119     2418806 :         days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
     120     2418806 :         last_month = (*m) - 1;
     121             : 
     122     2418806 :         if (last_month < 1) {
     123      393891 :                 last_month += 12;
     124      393891 :                 last_year = (*y) - 1;
     125             :         } else {
     126     2024915 :                 last_year = (*y);
     127             :         }
     128     2418806 :         leapyear = timelib_is_leap(last_year);
     129     2418806 :         days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];
     130             : 
     131     2418806 :         if (*d <= 0) {
     132       49912 :                 *d += days_last_month;
     133       49912 :                 (*m)--;
     134       49912 :                 return 1;
     135             :         }
     136     2368894 :         if (*d > days_this_month) {
     137      234369 :                 *d -= days_this_month;
     138      234369 :                 (*m)++;
     139      234369 :                 return 1;
     140             :         }
     141     2134525 :         return 0;
     142             : }
     143             : 
     144        1225 : static void do_adjust_for_weekday(timelib_time* time)
     145             : {
     146             :         timelib_sll current_dow, difference;
     147             : 
     148        1225 :         current_dow = timelib_day_of_week(time->y, time->m, time->d);
     149        1225 :         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        1222 :         difference = time->relative.weekday - current_dow;
     159        1222 :         if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
     160        1119 :                 difference += 7;
     161             :         }
     162        1222 :         if (time->relative.weekday >= 0) {
     163        1222 :                 time->d += difference;
     164             :         } else {
     165           0 :                 time->d -= (7 - (abs(time->relative.weekday) - current_dow));
     166             :         }
     167        1222 :         time->relative.have_weekday_relative = 0;
     168             : }
     169             : 
     170      173476 : void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
     171             : {
     172      173476 :         do_range_limit(0, 60, 60, &rt->s, &rt->i);
     173      173476 :         do_range_limit(0, 60, 60, &rt->i, &rt->h);
     174      173476 :         do_range_limit(0, 24, 24, &rt->h, &rt->d);
     175      173476 :         do_range_limit(0, 12, 12, &rt->m, &rt->y);
     176             : 
     177      173476 :         do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
     178      173476 :         do_range_limit(0, 12, 12, &rt->m, &rt->y);
     179      173476 : }
     180             : 
     181     2134525 : void timelib_do_normalize(timelib_time* time)
     182             : {
     183     2134525 :         if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->s, &time->i);
     184     2134525 :         if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->i, &time->h);
     185     2134525 :         if (time->s != TIMELIB_UNSET) do_range_limit(0, 24, 24, &time->h, &time->d);
     186     2134525 :         do_range_limit(1, 13, 12, &time->m, &time->y);
     187             : 
     188     2418806 :         do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
     189     2134525 :         do_range_limit(1, 13, 12, &time->m, &time->y);
     190     2134525 : }
     191             : 
     192      533631 : static void do_adjust_relative(timelib_time* time)
     193             : {
     194      533631 :         if (time->relative.have_weekday_relative) {
     195        1225 :                 do_adjust_for_weekday(time);
     196             :         }
     197      533631 :         timelib_do_normalize(time);
     198             : 
     199      533631 :         if (time->have_relative) {
     200      176772 :                 time->s += time->relative.s;
     201      176772 :                 time->i += time->relative.i;
     202      176772 :                 time->h += time->relative.h;
     203             : 
     204      176772 :                 time->d += time->relative.d;
     205      176772 :                 time->m += time->relative.m;
     206      176772 :                 time->y += time->relative.y;
     207             :         }
     208      533631 :         switch (time->relative.first_last_day_of) {
     209             :                 case 1: /* first */
     210          15 :                         time->d = 1;
     211          15 :                         break;
     212             :                 case 2: /* last */
     213           1 :                         time->d = 0;
     214           1 :                         time->m++;
     215             :                         break;
     216             :         }
     217      533631 :         timelib_do_normalize(time);
     218      533631 : }
     219             : 
     220           0 : static void do_adjust_special_weekday(timelib_time* time)
     221             : {
     222             :         timelib_sll current_dow, count;
     223             : 
     224           0 :         count = time->relative.special.amount;
     225             : 
     226           0 :         current_dow = timelib_day_of_week(time->y, time->m, time->d);
     227           0 :         if (count == 0) {
     228             :                 /* skip over saturday and sunday */
     229           0 :                 if (current_dow == 6) {
     230           0 :                         time->d += 2;
     231             :                 }
     232             :                 /* skip over sunday */
     233           0 :                 if (current_dow == 0) {
     234           0 :                         time->d += 1;
     235             :                 }
     236           0 :         } else if (count > 0) {
     237             :                 /* skip over saturday and sunday */
     238           0 :                 if (current_dow == 5) {
     239           0 :                         time->d += 2;
     240             :                 }
     241             :                 /* skip over sunday */
     242           0 :                 if (current_dow == 6) {
     243           0 :                         time->d += 1;
     244             :                 }
     245             :                 /* add increments of 5 weekdays as a week */
     246           0 :                 time->d += (count / 5) * 7;
     247             :                 /* if current DOW plus the remainder > 5, add two days */
     248           0 :                 current_dow = timelib_day_of_week(time->y, time->m, time->d);
     249           0 :                 time->d += (count % 5);
     250           0 :                 if ((count % 5) + current_dow > 5) {
     251           0 :                         time->d += 2;
     252             :                 }
     253           0 :         } else if (count < 0) {
     254             :                 /* skip over sunday and saturday */
     255           0 :                 if (current_dow == 1) {
     256           0 :                         time->d -= 2;
     257             :                 }
     258             :                 /* skip over satruday */
     259           0 :                 if (current_dow == 0 ) {
     260           0 :                         time->d -= 1;
     261             :                 }
     262             :                 /* subtract increments of 5 weekdays as a week */
     263           0 :                 time->d += (count / 5) * 7;
     264             :                 /* if current DOW minus the remainder < 0, subtract two days */
     265           0 :                 current_dow = timelib_day_of_week(time->y, time->m, time->d);
     266           0 :                 time->d += (count % 5);
     267           0 :                 if ((count % 5) + current_dow < 1) {
     268           0 :                         time->d -= 2;
     269             :                 }
     270             :         }
     271           0 : }
     272             : 
     273      533631 : static void do_adjust_special(timelib_time* time)
     274             : {
     275      533631 :         if (time->relative.have_special_relative) {
     276          29 :                 switch (time->relative.special.type) {
     277             :                         case TIMELIB_SPECIAL_WEEKDAY:
     278           0 :                                 do_adjust_special_weekday(time);
     279             :                                 break;
     280             :                 }
     281             :         }
     282      533631 :         timelib_do_normalize(time);
     283      533631 :         memset(&(time->relative.special), 0, sizeof(time->relative.special));
     284      533631 : }
     285             : 
     286      533631 : static void do_adjust_special_early(timelib_time* time)
     287             : {
     288      533631 :         if (time->relative.have_special_relative) {
     289          29 :                 switch (time->relative.special.type) {
     290             :                         case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
     291           4 :                                 time->d = 1;
     292           4 :                                 time->m += time->relative.m;
     293           4 :                                 time->relative.m = 0;
     294           4 :                                 break;
     295             :                         case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
     296          25 :                                 time->d = 1;
     297          25 :                                 time->m += time->relative.m + 1;
     298          25 :                                 time->relative.m = 0;
     299             :                                 break;
     300             :                 }
     301             :         }
     302      533631 :         timelib_do_normalize(time);
     303      533631 : }
     304             : 
     305      533631 : static timelib_sll do_years(timelib_sll year)
     306             : {
     307             :         timelib_sll i;
     308      533631 :         timelib_sll res = 0;
     309             :         timelib_sll eras;
     310             : 
     311      533631 :         eras = (year - 1970) / 40000;
     312      533631 :         if (eras != 0) {
     313          65 :                 year = year - (eras * 40000);
     314          65 :                 res += (SECS_PER_ERA * eras * 100);
     315             :         }
     316             : 
     317      533631 :         if (year >= 1970) {
     318    22824222 :                 for (i = year - 1; i >= 1970; i--) {
     319    27827842 :                         if (timelib_is_leap(i)) {
     320     5536948 :                                 res += (DAYS_PER_LYEAR * SECS_PER_DAY);
     321             :                         } else {
     322    16753946 :                                 res += (DAYS_PER_YEAR * SECS_PER_DAY);
     323             :                         }
     324             :                 }
     325             :         } else {
     326      919384 :                 for (i = 1969; i >= year; i--) {
     327     1141970 :                         if (timelib_is_leap(i)) {
     328      222889 :                                 res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
     329             :                         } else {
     330      696192 :                                 res -= (DAYS_PER_YEAR * SECS_PER_DAY);
     331             :                         }
     332             :                 }
     333             :         }
     334      533631 :         return res;
     335             : }
     336             : 
     337      533631 : static timelib_sll do_months(timelib_ull month, timelib_ull year)
     338             : {
     339      533631 :         if (timelib_is_leap(year)) {
     340       90692 :                 return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
     341             :         } else {
     342      442939 :                 return ((month_tab[month - 1]) * SECS_PER_DAY);
     343             :         }
     344             : }
     345             : 
     346      533631 : static timelib_sll do_days(timelib_ull day)
     347             : {
     348      533631 :         return ((day - 1) * SECS_PER_DAY);
     349             : }
     350             : 
     351      533631 : static timelib_sll do_time(timelib_ull hour, timelib_ull minute, timelib_ull second)
     352             : {
     353      533631 :         timelib_sll res = 0;
     354             : 
     355      533631 :         res += hour * 3600;
     356      533631 :         res += minute * 60;
     357      533631 :         res += second;
     358      533631 :         return res;
     359             : }
     360             : 
     361      533631 : static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
     362             : {
     363      533631 :         switch (tz->zone_type) {
     364             :                 case TIMELIB_ZONETYPE_OFFSET:
     365             : 
     366         509 :                         tz->is_localtime = 1;
     367         509 :                         return tz->z * 60;
     368             :                         break;
     369             : 
     370             :                 case TIMELIB_ZONETYPE_ABBR: {
     371             :                         timelib_sll tmp;
     372             : 
     373        2010 :                         tz->is_localtime = 1;
     374        2010 :                         tmp = tz->z;
     375        2010 :                         tmp -= tz->dst * 60;
     376        2010 :                         tmp *= 60;
     377        2010 :                         return tmp;
     378             :                         }
     379             :                         break;
     380             : 
     381             :                 case TIMELIB_ZONETYPE_ID:
     382      530214 :                         tzi = tz->tz_info;
     383             :                         /* Break intentionally missing */
     384             : 
     385             :                 default:
     386             :                         /* No timezone in struct, fallback to reference if possible */
     387      531112 :                         if (tzi) {
     388             :                                 timelib_time_offset *before, *after;
     389             :                                 timelib_sll          tmp;
     390             :                                 int                  in_transistion;
     391             :                                 
     392      530214 :                                 tz->is_localtime = 1;
     393      530214 :                                 before = timelib_get_time_zone_info(tz->sse, tzi);
     394      530214 :                                 after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
     395      530214 :                                 timelib_set_timezone(tz, tzi);
     396             : 
     397     1060274 :                                 in_transistion = (
     398      530214 :                                         ((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
     399      530060 :                                         ((tz->sse - after->offset) < after->transistion_time)
     400             :                                 );
     401             :                                 
     402      530593 :                                 if ((before->offset != after->offset) && !in_transistion) {
     403         379 :                                         tmp = -after->offset;
     404             :                                 } else {
     405      529835 :                                         tmp = -tz->z;
     406             :                                 }
     407      530214 :                                 timelib_time_offset_dtor(before);
     408      530214 :                                 timelib_time_offset_dtor(after);
     409             : 
     410             :                                 {
     411             :                                         timelib_time_offset *gmt_offset;
     412             : 
     413      530214 :                                         gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
     414      530214 :                                         tz->z = gmt_offset->offset;
     415             : 
     416      530214 :                                         tz->dst = gmt_offset->is_dst;
     417      530214 :                                         if (tz->tz_abbr) {
     418      530214 :                                                 free(tz->tz_abbr);
     419             :                                         }
     420      530214 :                                         tz->tz_abbr = strdup(gmt_offset->abbr);
     421      530214 :                                         timelib_time_offset_dtor(gmt_offset);
     422             :                                 }
     423      530214 :                                 return tmp;
     424             :                         }
     425             :         }
     426         898 :         return 0;
     427             : }
     428             : 
     429      533631 : void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
     430             : {
     431      533631 :         timelib_sll res = 0;
     432             : 
     433      533631 :         do_adjust_special_early(time);
     434      533631 :         do_adjust_relative(time);
     435      533631 :         do_adjust_special(time);
     436      533631 :         res += do_years(time->y);
     437      533631 :         res += do_months(time->m, time->y);
     438      533631 :         res += do_days(time->d);
     439      533631 :         res += do_time(time->h, time->i, time->s);
     440      533631 :         time->sse = res;
     441             : 
     442      533631 :         res += do_adjust_timezone(time, tzi);
     443      533631 :         time->sse = res;
     444             : 
     445      533631 :         time->sse_uptodate = 1;
     446      533631 :         time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = 0;
     447      533631 : }
     448             : 
     449             : #if 0
     450             : int main(void)
     451             : {
     452             :         timelib_sll res;
     453             :         timelib_time time;
     454             : 
     455             :         time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
     456             :         printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
     457             :                 time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
     458             :         if (time.have_relative) {
     459             :                 printf ("%3dY %3dM %3dD / %3dH %3dM %3dS", 
     460             :                         time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
     461             :         }
     462             :         if (time.have_weekday_relative) {
     463             :                 printf (" / %d", time.relative.weekday);
     464             :         }
     465             :         res = time2unixtime(&time);
     466             :         printf("%Ld\n", res);
     467             : 
     468             :         return 0;
     469             : }
     470             : #endif

Generated by: LCOV version 1.10

Generated at Thu, 16 Oct 2014 05:27:00 +0000 (6 days ago)

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