1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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 272374 2008-12-31 11:17:49Z sebastian $ */
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 12284 : {
35 : timelib_time *t;
36 12284 : t = calloc(1, sizeof(timelib_time));
37 :
38 12284 : return t;
39 : }
40 :
41 : void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
42 7157 : {
43 : unsigned int i;
44 :
45 7157 : TIMELIB_TIME_FREE(tm->tz_abbr);
46 7157 : tm->tz_abbr = strdup(tz_abbr);
47 28850 : for (i = 0; i < strlen(tz_abbr); i++) {
48 21693 : tm->tz_abbr[i] = toupper(tz_abbr[i]);
49 : }
50 7157 : }
51 :
52 : void timelib_time_dtor(timelib_time* t)
53 12284 : {
54 12284 : TIMELIB_TIME_FREE(t->tz_abbr);
55 12284 : TIMELIB_TIME_FREE(t);
56 12284 : }
57 :
58 : timelib_time_offset* timelib_time_offset_ctor(void)
59 24024 : {
60 : timelib_time_offset *t;
61 24024 : t = calloc(1, sizeof(timelib_time_offset));
62 :
63 24024 : return t;
64 : }
65 :
66 : void timelib_time_offset_dtor(timelib_time_offset* t)
67 24024 : {
68 24024 : TIMELIB_TIME_FREE(t->abbr);
69 24024 : TIMELIB_TIME_FREE(t);
70 24024 : }
71 :
72 : timelib_tzinfo* timelib_tzinfo_ctor(char *name)
73 673 : {
74 : timelib_tzinfo *t;
75 673 : t = calloc(1, sizeof(timelib_tzinfo));
76 673 : t->name = strdup(name);
77 :
78 673 : return t;
79 : }
80 :
81 : timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
82 0 : {
83 0 : timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
84 0 : tmp->ttisgmtcnt = tz->ttisgmtcnt;
85 0 : tmp->ttisstdcnt = tz->ttisstdcnt;
86 0 : tmp->leapcnt = tz->leapcnt;
87 0 : tmp->timecnt = tz->timecnt;
88 0 : tmp->typecnt = tz->typecnt;
89 0 : tmp->charcnt = tz->charcnt;
90 :
91 0 : tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t));
92 0 : tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
93 0 : memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t));
94 0 : memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char));
95 :
96 0 : tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
97 0 : memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo));
98 :
99 0 : tmp->timezone_abbr = (char*) malloc(tz->charcnt);
100 0 : memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt);
101 :
102 0 : tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
103 0 : memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo));
104 :
105 0 : return tmp;
106 : }
107 :
108 : void timelib_tzinfo_dtor(timelib_tzinfo *tz)
109 468 : {
110 468 : TIMELIB_TIME_FREE(tz->name);
111 468 : TIMELIB_TIME_FREE(tz->trans);
112 468 : TIMELIB_TIME_FREE(tz->trans_idx);
113 468 : TIMELIB_TIME_FREE(tz->type);
114 468 : TIMELIB_TIME_FREE(tz->timezone_abbr);
115 468 : TIMELIB_TIME_FREE(tz->leap_times);
116 468 : TIMELIB_TIME_FREE(tz);
117 468 : }
118 :
119 : char *timelib_get_tz_abbr_ptr(timelib_time *t)
120 0 : {
121 0 : if (!t->sse_uptodate) {
122 0 : timelib_update_ts(t, NULL);
123 : };
124 0 : return t->tz_abbr;
125 : }
126 :
127 : void timelib_error_container_dtor(timelib_error_container *errors)
128 3208 : {
129 : int i;
130 :
131 3249 : for (i = 0; i < errors->warning_count; i++) {
132 41 : free(errors->warning_messages[i].message);
133 : }
134 3208 : free(errors->warning_messages);
135 3363 : for (i = 0; i < errors->error_count; i++) {
136 155 : free(errors->error_messages[i].message);
137 : }
138 3208 : free(errors->error_messages);
139 3208 : free(errors);
140 3208 : }
141 :
142 : signed long timelib_date_to_int(timelib_time *d, int *error)
143 3692 : {
144 : timelib_sll ts;
145 :
146 3692 : ts = d->sse;
147 :
148 3692 : if (ts < LONG_MIN || ts > LONG_MAX) {
149 27 : if (error) {
150 27 : *error = 1;
151 : }
152 27 : return 0;
153 : }
154 3665 : if (error) {
155 3665 : *error = 0;
156 : }
157 3665 : return (signed long) d->sse;
158 : }
159 :
160 : void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec)
161 0 : {
162 0 : *hour = floor(h);
163 0 : *min = floor((h - *hour) * 60);
164 0 : *sec = (h - *hour - ((float) *min / 60)) * 3600;
165 0 : }
166 :
167 : void timelib_dump_date(timelib_time *d, int options)
168 0 : {
169 0 : if ((options & 2) == 2) {
170 0 : printf("TYPE: %d ", d->zone_type);
171 : }
172 0 : printf("TS: %lld | %s%04lld-%02lld-%02lld %02lld:%02lld:%02lld",
173 : d->sse, d->y < 0 ? "-" : "", TIMELIB_LLABS(d->y), d->m, d->d, d->h, d->i, d->s);
174 0 : if (d->f > +0.0) {
175 0 : printf(" %.5f", d->f);
176 : }
177 :
178 0 : if (d->is_localtime) {
179 0 : switch (d->zone_type) {
180 : case TIMELIB_ZONETYPE_OFFSET: /* Only offset */
181 0 : printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
182 0 : break;
183 : case TIMELIB_ZONETYPE_ID: /* Timezone struct */
184 : /* Show abbreviation if wanted */
185 0 : if (d->tz_abbr) {
186 0 : printf(" %s", d->tz_abbr);
187 : }
188 : /* Do we have a TimeZone struct? */
189 0 : if (d->tz_info) {
190 0 : printf(" %s", d->tz_info->name);
191 : }
192 0 : break;
193 : case TIMELIB_ZONETYPE_ABBR:
194 0 : printf(" %s", d->tz_abbr);
195 0 : printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
196 : break;
197 : }
198 : } else {
199 0 : printf(" GMT 00000");
200 : }
201 :
202 0 : if ((options & 1) == 1) {
203 0 : if (d->have_relative) {
204 0 : printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS",
205 : d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s);
206 : }
207 0 : if (d->have_weekday_relative) {
208 0 : printf(" / %d.%d", d->relative.weekday, d->relative.weekday_behavior);
209 : }
210 0 : if (d->have_special_relative) {
211 0 : switch (d->special.type) {
212 : case TIMELIB_SPECIAL_WEEKDAY:
213 0 : printf(" / %lld weekday", d->special.amount);
214 : break;
215 : }
216 : }
217 : }
218 0 : printf("\n");
219 0 : }
220 :
|