Back to index

php5  5.3.10
parse_tz.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: parse_tz.c 311110 2011-05-16 21:29:45Z johannes $ */
00020 
00021 #include "timelib.h"
00022 
00023 #include <stdio.h>
00024 
00025 #ifdef HAVE_LOCALE_H
00026 #include <locale.h>
00027 #endif
00028 
00029 #ifdef HAVE_STRING_H
00030 #include <string.h>
00031 #else
00032 #include <strings.h>
00033 #endif
00034 #include "timezonedb.h"
00035 
00036 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
00037 # if defined(__LITTLE_ENDIAN__)
00038 #  undef WORDS_BIGENDIAN
00039 # else 
00040 #  if defined(__BIG_ENDIAN__)
00041 #   define WORDS_BIGENDIAN
00042 #  endif
00043 # endif
00044 #endif
00045 
00046 #ifdef WORDS_BIGENDIAN
00047 #define timelib_conv_int(l) (l)
00048 #else
00049 #define timelib_conv_int(l) ((l & 0x000000ff) << 24) + ((l & 0x0000ff00) << 8) + ((l & 0x00ff0000) >> 8) + ((l & 0xff000000) >> 24)
00050 #endif
00051 
00052 static void read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
00053 {
00054        /* skip ID */
00055        *tzf += 4;
00056        
00057        /* read BC flag */
00058        tz->bc = (**tzf == '\1');
00059        *tzf += 1;
00060 
00061        /* read country code */
00062        memcpy(tz->location.country_code, *tzf, 2);
00063        tz->location.country_code[2] = '\0';
00064        *tzf += 2;
00065 
00066        /* skip read of preamble */
00067        *tzf += 13;
00068 }
00069 
00070 static void read_header(const unsigned char **tzf, timelib_tzinfo *tz)
00071 {
00072        uint32_t buffer[6];
00073 
00074        memcpy(&buffer, *tzf, sizeof(buffer));
00075        tz->ttisgmtcnt = timelib_conv_int(buffer[0]);
00076        tz->ttisstdcnt = timelib_conv_int(buffer[1]);
00077        tz->leapcnt    = timelib_conv_int(buffer[2]);
00078        tz->timecnt    = timelib_conv_int(buffer[3]);
00079        tz->typecnt    = timelib_conv_int(buffer[4]);
00080        tz->charcnt    = timelib_conv_int(buffer[5]);
00081        *tzf += sizeof(buffer);
00082 }
00083 
00084 static void read_transistions(const unsigned char **tzf, timelib_tzinfo *tz)
00085 {
00086        int32_t *buffer = NULL;
00087        uint32_t i;
00088        unsigned char *cbuffer = NULL;
00089 
00090        if (tz->timecnt) {
00091               buffer = (int32_t*) malloc(tz->timecnt * sizeof(int32_t));
00092               if (!buffer) {
00093                      return;
00094               }
00095               memcpy(buffer, *tzf, sizeof(int32_t) * tz->timecnt);
00096               *tzf += (sizeof(int32_t) * tz->timecnt);
00097               for (i = 0; i < tz->timecnt; i++) {
00098                      buffer[i] = timelib_conv_int(buffer[i]);
00099               }
00100 
00101               cbuffer = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
00102               if (!cbuffer) {
00103                      free(buffer);
00104                      return;
00105               }
00106               memcpy(cbuffer, *tzf, sizeof(unsigned char) * tz->timecnt);
00107               *tzf += sizeof(unsigned char) * tz->timecnt;
00108        }
00109        
00110        tz->trans = buffer;
00111        tz->trans_idx = cbuffer;
00112 }
00113 
00114 static void read_types(const unsigned char **tzf, timelib_tzinfo *tz)
00115 {
00116        unsigned char *buffer;
00117        int32_t *leap_buffer;
00118        unsigned int i, j;
00119 
00120        buffer = (unsigned char*) malloc(tz->typecnt * sizeof(unsigned char) * 6);
00121        if (!buffer) {
00122               return;
00123        }
00124        memcpy(buffer, *tzf, sizeof(unsigned char) * 6 * tz->typecnt);
00125        *tzf += sizeof(unsigned char) * 6 * tz->typecnt;
00126 
00127        tz->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
00128        if (!tz->type) {
00129               free(buffer);
00130               return;
00131        }
00132 
00133        for (i = 0; i < tz->typecnt; i++) {
00134               j = i * 6;
00135               tz->type[i].offset = (buffer[j] * 16777216) + (buffer[j + 1] * 65536) + (buffer[j + 2] * 256) + buffer[j + 3];
00136               tz->type[i].isdst = buffer[j + 4];
00137               tz->type[i].abbr_idx = buffer[j + 5];
00138        }
00139        free(buffer);
00140 
00141        tz->timezone_abbr = (char*) malloc(tz->charcnt);
00142        if (!tz->timezone_abbr) {
00143               return;
00144        }
00145        memcpy(tz->timezone_abbr, *tzf, sizeof(char) * tz->charcnt);
00146        *tzf += sizeof(char) * tz->charcnt;
00147 
00148        if (tz->leapcnt) {
00149               leap_buffer = (int32_t *) malloc(tz->leapcnt * 2 * sizeof(int32_t));
00150               if (!leap_buffer) {
00151                      return;
00152               }
00153               memcpy(leap_buffer, *tzf, sizeof(int32_t) * tz->leapcnt * 2);
00154               *tzf += sizeof(int32_t) * tz->leapcnt * 2;
00155 
00156               tz->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
00157               if (!tz->leap_times) {
00158                      free(leap_buffer);
00159                      return;
00160               }
00161               for (i = 0; i < tz->leapcnt; i++) {
00162                      tz->leap_times[i].trans = timelib_conv_int(leap_buffer[i * 2]);
00163                      tz->leap_times[i].offset = timelib_conv_int(leap_buffer[i * 2 + 1]);
00164               }
00165               free(leap_buffer);
00166        }
00167 
00168        if (tz->ttisstdcnt) {
00169               buffer = (unsigned char*) malloc(tz->ttisstdcnt * sizeof(unsigned char));
00170               if (!buffer) {
00171                      return;
00172               }
00173               memcpy(buffer, *tzf, sizeof(unsigned char) * tz->ttisstdcnt);
00174               *tzf += sizeof(unsigned char) * tz->ttisstdcnt;
00175 
00176               for (i = 0; i < tz->ttisstdcnt; i++) {
00177                      tz->type[i].isstdcnt = buffer[i];
00178               }
00179               free(buffer);
00180        }
00181 
00182        if (tz->ttisgmtcnt) {
00183               buffer = (unsigned char*) malloc(tz->ttisgmtcnt * sizeof(unsigned char));
00184               if (!buffer) {
00185                      return;
00186               }
00187               memcpy(buffer, *tzf, sizeof(unsigned char) * tz->ttisgmtcnt);
00188               *tzf += sizeof(unsigned char) * tz->ttisgmtcnt;
00189 
00190               for (i = 0; i < tz->ttisgmtcnt; i++) {
00191                      tz->type[i].isgmtcnt = buffer[i];
00192               }
00193               free(buffer);
00194        }
00195 }
00196 
00197 static void read_location(const unsigned char **tzf, timelib_tzinfo *tz)
00198 {
00199        uint32_t buffer[3];
00200        uint32_t comments_len;
00201 
00202        memcpy(&buffer, *tzf, sizeof(buffer));
00203        tz->location.latitude = timelib_conv_int(buffer[0]);
00204        tz->location.latitude = (tz->location.latitude / 100000) - 90;
00205        tz->location.longitude = timelib_conv_int(buffer[1]);
00206        tz->location.longitude = (tz->location.longitude / 100000) - 180;
00207        comments_len = timelib_conv_int(buffer[2]);
00208        *tzf += sizeof(buffer);
00209 
00210        tz->location.comments = malloc(comments_len + 1);
00211        memcpy(tz->location.comments, *tzf, comments_len);
00212        tz->location.comments[comments_len] = '\0';
00213        *tzf += comments_len;
00214 }
00215 
00216 void timelib_dump_tzinfo(timelib_tzinfo *tz)
00217 {
00218        uint32_t i;
00219 
00220        printf("Country Code:      %s\n", tz->location.country_code);
00221        printf("Geo Location:      %f,%f\n", tz->location.latitude, tz->location.longitude);
00222        printf("Comments:\n%s\n",          tz->location.comments);
00223        printf("BC:                %s\n",  tz->bc ? "" : "yes");
00224        printf("UTC/Local count:   %lu\n", (unsigned long) tz->ttisgmtcnt);
00225        printf("Std/Wall count:    %lu\n", (unsigned long) tz->ttisstdcnt);
00226        printf("Leap.sec. count:   %lu\n", (unsigned long) tz->leapcnt);
00227        printf("Trans. count:      %lu\n", (unsigned long) tz->timecnt);
00228        printf("Local types count: %lu\n", (unsigned long) tz->typecnt);
00229        printf("Zone Abbr. count:  %lu\n", (unsigned long) tz->charcnt);
00230 
00231        printf ("%8s (%12s) = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
00232               "", "", 0,
00233               (long int) tz->type[0].offset,
00234               tz->type[0].isdst,
00235               tz->type[0].abbr_idx,
00236               &tz->timezone_abbr[tz->type[0].abbr_idx],
00237               tz->type[0].isstdcnt,
00238               tz->type[0].isgmtcnt
00239               );
00240        for (i = 0; i < tz->timecnt; i++) {
00241               printf ("%08X (%12d) = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
00242                      tz->trans[i], tz->trans[i], tz->trans_idx[i],
00243                      (long int) tz->type[tz->trans_idx[i]].offset,
00244                      tz->type[tz->trans_idx[i]].isdst,
00245                      tz->type[tz->trans_idx[i]].abbr_idx,
00246                      &tz->timezone_abbr[tz->type[tz->trans_idx[i]].abbr_idx],
00247                      tz->type[tz->trans_idx[i]].isstdcnt,
00248                      tz->type[tz->trans_idx[i]].isgmtcnt
00249                      );
00250        }
00251        for (i = 0; i < tz->leapcnt; i++) {
00252               printf ("%08X (%12ld) = %d\n",
00253                      tz->leap_times[i].trans,
00254                      (long) tz->leap_times[i].trans,
00255                      tz->leap_times[i].offset);
00256        }
00257 }
00258 
00259 static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
00260 {
00261        int left = 0, right = tzdb->index_size - 1;
00262 #ifdef HAVE_SETLOCALE
00263        char *cur_locale = NULL, *tmp;
00264 
00265        tmp = setlocale(LC_CTYPE, NULL);
00266        if (tmp) {
00267               cur_locale = strdup(tmp);
00268        }
00269        setlocale(LC_CTYPE, "C");
00270 #endif 
00271 
00272        do {
00273               int mid = ((unsigned)left + right) >> 1;
00274               int cmp = strcasecmp(timezone, tzdb->index[mid].id);
00275 
00276               if (cmp < 0) {
00277                      right = mid - 1;
00278               } else if (cmp > 0) {
00279                      left = mid + 1;
00280               } else { /* (cmp == 0) */
00281                      (*tzf) = &(tzdb->data[tzdb->index[mid].pos]);
00282 #ifdef HAVE_SETLOCALE
00283                      setlocale(LC_CTYPE, cur_locale);
00284                      if (cur_locale) free(cur_locale);
00285 #endif 
00286                      return 1;
00287               }
00288 
00289        } while (left <= right);
00290 
00291 #ifdef HAVE_SETLOCALE
00292        setlocale(LC_CTYPE, cur_locale);
00293        if (cur_locale) free(cur_locale);
00294 #endif 
00295        return 0;
00296 }
00297 
00298 const timelib_tzdb *timelib_builtin_db(void)
00299 {
00300        return &timezonedb_builtin;
00301 }
00302 
00303 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
00304 {
00305        *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
00306        return timezonedb_idx_builtin;
00307 }
00308 
00309 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
00310 {
00311        const unsigned char *tzf;
00312        return (seek_to_tz_position(&tzf, timezone, tzdb));
00313 }
00314 
00315 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
00316 {
00317        const unsigned char *tzf;
00318        timelib_tzinfo *tmp;
00319 
00320        if (seek_to_tz_position(&tzf, timezone, tzdb)) {
00321               tmp = timelib_tzinfo_ctor(timezone);
00322 
00323               read_preamble(&tzf, tmp);
00324               read_header(&tzf, tmp);
00325               read_transistions(&tzf, tmp);
00326               read_types(&tzf, tmp);
00327               read_location(&tzf, tmp);
00328        } else {
00329               tmp = NULL;
00330        }
00331 
00332        return tmp;
00333 }
00334 
00335 static ttinfo* fetch_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time)
00336 {
00337        uint32_t i;
00338 
00339        /* If there is no transistion time, we pick the first one, if that doesn't
00340         * exist we return NULL */
00341        if (!tz->timecnt || !tz->trans) {
00342               *transition_time = 0;
00343               if (tz->typecnt == 1) {
00344                      return &(tz->type[0]);
00345               }
00346               return NULL;
00347        }
00348 
00349        /* If the TS is lower than the first transistion time, then we scan over
00350         * all the transistion times to find the first non-DST one, or the first
00351         * one in case there are only DST entries. Not sure which smartass came up
00352         * with this idea in the first though :) */
00353        if (ts < tz->trans[0]) {
00354               uint32_t j;
00355 
00356               *transition_time = 0;
00357               j = 0;
00358               while (j < tz->timecnt && tz->type[j].isdst) {
00359                      ++j;
00360               }
00361               if (j == tz->timecnt) {
00362                      j = 0;
00363               }
00364               return &(tz->type[j]);
00365        }
00366 
00367        /* In all other cases we loop through the available transtion times to find
00368         * the correct entry */
00369        for (i = 0; i < tz->timecnt; i++) {
00370               if (ts < tz->trans[i]) {
00371                      *transition_time = tz->trans[i - 1];
00372                      return &(tz->type[tz->trans_idx[i - 1]]);
00373               }
00374        }
00375        *transition_time = tz->trans[tz->timecnt - 1];
00376        return &(tz->type[tz->trans_idx[tz->timecnt - 1]]);
00377 }
00378 
00379 static tlinfo* fetch_leaptime_offset(timelib_tzinfo *tz, timelib_sll ts)
00380 {
00381        int i;
00382 
00383        if (!tz->leapcnt || !tz->leap_times) {
00384               return NULL;
00385        }
00386 
00387        for (i = tz->leapcnt - 1; i > 0; i--) {
00388               if (ts > tz->leap_times[i].trans) {
00389                      return &(tz->leap_times[i]);
00390               }
00391        }
00392        return NULL;
00393 }
00394 
00395 int timelib_timestamp_is_in_dst(timelib_sll ts, timelib_tzinfo *tz)
00396 {
00397        ttinfo *to;
00398        timelib_sll dummy;
00399        
00400        if ((to = fetch_timezone_offset(tz, ts, &dummy))) {
00401               return to->isdst;
00402        }
00403        return -1;
00404 }
00405 
00406 timelib_time_offset *timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz)
00407 {
00408        ttinfo *to;
00409        tlinfo *tl;
00410        int32_t offset = 0, leap_secs = 0;
00411        char *abbr;
00412        timelib_time_offset *tmp = timelib_time_offset_ctor();
00413        timelib_sll                transistion_time;
00414 
00415        if ((to = fetch_timezone_offset(tz, ts, &transistion_time))) {
00416               offset = to->offset;
00417               abbr = &(tz->timezone_abbr[to->abbr_idx]);
00418               tmp->is_dst = to->isdst;
00419               tmp->transistion_time = transistion_time;
00420        } else {
00421               offset = 0;
00422               abbr = tz->timezone_abbr;
00423               tmp->is_dst = 0;
00424               tmp->transistion_time = 0;
00425        }
00426 
00427        if ((tl = fetch_leaptime_offset(tz, ts))) {
00428               leap_secs = -tl->offset;
00429        }
00430 
00431        tmp->offset = offset;
00432        tmp->leap_secs = leap_secs;
00433        tmp->abbr = abbr ? strdup(abbr) : strdup("GMT");
00434 
00435        return tmp;
00436 }
00437 
00438 timelib_sll timelib_get_current_offset(timelib_time *t)
00439 {
00440        timelib_time_offset *gmt_offset;
00441        timelib_sll retval;
00442                      
00443        switch (t->zone_type) {
00444               case TIMELIB_ZONETYPE_ABBR:
00445               case TIMELIB_ZONETYPE_OFFSET:
00446                      return (t->z + t->dst) * -60;
00447                      
00448               case TIMELIB_ZONETYPE_ID:
00449                      gmt_offset = timelib_get_time_zone_info(t->sse, t->tz_info);
00450                      retval = gmt_offset->offset;
00451                      timelib_time_offset_dtor(gmt_offset);
00452                      return retval;
00453 
00454               default:
00455                      return 0;
00456        }
00457 }