Annotation of embedaddon/php/ext/date/lib/timelib.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:    +----------------------------------------------------------------------+
                      3:    | PHP Version 5                                                        |
                      4:    +----------------------------------------------------------------------+
                      5:    | Copyright (c) 1997-2010 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: timelib.c 305315 2010-11-13 14:57:47Z derick $ */
                     20: 
                     21: #include "timelib.h"
                     22: #include <ctype.h>
                     23: #include <math.h>
                     24: 
                     25: #define TIMELIB_TIME_FREE(m)   \
                     26:        if (m) {                \
                     27:                free(m);        \
                     28:                m = NULL;       \
                     29:        }                       \
                     30: 
                     31: #define TIMELIB_LLABS(y) (y < 0 ? (y * -1) : y)
                     32: 
                     33: timelib_time* timelib_time_ctor(void)
                     34: {
                     35:        timelib_time *t;
                     36:        t = calloc(1, sizeof(timelib_time));
                     37: 
                     38:        return t;
                     39: }
                     40: 
                     41: timelib_rel_time* timelib_rel_time_ctor(void)
                     42: {
                     43:        timelib_rel_time *t;
                     44:        t = calloc(1, sizeof(timelib_rel_time));
                     45: 
                     46:        return t;
                     47: }
                     48: 
                     49: timelib_time* timelib_time_clone(timelib_time *orig)
                     50: {
                     51:        timelib_time *tmp = timelib_time_ctor();
                     52:        memcpy(tmp, orig, sizeof(timelib_time));
                     53:        if (orig->tz_abbr) {
                     54:                tmp->tz_abbr = strdup(orig->tz_abbr);
                     55:        }
                     56:        if (orig->tz_info) {
                     57:                tmp->tz_info = orig->tz_info;
                     58:        }
                     59:        return tmp;
                     60: }
                     61: 
                     62: timelib_rel_time* timelib_rel_time_clone(timelib_rel_time *rel)
                     63: {
                     64:        timelib_rel_time *tmp = timelib_rel_time_ctor();
                     65:        memcpy(tmp, rel, sizeof(timelib_rel_time));
                     66:        return tmp;
                     67: }
                     68: 
                     69: void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
                     70: {
                     71:        unsigned int i;
                     72:        
                     73:        TIMELIB_TIME_FREE(tm->tz_abbr);
                     74:        tm->tz_abbr = strdup(tz_abbr);
                     75:        for (i = 0; i < strlen(tz_abbr); i++) {
                     76:                tm->tz_abbr[i] = toupper(tz_abbr[i]);
                     77:        }
                     78: }
                     79: 
                     80: void timelib_time_dtor(timelib_time* t)
                     81: {
                     82:        TIMELIB_TIME_FREE(t->tz_abbr);
                     83:        TIMELIB_TIME_FREE(t);
                     84: }
                     85: 
                     86: void timelib_rel_time_dtor(timelib_rel_time* t)
                     87: {
                     88:        TIMELIB_TIME_FREE(t);
                     89: }
                     90: 
                     91: timelib_time_offset* timelib_time_offset_ctor(void)
                     92: {
                     93:        timelib_time_offset *t;
                     94:        t = calloc(1, sizeof(timelib_time_offset));
                     95: 
                     96:        return t;
                     97: }
                     98: 
                     99: void timelib_time_offset_dtor(timelib_time_offset* t)
                    100: {
                    101:        TIMELIB_TIME_FREE(t->abbr);
                    102:        TIMELIB_TIME_FREE(t);
                    103: }
                    104: 
                    105: timelib_tzinfo* timelib_tzinfo_ctor(char *name)
                    106: {
                    107:        timelib_tzinfo *t;
                    108:        t = calloc(1, sizeof(timelib_tzinfo));
                    109:        t->name = strdup(name);
                    110: 
                    111:        return t;
                    112: }
                    113: 
                    114: timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
                    115: {
                    116:        timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
                    117:        tmp->ttisgmtcnt = tz->ttisgmtcnt;
                    118:        tmp->ttisstdcnt = tz->ttisstdcnt;
                    119:        tmp->leapcnt = tz->leapcnt;
                    120:        tmp->timecnt = tz->timecnt;
                    121:        tmp->typecnt = tz->typecnt;
                    122:        tmp->charcnt = tz->charcnt;
                    123:        
                    124:        tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t));
                    125:        tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
                    126:        memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t));
                    127:        memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char));
                    128: 
                    129:        tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
                    130:        memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo));
                    131: 
                    132:        tmp->timezone_abbr = (char*) malloc(tz->charcnt);
                    133:        memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt);
                    134: 
                    135:        tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
                    136:        memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo));
                    137: 
                    138:        return tmp;
                    139: }
                    140: 
                    141: void timelib_tzinfo_dtor(timelib_tzinfo *tz)
                    142: {
                    143:        TIMELIB_TIME_FREE(tz->name);
                    144:        TIMELIB_TIME_FREE(tz->trans);
                    145:        TIMELIB_TIME_FREE(tz->trans_idx);
                    146:        TIMELIB_TIME_FREE(tz->type);
                    147:        TIMELIB_TIME_FREE(tz->timezone_abbr);
                    148:        TIMELIB_TIME_FREE(tz->leap_times);
                    149:        TIMELIB_TIME_FREE(tz->location.comments);
                    150:        TIMELIB_TIME_FREE(tz);
                    151:        tz = NULL;
                    152: }
                    153: 
                    154: char *timelib_get_tz_abbr_ptr(timelib_time *t)
                    155: {
                    156:        if (!t->sse_uptodate) {
                    157:                timelib_update_ts(t, NULL);
                    158:        };
                    159:        return t->tz_abbr;
                    160: }
                    161: 
                    162: void timelib_error_container_dtor(timelib_error_container *errors)
                    163: {
                    164:        int i;
                    165: 
                    166:        for (i = 0; i < errors->warning_count; i++) {
                    167:                free(errors->warning_messages[i].message);
                    168:        }
                    169:        free(errors->warning_messages);
                    170:        for (i = 0; i < errors->error_count; i++) {
                    171:                free(errors->error_messages[i].message);
                    172:        }
                    173:        free(errors->error_messages);
                    174:        free(errors);
                    175: }
                    176: 
                    177: signed long timelib_date_to_int(timelib_time *d, int *error)
                    178: {
                    179:        timelib_sll ts;
                    180: 
                    181:        ts = d->sse;
                    182: 
                    183:        if (ts < LONG_MIN || ts > LONG_MAX) {
                    184:                if (error) {
                    185:                        *error = 1;
                    186:                }
                    187:                return 0;
                    188:        }
                    189:        if (error) {
                    190:                *error = 0;
                    191:        }
                    192:        return (signed long) d->sse;
                    193: }
                    194: 
                    195: void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec)
                    196: {
                    197:        *hour = floor(h);
                    198:        *min =  floor((h - *hour) * 60);
                    199:        *sec =  (h - *hour - ((float) *min / 60)) * 3600;
                    200: }
                    201: 
                    202: void timelib_dump_date(timelib_time *d, int options)
                    203: {
                    204:        if ((options & 2) == 2) {
                    205:                printf("TYPE: %d ", d->zone_type);
                    206:        }
                    207:        printf("TS: %lld | %s%04lld-%02lld-%02lld %02lld:%02lld:%02lld",
                    208:                d->sse, d->y < 0 ? "-" : "", TIMELIB_LLABS(d->y), d->m, d->d, d->h, d->i, d->s);
                    209:        if (d->f > +0.0) {
                    210:                printf(" %.5f", d->f);
                    211:        }
                    212: 
                    213:        if (d->is_localtime) {
                    214:                switch (d->zone_type) {
                    215:                        case TIMELIB_ZONETYPE_OFFSET: /* Only offset */
                    216:                                printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
                    217:                                break;
                    218:                        case TIMELIB_ZONETYPE_ID: /* Timezone struct */
                    219:                                /* Show abbreviation if wanted */
                    220:                                if (d->tz_abbr) {
                    221:                                        printf(" %s", d->tz_abbr);
                    222:                                }
                    223:                                /* Do we have a TimeZone struct? */
                    224:                                if (d->tz_info) {
                    225:                                        printf(" %s", d->tz_info->name);
                    226:                                }
                    227:                                break;
                    228:                        case TIMELIB_ZONETYPE_ABBR:
                    229:                                printf(" %s", d->tz_abbr);
                    230:                                printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
                    231:                                break;
                    232:                }
                    233:        }
                    234: 
                    235:        if ((options & 1) == 1) {
                    236:                if (d->have_relative) {
                    237:                        printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS", 
                    238:                                d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s);
                    239:                        if (d->relative.first_last_day_of != 0) {
                    240:                                switch (d->relative.first_last_day_of) {
                    241:                                        case 1:
                    242:                                                printf(" / first day of");
                    243:                                                break;
                    244:                                        case 2:
                    245:                                                printf(" / last day of");
                    246:                                                break;
                    247:                                }
                    248:                        }
                    249:                        if (d->relative.have_weekday_relative) {
                    250:                                printf(" / %d.%d", d->relative.weekday, d->relative.weekday_behavior);
                    251:                        }
                    252:                        if (d->relative.have_special_relative) {
                    253:                                switch (d->relative.special.type) {
                    254:                                        case TIMELIB_SPECIAL_WEEKDAY:
                    255:                                                printf(" / %lld weekday", d->relative.special.amount);
                    256:                                                break;
                    257:                                        case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
                    258:                                                printf(" / x y of z month");
                    259:                                                break;
                    260:                                        case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
                    261:                                                printf(" / last y of z month");
                    262:                                                break;
                    263:                                }
                    264:                        }
                    265:                }
                    266:        }
                    267:        printf("\n");
                    268: }
                    269: 
                    270: void timelib_dump_rel_time(timelib_rel_time *d)
                    271: {
                    272:        printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS (days: %lld)%s", 
                    273:                d->y, d->m, d->d, d->h, d->i, d->s, d->days, d->invert ? " inverted" : "");
                    274:        if (d->first_last_day_of != 0) {
                    275:                switch (d->first_last_day_of) {
                    276:                        case 1:
                    277:                                printf(" / first day of");
                    278:                                break;
                    279:                        case 2:
                    280:                                printf(" / last day of");
                    281:                                break;
                    282:                }
                    283:        }
                    284:        printf("\n");
                    285: }
                    286: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>