1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015 Derick Rethans
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "timelib.h"
26
27 #include <stdio.h>
28
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #else
36 #include <strings.h>
37 #endif
38
39 static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
40 static int month_tab[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
41
42
43 /* Converts a Unix timestamp value into broken down time, in GMT */
timelib_unixtime2gmt(timelib_time * tm,timelib_sll ts)44 void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
45 {
46 timelib_sll days, remainder, tmp_days;
47 timelib_sll cur_year = 1970;
48 timelib_sll i;
49 timelib_sll hours, minutes, seconds;
50 int *months;
51
52 days = ts / SECS_PER_DAY;
53 remainder = ts - (days * SECS_PER_DAY);
54 if (ts < 0 && remainder == 0) {
55 days++;
56 remainder -= SECS_PER_DAY;
57 }
58 TIMELIB_DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
59
60 if (ts >= 0) {
61 tmp_days = days + 1;
62
63 if (tmp_days >= DAYS_PER_LYEAR_PERIOD || tmp_days <= -DAYS_PER_LYEAR_PERIOD) {
64 cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
65 tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
66 }
67
68 while (tmp_days >= DAYS_PER_LYEAR) {
69 cur_year++;
70 if (timelib_is_leap(cur_year)) {
71 tmp_days -= DAYS_PER_LYEAR;
72 } else {
73 tmp_days -= DAYS_PER_YEAR;
74 }
75 }
76 } else {
77 tmp_days = days;
78
79 /* Guess why this might be for, it has to do with a pope ;-). It's also
80 * only valid for Great Brittain and it's colonies. It needs fixing for
81 * other locales. *sigh*, why is this crap so complex! */
82 /*
83 if (ts <= TIMELIB_LL_CONST(-6857352000)) {
84 tmp_days -= 11;
85 }
86 */
87
88 while (tmp_days <= 0) {
89 if (tmp_days < -1460970) {
90 cur_year -= 4000;
91 TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
92 tmp_days += 1460970;
93 } else {
94 cur_year--;
95 TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
96 if (timelib_is_leap(cur_year)) {
97 tmp_days += DAYS_PER_LYEAR;
98 } else {
99 tmp_days += DAYS_PER_YEAR;
100 }
101 }
102 }
103 remainder += SECS_PER_DAY;
104 }
105 TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
106
107 months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
108 if (timelib_is_leap(cur_year) && cur_year < 1970) {
109 tmp_days--;
110 }
111 i = 11;
112 while (i > 0) {
113 TIMELIB_DEBUG(printf("month=%lld (%d)\n", i, months[i]););
114 if (tmp_days > months[i]) {
115 break;
116 }
117 i--;
118 }
119 TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
120
121 /* That was the date, now we do the tiiiime */
122 hours = remainder / 3600;
123 minutes = (remainder - hours * 3600) / 60;
124 seconds = remainder % 60;
125 TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
126
127 tm->y = cur_year;
128 tm->m = i + 1;
129 tm->d = tmp_days - months[i];
130 tm->h = hours;
131 tm->i = minutes;
132 tm->s = seconds;
133 tm->z = 0;
134 tm->dst = 0;
135 tm->sse = ts;
136 tm->sse_uptodate = 1;
137 tm->tim_uptodate = 1;
138 tm->is_localtime = 0;
139 }
140
timelib_update_from_sse(timelib_time * tm)141 void timelib_update_from_sse(timelib_time *tm)
142 {
143 timelib_sll sse;
144 int z = tm->z;
145 signed int dst = tm->dst;
146
147 sse = tm->sse;
148
149 switch (tm->zone_type) {
150 case TIMELIB_ZONETYPE_ABBR:
151 case TIMELIB_ZONETYPE_OFFSET: {
152 timelib_unixtime2gmt(tm, tm->sse - (tm->z * 60) + (tm->dst * 3600));
153
154 goto cleanup;
155 }
156
157 case TIMELIB_ZONETYPE_ID: {
158 timelib_time_offset *gmt_offset;
159
160 gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
161 timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
162 timelib_time_offset_dtor(gmt_offset);
163
164 goto cleanup;
165 }
166
167 default:
168 timelib_unixtime2gmt(tm, tm->sse);
169 goto cleanup;
170 }
171 cleanup:
172 tm->sse = sse;
173 tm->is_localtime = 1;
174 tm->have_zone = 1;
175 tm->z = z;
176 tm->dst = dst;
177 }
178
timelib_unixtime2local(timelib_time * tm,timelib_sll ts)179 void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
180 {
181 timelib_time_offset *gmt_offset;
182 timelib_tzinfo *tz = tm->tz_info;
183
184 switch (tm->zone_type) {
185 case TIMELIB_ZONETYPE_ABBR:
186 case TIMELIB_ZONETYPE_OFFSET: {
187 int z = tm->z;
188 signed int dst = tm->dst;
189
190 timelib_unixtime2gmt(tm, ts - (tm->z * 60) + (tm->dst * 3600));
191
192 tm->sse = ts;
193 tm->z = z;
194 tm->dst = dst;
195 break;
196 }
197
198 case TIMELIB_ZONETYPE_ID:
199 gmt_offset = timelib_get_time_zone_info(ts, tz);
200 timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
201
202 /* we need to reset the sse here as unixtime2gmt modifies it */
203 tm->sse = ts;
204 tm->dst = gmt_offset->is_dst;
205 tm->z = gmt_offset->offset;
206 tm->tz_info = tz;
207
208 timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
209 timelib_time_offset_dtor(gmt_offset);
210 break;
211
212 default:
213 tm->is_localtime = 0;
214 tm->have_zone = 0;
215 return;
216 }
217
218 tm->is_localtime = 1;
219 tm->have_zone = 1;
220 }
221
timelib_set_timezone_from_offset(timelib_time * t,timelib_sll utc_offset)222 void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
223 {
224 if (t->tz_abbr) {
225 timelib_free(t->tz_abbr);
226 }
227 t->tz_abbr = NULL;
228
229 t->z = utc_offset;
230 t->have_zone = 1;
231 t->zone_type = TIMELIB_ZONETYPE_OFFSET;
232 t->dst = 0;
233 t->tz_info = NULL;
234 }
235
timelib_set_timezone_from_abbr(timelib_time * t,timelib_abbr_info abbr_info)236 void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)
237 {
238 if (t->tz_abbr) {
239 timelib_free(t->tz_abbr);
240 }
241 t->tz_abbr = timelib_strdup(abbr_info.abbr);
242
243 t->z = abbr_info.utc_offset;
244 t->have_zone = 1;
245 t->zone_type = TIMELIB_ZONETYPE_ABBR;
246 t->dst = abbr_info.dst;
247 t->tz_info = NULL;
248 }
249
timelib_set_timezone(timelib_time * t,timelib_tzinfo * tz)250 void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
251 {
252 timelib_time_offset *gmt_offset;
253
254 gmt_offset = timelib_get_time_zone_info(t->sse, tz);
255 t->z = gmt_offset->offset;
256 /*
257 if (t->dst != gmt_offset->is_dst) {
258 printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
259 exit(1);
260 }
261 */
262 t->dst = gmt_offset->is_dst;
263 t->tz_info = tz;
264 if (t->tz_abbr) {
265 timelib_free(t->tz_abbr);
266 }
267 t->tz_abbr = timelib_strdup(gmt_offset->abbr);
268 timelib_time_offset_dtor(gmt_offset);
269
270 t->have_zone = 1;
271 t->zone_type = TIMELIB_ZONETYPE_ID;
272 }
273
274 /* Converts the time stored in the struct to localtime if localtime = true,
275 * otherwise it converts it to gmttime. This is only done when necessary
276 * ofcourse. */
timelib_apply_localtime(timelib_time * t,unsigned int localtime)277 int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
278 {
279 if (localtime) {
280 /* Converting from GMT time to local time */
281 TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
282
283 /* Check if TZ is set */
284 if (!t->tz_info) {
285 TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
286 return -1;
287 }
288
289 timelib_unixtime2local(t, t->sse);
290 } else {
291 /* Converting from local time to GMT time */
292 TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
293
294 timelib_unixtime2gmt(t, t->sse);
295 }
296 return 0;
297 }
298
299 #if HAVE_GETTIMEOFDAY
timelib_set_fraction_from_timeval(timelib_time * t,struct timeval tp)300 void timelib_set_fraction_from_timeval(timelib_time *t, struct timeval tp)
301 {
302 t->f = (double) tp.tv_usec / 1000000;
303 }
304 #endif
305