Back to index

php5  5.3.10
timelib.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: timelib.c 305315 2010-11-13 14:57:47Z derick $ */
00020 
00021 #include "timelib.h"
00022 #include <ctype.h>
00023 #include <math.h>
00024 
00025 #define TIMELIB_TIME_FREE(m)       \
00026        if (m) {             \
00027               free(m);      \
00028               m = NULL;     \
00029        }                    \
00030 
00031 #define TIMELIB_LLABS(y) (y < 0 ? (y * -1) : y)
00032 
00033 timelib_time* timelib_time_ctor(void)
00034 {
00035        timelib_time *t;
00036        t = calloc(1, sizeof(timelib_time));
00037 
00038        return t;
00039 }
00040 
00041 timelib_rel_time* timelib_rel_time_ctor(void)
00042 {
00043        timelib_rel_time *t;
00044        t = calloc(1, sizeof(timelib_rel_time));
00045 
00046        return t;
00047 }
00048 
00049 timelib_time* timelib_time_clone(timelib_time *orig)
00050 {
00051        timelib_time *tmp = timelib_time_ctor();
00052        memcpy(tmp, orig, sizeof(timelib_time));
00053        if (orig->tz_abbr) {
00054               tmp->tz_abbr = strdup(orig->tz_abbr);
00055        }
00056        if (orig->tz_info) {
00057               tmp->tz_info = orig->tz_info;
00058        }
00059        return tmp;
00060 }
00061 
00062 timelib_rel_time* timelib_rel_time_clone(timelib_rel_time *rel)
00063 {
00064        timelib_rel_time *tmp = timelib_rel_time_ctor();
00065        memcpy(tmp, rel, sizeof(timelib_rel_time));
00066        return tmp;
00067 }
00068 
00069 void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
00070 {
00071        unsigned int i;
00072        
00073        TIMELIB_TIME_FREE(tm->tz_abbr);
00074        tm->tz_abbr = strdup(tz_abbr);
00075        for (i = 0; i < strlen(tz_abbr); i++) {
00076               tm->tz_abbr[i] = toupper(tz_abbr[i]);
00077        }
00078 }
00079 
00080 void timelib_time_dtor(timelib_time* t)
00081 {
00082        TIMELIB_TIME_FREE(t->tz_abbr);
00083        TIMELIB_TIME_FREE(t);
00084 }
00085 
00086 void timelib_rel_time_dtor(timelib_rel_time* t)
00087 {
00088        TIMELIB_TIME_FREE(t);
00089 }
00090 
00091 timelib_time_offset* timelib_time_offset_ctor(void)
00092 {
00093        timelib_time_offset *t;
00094        t = calloc(1, sizeof(timelib_time_offset));
00095 
00096        return t;
00097 }
00098 
00099 void timelib_time_offset_dtor(timelib_time_offset* t)
00100 {
00101        TIMELIB_TIME_FREE(t->abbr);
00102        TIMELIB_TIME_FREE(t);
00103 }
00104 
00105 timelib_tzinfo* timelib_tzinfo_ctor(char *name)
00106 {
00107        timelib_tzinfo *t;
00108        t = calloc(1, sizeof(timelib_tzinfo));
00109        t->name = strdup(name);
00110 
00111        return t;
00112 }
00113 
00114 timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
00115 {
00116        timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
00117        tmp->ttisgmtcnt = tz->ttisgmtcnt;
00118        tmp->ttisstdcnt = tz->ttisstdcnt;
00119        tmp->leapcnt = tz->leapcnt;
00120        tmp->timecnt = tz->timecnt;
00121        tmp->typecnt = tz->typecnt;
00122        tmp->charcnt = tz->charcnt;
00123        
00124        tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t));
00125        tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
00126        memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t));
00127        memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char));
00128 
00129        tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
00130        memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo));
00131 
00132        tmp->timezone_abbr = (char*) malloc(tz->charcnt);
00133        memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt);
00134 
00135        tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
00136        memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo));
00137 
00138        return tmp;
00139 }
00140 
00141 void timelib_tzinfo_dtor(timelib_tzinfo *tz)
00142 {
00143        TIMELIB_TIME_FREE(tz->name);
00144        TIMELIB_TIME_FREE(tz->trans);
00145        TIMELIB_TIME_FREE(tz->trans_idx);
00146        TIMELIB_TIME_FREE(tz->type);
00147        TIMELIB_TIME_FREE(tz->timezone_abbr);
00148        TIMELIB_TIME_FREE(tz->leap_times);
00149        TIMELIB_TIME_FREE(tz->location.comments);
00150        TIMELIB_TIME_FREE(tz);
00151        tz = NULL;
00152 }
00153 
00154 char *timelib_get_tz_abbr_ptr(timelib_time *t)
00155 {
00156        if (!t->sse_uptodate) {
00157               timelib_update_ts(t, NULL);
00158        };
00159        return t->tz_abbr;
00160 }
00161 
00162 void timelib_error_container_dtor(timelib_error_container *errors)
00163 {
00164        int i;
00165 
00166        for (i = 0; i < errors->warning_count; i++) {
00167               free(errors->warning_messages[i].message);
00168        }
00169        free(errors->warning_messages);
00170        for (i = 0; i < errors->error_count; i++) {
00171               free(errors->error_messages[i].message);
00172        }
00173        free(errors->error_messages);
00174        free(errors);
00175 }
00176 
00177 signed long timelib_date_to_int(timelib_time *d, int *error)
00178 {
00179        timelib_sll ts;
00180 
00181        ts = d->sse;
00182 
00183        if (ts < LONG_MIN || ts > LONG_MAX) {
00184               if (error) {
00185                      *error = 1;
00186               }
00187               return 0;
00188        }
00189        if (error) {
00190               *error = 0;
00191        }
00192        return (signed long) d->sse;
00193 }
00194 
00195 void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec)
00196 {
00197        *hour = floor(h);
00198        *min =  floor((h - *hour) * 60);
00199        *sec =  (h - *hour - ((float) *min / 60)) * 3600;
00200 }
00201 
00202 void timelib_dump_date(timelib_time *d, int options)
00203 {
00204        if ((options & 2) == 2) {
00205               printf("TYPE: %d ", d->zone_type);
00206        }
00207        printf("TS: %lld | %s%04lld-%02lld-%02lld %02lld:%02lld:%02lld",
00208               d->sse, d->y < 0 ? "-" : "", TIMELIB_LLABS(d->y), d->m, d->d, d->h, d->i, d->s);
00209        if (d->f > +0.0) {
00210               printf(" %.5f", d->f);
00211        }
00212 
00213        if (d->is_localtime) {
00214               switch (d->zone_type) {
00215                      case TIMELIB_ZONETYPE_OFFSET: /* Only offset */
00216                             printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
00217                             break;
00218                      case TIMELIB_ZONETYPE_ID: /* Timezone struct */
00219                             /* Show abbreviation if wanted */
00220                             if (d->tz_abbr) {
00221                                    printf(" %s", d->tz_abbr);
00222                             }
00223                             /* Do we have a TimeZone struct? */
00224                             if (d->tz_info) {
00225                                    printf(" %s", d->tz_info->name);
00226                             }
00227                             break;
00228                      case TIMELIB_ZONETYPE_ABBR:
00229                             printf(" %s", d->tz_abbr);
00230                             printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
00231                             break;
00232               }
00233        }
00234 
00235        if ((options & 1) == 1) {
00236               if (d->have_relative) {
00237                      printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS", 
00238                             d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s);
00239                      if (d->relative.first_last_day_of != 0) {
00240                             switch (d->relative.first_last_day_of) {
00241                                    case 1:
00242                                           printf(" / first day of");
00243                                           break;
00244                                    case 2:
00245                                           printf(" / last day of");
00246                                           break;
00247                             }
00248                      }
00249                      if (d->relative.have_weekday_relative) {
00250                             printf(" / %d.%d", d->relative.weekday, d->relative.weekday_behavior);
00251                      }
00252                      if (d->relative.have_special_relative) {
00253                             switch (d->relative.special.type) {
00254                                    case TIMELIB_SPECIAL_WEEKDAY:
00255                                           printf(" / %lld weekday", d->relative.special.amount);
00256                                           break;
00257                                    case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
00258                                           printf(" / x y of z month");
00259                                           break;
00260                                    case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
00261                                           printf(" / last y of z month");
00262                                           break;
00263                             }
00264                      }
00265               }
00266        }
00267        printf("\n");
00268 }
00269 
00270 void timelib_dump_rel_time(timelib_rel_time *d)
00271 {
00272        printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS (days: %lld)%s", 
00273               d->y, d->m, d->d, d->h, d->i, d->s, d->days, d->invert ? " inverted" : "");
00274        if (d->first_last_day_of != 0) {
00275               switch (d->first_last_day_of) {
00276                      case 1:
00277                             printf(" / first day of");
00278                             break;
00279                      case 2:
00280                             printf(" / last day of");
00281                             break;
00282               }
00283        }
00284        printf("\n");
00285 }
00286