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-04-08 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-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             : 
      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    14130427 : static int do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
      32             : {
      33    14130427 :         if (*a < start) {
      34       76384 :                 *b -= (start - *a - 1) / adj + 1;
      35       76384 :                 *a += adj * ((start - *a - 1) / adj + 1);
      36             :         }
      37    14130427 :         if (*a >= end) {
      38       79144 :                 *b += *a / adj;
      39       79144 :                 *a -= adj * (*a / adj);
      40             :         }
      41    14130427 :         return 0;
      42             : }
      43             : 
      44        3561 : static void inc_month(timelib_sll *y, timelib_sll *m)
      45             : {
      46        3561 :         (*m)++;
      47        3561 :         if (*m > 12) {
      48        1326 :                 *m -= 12;
      49        1326 :                 (*y)++;
      50             :         }
      51        3561 : }
      52             : 
      53       78741 : static void dec_month(timelib_sll *y, timelib_sll *m)
      54             : {
      55       78741 :         (*m)--;
      56       78741 :         if (*m < 1) {
      57        5958 :                 *m += 12;
      58        5958 :                 (*y)--;
      59             :         }
      60       78741 : }
      61             : 
      62      173475 : 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)
      63             : {
      64             :         timelib_sll leapyear;
      65             :         timelib_sll month, year;
      66             :         timelib_sll days;
      67             : 
      68      173475 :         do_range_limit(1, 13, 12, base_m, base_y);
      69             : 
      70      173475 :         year = *base_y;
      71      173475 :         month = *base_m;
      72             : 
      73             : /*
      74             :         printf( "S: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
      75             : */
      76      173475 :         if (!invert) {
      77      410861 :                 while (*d < 0) {
      78       78741 :                         dec_month(&year, &month);
      79       78741 :                         leapyear = timelib_is_leap(year);
      80       78741 :                         days = leapyear ? days_in_month_leap[month] : days_in_month[month];
      81             : 
      82             :                         /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
      83             : 
      84       78741 :                         *d += days;
      85       78741 :                         (*m)--;
      86             :                 }
      87             :         } else {
      88       18391 :                 while (*d < 0) {
      89        3561 :                         leapyear = timelib_is_leap(year);
      90        3561 :                         days = leapyear ? days_in_month_leap[month] : days_in_month[month];
      91             : 
      92             :                         /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
      93             : 
      94        3561 :                         *d += days;
      95        3561 :                         (*m)--;
      96        3561 :                         inc_month(&year, &month);
      97             :                 }
      98             :         }
      99             :         /*
     100             :         printf( "E: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
     101             :         */
     102      173475 : }
     103             : 
     104     2417775 : static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
     105             : {
     106             :         timelib_sll leapyear;
     107             :         timelib_sll days_this_month;
     108             :         timelib_sll last_month, last_year;
     109             :         timelib_sll days_last_month;
     110             :         
     111             :         /* can jump an entire leap year period quickly */
     112     2417775 :         if (*d >= DAYS_PER_LYEAR_PERIOD || *d <= -DAYS_PER_LYEAR_PERIOD) {
     113          19 :                 *y += YEARS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
     114          19 :                 *d -= DAYS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
     115             :         }
     116             : 
     117     2417775 :         do_range_limit(1, 13, 12, m, y);
     118             : 
     119     2417775 :         leapyear = timelib_is_leap(*y);
     120     2417775 :         days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
     121     2417775 :         last_month = (*m) - 1;
     122             : 
     123     2417775 :         if (last_month < 1) {
     124      393795 :                 last_month += 12;
     125      393795 :                 last_year = (*y) - 1;
     126             :         } else {
     127     2023980 :                 last_year = (*y);
     128             :         }
     129     2417775 :         leapyear = timelib_is_leap(last_year);
     130     2417775 :         days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];
     131             : 
     132     2417775 :         if (*d <= 0) {
     133       51578 :                 *d += days_last_month;
     134       51578 :                 (*m)--;
     135       51578 :                 return 1;
     136             :         }
     137     2366197 :         if (*d > days_this_month) {
     138      231836 :                 *d -= days_this_month;
     139      231836 :                 (*m)++;
     140      231836 :                 return 1;
     141             :         }
     142     2134361 :         return 0;
     143             : }
     144             : 
     145        1223 : static void do_adjust_for_weekday(timelib_time* time)
     146             : {
     147             :         timelib_sll current_dow, difference;
     148             : 
     149        1223 :         current_dow = timelib_day_of_week(time->y, time->m, time->d);
     150        1223 :         if (time->relative.weekday_behavior == 2)
     151             :         {
     152           3 :                 if (time->relative.weekday == 0) {
     153           0 :                         time->relative.weekday = 7;
     154             :                 }
     155           3 :                 time->d -= current_dow;
     156           3 :                 time->d += time->relative.weekday;
     157           3 :                 return;
     158             :         }
     159        1220 :         difference = time->relative.weekday - current_dow;
     160        1220 :         if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
     161         115 :                 difference += 7;
     162             :         }
     163        1220 :         if (time->relative.weekday >= 0) {
     164        1220 :                 time->d += difference;
     165             :         } else {
     166           0 :                 time->d -= (7 - (abs(time->relative.weekday) - current_dow));
     167             :         }
     168        1220 :         time->relative.have_weekday_relative = 0;
     169             : }
     170             : 
     171      173475 : void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
     172             : {
     173      173475 :         do {} while (do_range_limit(0, 60, 60, &rt->s, &rt->i));
     174      173475 :         do {} while (do_range_limit(0, 60, 60, &rt->i, &rt->h));
     175      173475 :         do {} while (do_range_limit(0, 24, 24, &rt->h, &rt->d));
     176      173475 :         do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));
     177             : 
     178      173475 :         do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
     179      173475 :         do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));
     180      173475 : }
     181             : 
     182     2134361 : void timelib_do_normalize(timelib_time* time)
     183             : {
     184     2134361 :         if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 60, 60, &time->s, &time->i));
     185     2134361 :         if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 60, 60, &time->i, &time->h));
     186     2134361 :         if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 24, 24, &time->h, &time->d));
     187     2134361 :         do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));
     188             : 
     189     2417775 :         do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
     190     2134361 :         do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));
     191     2134361 : }
     192             : 
     193      533590 : static void do_adjust_relative(timelib_time* time)
     194             : {
     195      533590 :         if (time->relative.have_weekday_relative) {
     196        1223 :                 do_adjust_for_weekday(time);
     197             :         }
     198      533590 :         timelib_do_normalize(time);
     199             : 
     200      533590 :         if (time->have_relative) {
     201      176762 :                 time->s += time->relative.s;
     202      176762 :                 time->i += time->relative.i;
     203      176762 :                 time->h += time->relative.h;
     204             : 
     205      176762 :                 time->d += time->relative.d;
     206      176762 :                 time->m += time->relative.m;
     207      176762 :                 time->y += time->relative.y;
     208             :         }
     209      533590 :         switch (time->relative.first_last_day_of) {
     210             :                 case 1: /* first */
     211          15 :                         time->d = 1;
     212          15 :                         break;
     213             :                 case 2: /* last */
     214           1 :                         time->d = 0;
     215           1 :                         time->m++;
     216             :                         break;
     217             :         }
     218      533590 :         timelib_do_normalize(time);
     219      533590 : }
     220             : 
     221           0 : static void do_adjust_special_weekday(timelib_time* time)
     222             : {
     223             :         timelib_sll current_dow, count;
     224             : 
     225           0 :         count = time->relative.special.amount;
     226             : 
     227           0 :         current_dow = timelib_day_of_week(time->y, time->m, time->d);
     228           0 :         if (count == 0) {
     229             :                 /* skip over saturday and sunday */
     230           0 :                 if (current_dow == 6) {
     231           0 :                         time->d += 2;
     232             :                 }
     233             :                 /* skip over sunday */
     234           0 :                 if (current_dow == 0) {
     235           0 :                         time->d += 1;
     236             :                 }
     237           0 :         } else if (count > 0) {
     238             :                 /* skip over saturday and sunday */
     239           0 :                 if (current_dow == 5) {
     240           0 :                         time->d += 2;
     241             :                 }
     242             :                 /* skip over sunday */
     243           0 :                 if (current_dow == 6) {
     244           0 :                         time->d += 1;
     245             :                 }
     246             :                 /* add increments of 5 weekdays as a week */
     247           0 :                 time->d += (count / 5) * 7;
     248             :                 /* if current DOW plus the remainder > 5, add two days */
     249           0 :                 current_dow = timelib_day_of_week(time->y, time->m, time->d);
     250           0 :                 time->d += (count % 5);
     251           0 :                 if ((count % 5) + current_dow > 5) {
     252           0 :                         time->d += 2;
     253             :                 }
     254           0 :         } else if (count < 0) {
     255             :                 /* skip over sunday and saturday */
     256           0 :                 if (current_dow == 1) {
     257           0 :                         time->d -= 2;
     258             :                 }
     259             :                 /* skip over satruday */
     260           0 :                 if (current_dow == 0 ) {
     261           0 :                         time->d -= 1;
     262             :                 }
     263             :                 /* subtract increments of 5 weekdays as a week */
     264           0 :                 time->d += (count / 5) * 7;
     265             :                 /* if current DOW minus the remainder < 0, subtract two days */
     266           0 :                 current_dow = timelib_day_of_week(time->y, time->m, time->d);
     267           0 :                 time->d += (count % 5);
     268           0 :                 if ((count % 5) + current_dow < 1) {
     269           0 :                         time->d -= 2;
     270             :                 }
     271             :         }
     272           0 : }
     273             : 
     274      533590 : static void do_adjust_special(timelib_time* time)
     275             : {
     276      533590 :         if (time->relative.have_special_relative) {
     277          29 :                 switch (time->relative.special.type) {
     278             :                         case TIMELIB_SPECIAL_WEEKDAY:
     279           0 :                                 do_adjust_special_weekday(time);
     280             :                                 break;
     281             :                 }
     282             :         }
     283      533590 :         timelib_do_normalize(time);
     284      533590 :         memset(&(time->relative.special), 0, sizeof(time->relative.special));
     285      533590 : }
     286             : 
     287      533590 : static void do_adjust_special_early(timelib_time* time)
     288             : {
     289      533590 :         if (time->relative.have_special_relative) {
     290          29 :                 switch (time->relative.special.type) {
     291             :                         case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
     292           4 :                                 time->d = 1;
     293           4 :                                 time->m += time->relative.m;
     294           4 :                                 time->relative.m = 0;
     295           4 :                                 break;
     296             :                         case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
     297          25 :                                 time->d = 1;
     298          25 :                                 time->m += time->relative.m + 1;
     299          25 :                                 time->relative.m = 0;
     300             :                                 break;
     301             :                 }
     302             :         }
     303      533590 :         timelib_do_normalize(time);
     304      533590 : }
     305             : 
     306      533590 : static timelib_sll do_years(timelib_sll year)
     307             : {
     308             :         timelib_sll i;
     309      533590 :         timelib_sll res = 0;
     310             :         timelib_sll eras;
     311             : 
     312      533590 :         eras = (year - 1970) / 40000;
     313      533590 :         if (eras != 0) {
     314          65 :                 year = year - (eras * 40000);
     315          65 :                 res += (SECS_PER_ERA * eras * 100);
     316             :         }
     317             : 
     318      533590 :         if (year >= 1970) {
     319    22822716 :                 for (i = year - 1; i >= 1970; i--) {
     320    27826020 :                         if (timelib_is_leap(i)) {
     321     5536593 :                                 res += (DAYS_PER_LYEAR * SECS_PER_DAY);
     322             :                         } else {
     323    16752834 :                                 res += (DAYS_PER_YEAR * SECS_PER_DAY);
     324             :                         }
     325             :                 }
     326             :         } else {
     327      918033 :                 for (i = 1969; i >= year; i--) {
     328     1140294 :                         if (timelib_is_leap(i)) {
     329      222562 :                                 res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
     330             :                         } else {
     331      695170 :                                 res -= (DAYS_PER_YEAR * SECS_PER_DAY);
     332             :                         }
     333             :                 }
     334             :         }
     335      533590 :         return res;
     336             : }
     337             : 
     338      533590 : static timelib_sll do_months(timelib_ull month, timelib_ull year)
     339             : {
     340      533590 :         if (timelib_is_leap(year)) {
     341       90665 :                 return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
     342             :         } else {
     343      442925 :                 return ((month_tab[month - 1]) * SECS_PER_DAY);
     344             :         }
     345             : }
     346             : 
     347      533590 : static timelib_sll do_days(timelib_ull day)
     348             : {
     349      533590 :         return ((day - 1) * SECS_PER_DAY);
     350             : }
     351             : 
     352      533590 : static timelib_sll do_time(timelib_ull hour, timelib_ull minute, timelib_ull second)
     353             : {
     354      533590 :         timelib_sll res = 0;
     355             : 
     356      533590 :         res += hour * 3600;
     357      533590 :         res += minute * 60;
     358      533590 :         res += second;
     359      533590 :         return res;
     360             : }
     361             : 
     362      533590 : static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
     363             : {
     364      533590 :         switch (tz->zone_type) {
     365             :                 case TIMELIB_ZONETYPE_OFFSET:
     366             : 
     367         502 :                         tz->is_localtime = 1;
     368         502 :                         return tz->z * 60;
     369             :                         break;
     370             : 
     371             :                 case TIMELIB_ZONETYPE_ABBR: {
     372             :                         timelib_sll tmp;
     373             : 
     374        2005 :                         tz->is_localtime = 1;
     375        2005 :                         tmp = tz->z;
     376        2005 :                         tmp -= tz->dst * 60;
     377        2005 :                         tmp *= 60;
     378        2005 :                         return tmp;
     379             :                         }
     380             :                         break;
     381             : 
     382             :                 case TIMELIB_ZONETYPE_ID:
     383      530185 :                         tzi = tz->tz_info;
     384             :                         /* Break intentionally missing */
     385             : 
     386             :                 default:
     387             :                         /* No timezone in struct, fallback to reference if possible */
     388      531083 :                         if (tzi) {
     389             :                                 timelib_time_offset *before, *after;
     390             :                                 timelib_sll          tmp;
     391             :                                 int                  in_transistion;
     392             :                                 
     393      530185 :                                 tz->is_localtime = 1;
     394      530185 :                                 before = timelib_get_time_zone_info(tz->sse, tzi);
     395      530185 :                                 after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
     396      530185 :                                 timelib_set_timezone(tz, tzi);
     397             : 
     398     1060221 :                                 in_transistion = (
     399      530185 :                                         ((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
     400      530036 :                                         ((tz->sse - after->offset) < after->transistion_time)
     401             :                                 );
     402             :                                 
     403      530559 :                                 if ((before->offset != after->offset) && !in_transistion) {
     404         374 :                                         tmp = -after->offset;
     405             :                                 } else {
     406      529811 :                                         tmp = -tz->z;
     407             :                                 }
     408      530185 :                                 timelib_time_offset_dtor(before);
     409      530185 :                                 timelib_time_offset_dtor(after);
     410             : 
     411             :                                 {
     412             :                                         timelib_time_offset *gmt_offset;
     413             : 
     414      530185 :                                         gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
     415      530185 :                                         tz->z = gmt_offset->offset;
     416             : 
     417      530185 :                                         tz->dst = gmt_offset->is_dst;
     418      530185 :                                         if (tz->tz_abbr) {
     419      530185 :                                                 free(tz->tz_abbr);
     420             :                                         }
     421      530185 :                                         tz->tz_abbr = strdup(gmt_offset->abbr);
     422      530185 :                                         timelib_time_offset_dtor(gmt_offset);
     423             :                                 }
     424      530185 :                                 return tmp;
     425             :                         }
     426             :         }
     427         898 :         return 0;
     428             : }
     429             : 
     430      533590 : void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
     431             : {
     432      533590 :         timelib_sll res = 0;
     433             : 
     434      533590 :         do_adjust_special_early(time);
     435      533590 :         do_adjust_relative(time);
     436      533590 :         do_adjust_special(time);
     437      533590 :         res += do_years(time->y);
     438      533590 :         res += do_months(time->m, time->y);
     439      533590 :         res += do_days(time->d);
     440      533590 :         res += do_time(time->h, time->i, time->s);
     441      533590 :         time->sse = res;
     442             : 
     443      533590 :         res += do_adjust_timezone(time, tzi);
     444      533590 :         time->sse = res;
     445             : 
     446      533590 :         time->sse_uptodate = 1;
     447      533590 :         time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = 0;
     448      533590 : }
     449             : 
     450             : #if 0
     451             : int main(void)
     452             : {
     453             :         timelib_sll res;
     454             :         timelib_time time;
     455             : 
     456             :         time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
     457             :         printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
     458             :                 time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
     459             :         if (time.have_relative) {
     460             :                 printf ("%3dY %3dM %3dD / %3dH %3dM %3dS", 
     461             :                         time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
     462             :         }
     463             :         if (time.have_weekday_relative) {
     464             :                 printf (" / %d", time.relative.weekday);
     465             :         }
     466             :         res = time2unixtime(&time);
     467             :         printf("%Ld\n", res);
     468             : 
     469             :         return 0;
     470             : }
     471             : #endif

Generated by: LCOV version 1.10

Generated at Tue, 08 Apr 2014 11:59:35 +0000 (9 days ago)

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