Back to index

php5  5.3.10
tm2unixtime.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2010 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: tm2unixtime.c 320481 2011-12-06 06:21:08Z derick $ */
00020 
00021 #include "timelib.h"
00022 
00023 /*                                    jan  feb  mrt  apr  may  jun  jul  aug  sep  oct  nov  dec */
00024 static int month_tab_leap[12]     = {  -1,  30,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
00025 static int month_tab[12]          = {   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
00026 
00027 /*                                    dec  jan  feb  mrt  apr  may  jun  jul  aug  sep  oct  nov  dec */
00028 static int days_in_month_leap[13] = {  31,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
00029 static int days_in_month[13]      = {  31,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
00030 
00031 static int do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
00032 {
00033        if (*a < start) {
00034               *b -= (start - *a - 1) / adj + 1;
00035               *a += adj * ((start - *a - 1) / adj + 1);
00036        }
00037        if (*a >= end) {
00038               *b += *a / adj;
00039               *a -= adj * (*a / adj);
00040        }
00041        return 0;
00042 }
00043 
00044 static void inc_month(timelib_sll *y, timelib_sll *m)
00045 {
00046        (*m)++;
00047        if (*m > 12) {
00048               *m -= 12;
00049               (*y)++;
00050        }
00051 }
00052 
00053 static void dec_month(timelib_sll *y, timelib_sll *m)
00054 {
00055        (*m)--;
00056        if (*m < 1) {
00057               *m += 12;
00058               (*y)--;
00059        }
00060 }
00061 
00062 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)
00063 {
00064        timelib_sll leapyear;
00065        timelib_sll month, year;
00066        timelib_sll days;
00067 
00068        do_range_limit(1, 13, 12, base_m, base_y);
00069 
00070        year = *base_y;
00071        month = *base_m;
00072 
00073 /*
00074        printf( "S: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
00075 */
00076        if (!invert) {
00077               while (*d < 0) {
00078                      dec_month(&year, &month);
00079                      leapyear = timelib_is_leap(year);
00080                      days = leapyear ? days_in_month_leap[month] : days_in_month[month];
00081 
00082                      /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
00083 
00084                      *d += days;
00085                      (*m)--;
00086               }
00087        } else {
00088               while (*d < 0) {
00089                      leapyear = timelib_is_leap(year);
00090                      days = leapyear ? days_in_month_leap[month] : days_in_month[month];
00091 
00092                      /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
00093 
00094                      *d += days;
00095                      (*m)--;
00096                      inc_month(&year, &month);
00097               }
00098        }
00099        /*
00100        printf( "E: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
00101        */
00102 }
00103 
00104 static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
00105 {
00106        timelib_sll leapyear;
00107        timelib_sll days_this_month;
00108        timelib_sll last_month, last_year;
00109        timelib_sll days_last_month;
00110        
00111        /* can jump an entire leap year period quickly */
00112        if (*d >= DAYS_PER_LYEAR_PERIOD || *d <= -DAYS_PER_LYEAR_PERIOD) {
00113               *y += YEARS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
00114               *d -= DAYS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
00115        }
00116 
00117        do_range_limit(1, 13, 12, m, y);
00118 
00119        leapyear = timelib_is_leap(*y);
00120        days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
00121        last_month = (*m) - 1;
00122 
00123        if (last_month < 1) {
00124               last_month += 12;
00125               last_year = (*y) - 1;
00126        } else {
00127               last_year = (*y);
00128        }
00129        leapyear = timelib_is_leap(last_year);
00130        days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];
00131 
00132        if (*d <= 0) {
00133               *d += days_last_month;
00134               (*m)--;
00135               return 1;
00136        }
00137        if (*d > days_this_month) {
00138               *d -= days_this_month;
00139               (*m)++;
00140               return 1;
00141        }
00142        return 0;
00143 }
00144 
00145 static void do_adjust_for_weekday(timelib_time* time)
00146 {
00147        timelib_sll current_dow, difference;
00148 
00149        current_dow = timelib_day_of_week(time->y, time->m, time->d);
00150        if (time->relative.weekday_behavior == 2)
00151        {
00152               if (time->relative.weekday == 0) {
00153                      time->relative.weekday = 7;
00154               }
00155               time->d -= current_dow;
00156               time->d += time->relative.weekday;
00157               return;
00158        }
00159        difference = time->relative.weekday - current_dow;
00160        if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
00161               difference += 7;
00162        }
00163        if (time->relative.weekday >= 0) {
00164               time->d += difference;
00165        } else {
00166               time->d -= (7 - (abs(time->relative.weekday) - current_dow));
00167        }
00168        time->relative.have_weekday_relative = 0;
00169 }
00170 
00171 void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
00172 {
00173        do {} while (do_range_limit(0, 60, 60, &rt->s, &rt->i));
00174        do {} while (do_range_limit(0, 60, 60, &rt->i, &rt->h));
00175        do {} while (do_range_limit(0, 24, 24, &rt->h, &rt->d));
00176        do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));
00177 
00178        do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
00179        do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));
00180 }
00181 
00182 void timelib_do_normalize(timelib_time* time)
00183 {
00184        if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 60, 60, &time->s, &time->i));
00185        if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 60, 60, &time->i, &time->h));
00186        if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 24, 24, &time->h, &time->d));
00187        do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));
00188 
00189        do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
00190        do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));
00191 }
00192 
00193 static void do_adjust_relative(timelib_time* time)
00194 {
00195        if (time->relative.have_weekday_relative) {
00196               do_adjust_for_weekday(time);
00197        }
00198        timelib_do_normalize(time);
00199 
00200        if (time->have_relative) {
00201               time->s += time->relative.s;
00202               time->i += time->relative.i;
00203               time->h += time->relative.h;
00204 
00205               time->d += time->relative.d;
00206               time->m += time->relative.m;
00207               time->y += time->relative.y;
00208        }
00209        switch (time->relative.first_last_day_of) {
00210               case 1: /* first */
00211                      time->d = 1;
00212                      break;
00213               case 2: /* last */
00214                      time->d = 0;
00215                      time->m++;
00216                      break;
00217        }
00218        timelib_do_normalize(time);
00219 }
00220 
00221 static void do_adjust_special_weekday(timelib_time* time)
00222 {
00223        timelib_sll current_dow, count;
00224 
00225        count = time->relative.special.amount;
00226 
00227        current_dow = timelib_day_of_week(time->y, time->m, time->d);
00228        if (count == 0) {
00229               /* skip over saturday and sunday */
00230               if (current_dow == 6) {
00231                      time->d += 2;
00232               }
00233               /* skip over sunday */
00234               if (current_dow == 0) {
00235                      time->d += 1;
00236               }
00237        } else if (count > 0) {
00238               /* skip over saturday and sunday */
00239               if (current_dow == 5) {
00240                      time->d += 2;
00241               }
00242               /* skip over sunday */
00243               if (current_dow == 6) {
00244                      time->d += 1;
00245               }
00246               /* add increments of 5 weekdays as a week */
00247               time->d += (count / 5) * 7;
00248               /* if current DOW plus the remainder > 5, add two days */
00249               current_dow = timelib_day_of_week(time->y, time->m, time->d);
00250               time->d += (count % 5);
00251               if ((count % 5) + current_dow > 5) {
00252                      time->d += 2;
00253               }
00254        } else if (count < 0) {
00255               /* skip over sunday and saturday */
00256               if (current_dow == 1) {
00257                      time->d -= 2;
00258               }
00259               /* skip over satruday */
00260               if (current_dow == 0 ) {
00261                      time->d -= 1;
00262               }
00263               /* subtract increments of 5 weekdays as a week */
00264               time->d += (count / 5) * 7;
00265               /* if current DOW minus the remainder < 0, subtract two days */
00266               current_dow = timelib_day_of_week(time->y, time->m, time->d);
00267               time->d += (count % 5);
00268               if ((count % 5) + current_dow < 1) {
00269                      time->d -= 2;
00270               }
00271        }
00272 }
00273 
00274 static void do_adjust_special(timelib_time* time)
00275 {
00276        if (time->relative.have_special_relative) {
00277               switch (time->relative.special.type) {
00278                      case TIMELIB_SPECIAL_WEEKDAY:
00279                             do_adjust_special_weekday(time);
00280                             break;
00281               }
00282        }
00283        timelib_do_normalize(time);
00284        memset(&(time->relative.special), 0, sizeof(time->relative.special));
00285 }
00286 
00287 static void do_adjust_special_early(timelib_time* time)
00288 {
00289        if (time->relative.have_special_relative) {
00290               switch (time->relative.special.type) {
00291                      case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
00292                             time->d = 1;
00293                             time->m += time->relative.m;
00294                             time->relative.m = 0;
00295                             break;
00296                      case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
00297                             time->d = 1;
00298                             time->m += time->relative.m + 1;
00299                             time->relative.m = 0;
00300                             break;
00301               }
00302        }
00303        timelib_do_normalize(time);
00304 }
00305 
00306 static timelib_sll do_years(timelib_sll year)
00307 {
00308        timelib_sll i;
00309        timelib_sll res = 0;
00310        timelib_sll eras;
00311 
00312        eras = (year - 1970) / 40000;
00313        if (eras != 0) {
00314               year = year - (eras * 40000);
00315               res += (SECS_PER_ERA * eras * 100);
00316        }
00317 
00318        if (year >= 1970) {
00319               for (i = year - 1; i >= 1970; i--) {
00320                      if (timelib_is_leap(i)) {
00321                             res += (DAYS_PER_LYEAR * SECS_PER_DAY);
00322                      } else {
00323                             res += (DAYS_PER_YEAR * SECS_PER_DAY);
00324                      }
00325               }
00326        } else {
00327               for (i = 1969; i >= year; i--) {
00328                      if (timelib_is_leap(i)) {
00329                             res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
00330                      } else {
00331                             res -= (DAYS_PER_YEAR * SECS_PER_DAY);
00332                      }
00333               }
00334        }
00335        return res;
00336 }
00337 
00338 static timelib_sll do_months(timelib_ull month, timelib_ull year)
00339 {
00340        if (timelib_is_leap(year)) {
00341               return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
00342        } else {
00343               return ((month_tab[month - 1]) * SECS_PER_DAY);
00344        }
00345 }
00346 
00347 static timelib_sll do_days(timelib_ull day)
00348 {
00349        return ((day - 1) * SECS_PER_DAY);
00350 }
00351 
00352 static timelib_sll do_time(timelib_ull hour, timelib_ull minute, timelib_ull second)
00353 {
00354        timelib_sll res = 0;
00355 
00356        res += hour * 3600;
00357        res += minute * 60;
00358        res += second;
00359        return res;
00360 }
00361 
00362 static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
00363 {
00364        switch (tz->zone_type) {
00365               case TIMELIB_ZONETYPE_OFFSET:
00366 
00367                      tz->is_localtime = 1;
00368                      return tz->z * 60;
00369                      break;
00370 
00371               case TIMELIB_ZONETYPE_ABBR: {
00372                      timelib_sll tmp;
00373 
00374                      tz->is_localtime = 1;
00375                      tmp = tz->z;
00376                      tmp -= tz->dst * 60;
00377                      tmp *= 60;
00378                      return tmp;
00379                      }
00380                      break;
00381 
00382               case TIMELIB_ZONETYPE_ID:
00383                      tzi = tz->tz_info;
00384                      /* Break intentionally missing */
00385 
00386               default:
00387                      /* No timezone in struct, fallback to reference if possible */
00388                      if (tzi) {
00389                             timelib_time_offset *before, *after;
00390                             timelib_sll          tmp;
00391                             int                  in_transistion;
00392                             
00393                             tz->is_localtime = 1;
00394                             before = timelib_get_time_zone_info(tz->sse, tzi);
00395                             after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
00396                             timelib_set_timezone(tz, tzi);
00397 
00398                             in_transistion = (
00399                                    ((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
00400                                    ((tz->sse - after->offset) < after->transistion_time)
00401                             );
00402                             
00403                             if ((before->offset != after->offset) && !in_transistion) {
00404                                    tmp = -after->offset;
00405                             } else {
00406                                    tmp = -tz->z;
00407                             }
00408                             timelib_time_offset_dtor(before);
00409                             timelib_time_offset_dtor(after);
00410 
00411                             {
00412                                    timelib_time_offset *gmt_offset;
00413 
00414                                    gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
00415                                    tz->z = gmt_offset->offset;
00416 
00417                                    tz->dst = gmt_offset->is_dst;
00418                                    if (tz->tz_abbr) {
00419                                           free(tz->tz_abbr);
00420                                    }
00421                                    tz->tz_abbr = strdup(gmt_offset->abbr);
00422                                    timelib_time_offset_dtor(gmt_offset);
00423                             }
00424                             return tmp;
00425                      }
00426        }
00427        return 0;
00428 }
00429 
00430 void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
00431 {
00432        timelib_sll res = 0;
00433 
00434        do_adjust_special_early(time);
00435        do_adjust_relative(time);
00436        do_adjust_special(time);
00437        res += do_years(time->y);
00438        res += do_months(time->m, time->y);
00439        res += do_days(time->d);
00440        res += do_time(time->h, time->i, time->s);
00441        time->sse = res;
00442 
00443        res += do_adjust_timezone(time, tzi);
00444        time->sse = res;
00445 
00446        time->sse_uptodate = 1;
00447        time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = 0;
00448 }
00449 
00450 #if 0
00451 int main(void)
00452 {
00453        timelib_sll res;
00454        timelib_time time;
00455 
00456        time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
00457        printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
00458               time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
00459        if (time.have_relative) {
00460               printf ("%3dY %3dM %3dD / %3dH %3dM %3dS", 
00461                      time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
00462        }
00463        if (time.have_weekday_relative) {
00464               printf (" / %d", time.relative.weekday);
00465        }
00466        res = time2unixtime(&time);
00467        printf("%Ld\n", res);
00468 
00469        return 0;
00470 }
00471 #endif