|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP Version 5 | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 1997-2004 The PHP Group | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 3.0 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_0.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 <dr@ez.no> | |
| 16 | + +----------------------------------------------------------------------+ |
| 17 | + */ |
| 18 | + |
| 19 | +/* $Id$ */ |
| 20 | + |
| 21 | +#include "timelib_structs.h" |
| 22 | +#include "datetime.h" |
| 23 | +#include <ctype.h> |
| 24 | + |
| 25 | +timelib_time* timelib_time_ctor() |
| 26 | +{ |
| 27 | + timelib_time *t; |
| 28 | + t = calloc(1, sizeof(timelib_time)); |
| 29 | + |
| 30 | + return t; |
| 31 | +} |
| 32 | + |
| 33 | +void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr) |
| 34 | +{ |
| 35 | + int i; |
| 36 | + |
| 37 | + if (tm->tz_abbr) { |
| 38 | + free(tm->tz_abbr); |
| 39 | + } |
| 40 | + tm->tz_abbr = strdup(tz_abbr); |
| 41 | + for (i = 0; i < strlen(tz_abbr); i++) { |
| 42 | + tm->tz_abbr[i] = toupper(tz_abbr[i]); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void timelib_time_dtor(timelib_time* t) |
| 47 | +{ |
| 48 | + if (t->tz_abbr) { |
| 49 | + free(t->tz_abbr); |
| 50 | + } |
| 51 | + free(t); |
| 52 | +} |
| 53 | + |
| 54 | +timelib_time_offset* timelib_time_offset_ctor() |
| 55 | +{ |
| 56 | + timelib_time_offset *t; |
| 57 | + t = calloc(1, sizeof(timelib_time_offset)); |
| 58 | + |
| 59 | + return t; |
| 60 | +} |
| 61 | + |
| 62 | +void timelib_time_offset_dtor(timelib_time_offset* t) |
| 63 | +{ |
| 64 | + if (t->abbr) { |
| 65 | + free(t->abbr); |
| 66 | + } |
| 67 | + free(t); |
| 68 | +} |
| 69 | + |
| 70 | +timelib_tzinfo* timelib_tzinfo_ctor(char *name) |
| 71 | +{ |
| 72 | + timelib_tzinfo *t; |
| 73 | + t = calloc(1, sizeof(timelib_tzinfo)); |
| 74 | + t->name = strdup(name); |
| 75 | + |
| 76 | + return t; |
| 77 | +} |
| 78 | + |
| 79 | +timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz) |
| 80 | +{ |
| 81 | + timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name); |
| 82 | + tmp->ttisgmtcnt = tz->ttisgmtcnt; |
| 83 | + tmp->ttisstdcnt = tz->ttisstdcnt; |
| 84 | + tmp->leapcnt = tz->leapcnt; |
| 85 | + tmp->timecnt = tz->timecnt; |
| 86 | + tmp->typecnt = tz->typecnt; |
| 87 | + tmp->charcnt = tz->charcnt; |
| 88 | + |
| 89 | + tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t)); |
| 90 | + tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char)); |
| 91 | + memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t)); |
| 92 | + memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char)); |
| 93 | + |
| 94 | + tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo)); |
| 95 | + memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo)); |
| 96 | + |
| 97 | + tmp->timezone_abbr = (char*) malloc(tz->charcnt); |
| 98 | + memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt); |
| 99 | + |
| 100 | + tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo)); |
| 101 | + memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo)); |
| 102 | + |
| 103 | + return tmp; |
| 104 | +} |
| 105 | + |
| 106 | +void timelib_tzinfo_dtor(timelib_tzinfo *tz) |
| 107 | +{ |
| 108 | + free(tz->name); |
| 109 | + free(tz->trans); |
| 110 | + free(tz->trans_idx); |
| 111 | + free(tz->type); |
| 112 | + free(tz->timezone_abbr); |
| 113 | + free(tz->leap_times); |
| 114 | + free(tz); |
| 115 | +} |
| 116 | + |
| 117 | +char *timelib_get_tz_abbr_ptr(timelib_time *t) |
| 118 | +{ |
| 119 | + if (!t->sse_uptodate) { |
| 120 | + timelib_update_ts(t, NULL); |
| 121 | + }; |
| 122 | + return t->tz_abbr; |
| 123 | +} |
| 124 | + |
| 125 | +signed long timelib_date_to_int(timelib_time *d, int *error) |
| 126 | +{ |
| 127 | + timelib_sll ts; |
| 128 | + |
| 129 | + ts = d->sse; |
| 130 | + |
| 131 | + if (ts < LONG_MIN || ts > LONG_MAX) { |
| 132 | + if (error) { |
| 133 | + *error = 1; |
| 134 | + } |
| 135 | + return 0; |
| 136 | + } |
| 137 | + if (error) { |
| 138 | + *error = 0; |
| 139 | + } |
| 140 | + return d->sse; |
| 141 | +} |
| 142 | + |
| 143 | +void timelib_dump_date(timelib_time *d, int options) |
| 144 | +{ |
| 145 | + if ((options & 2) == 2) { |
| 146 | + printf("TYPE: %d ", d->zone_type); |
| 147 | + } |
| 148 | + printf("TS: %lld | %04lld-%02lld-%02lld %02lld:%02lld:%02lld", |
| 149 | + d->sse, d->y, d->m, d->d, d->h, d->i, d->s); |
| 150 | + if (d->f > +0.0) { |
| 151 | + printf(" %.5f", d->f); |
| 152 | + } |
| 153 | + |
| 154 | + if (d->is_localtime) { |
| 155 | + switch (d->zone_type) { |
| 156 | + case TIMELIB_ZONETYPE_OFFSET: /* Only offset */ |
| 157 | + printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : ""); |
| 158 | + break; |
| 159 | + case TIMELIB_ZONETYPE_ID: /* Timezone struct */ |
| 160 | + /* Show abbreviation if wanted */ |
| 161 | + if (d->tz_abbr) { |
| 162 | + printf(" %s", d->tz_abbr); |
| 163 | + } |
| 164 | + /* Do we have a TimeZone struct? */ |
| 165 | + if (d->tz_info) { |
| 166 | + printf(" %s", d->tz_info->name); |
| 167 | + } |
| 168 | + break; |
| 169 | + case TIMELIB_ZONETYPE_ABBR: |
| 170 | + printf(" %s", d->tz_abbr); |
| 171 | + printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : ""); |
| 172 | + break; |
| 173 | + } |
| 174 | + } else { |
| 175 | + printf(" GMT 00000"); |
| 176 | + } |
| 177 | + |
| 178 | + if ((options & 1) == 1) { |
| 179 | + if (d->have_relative) { |
| 180 | + printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS", |
| 181 | + d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s); |
| 182 | + } |
| 183 | + if (d->have_weekday_relative) { |
| 184 | + printf(" / %d", d->relative.weekday); |
| 185 | + } |
| 186 | + } |
| 187 | + printf("\n"); |
| 188 | +} |
| 189 | + |
0 commit comments