1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <derick@derickrethans.nl> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "timelib.h"
/* jan feb mrt apr may jun jul aug sep oct nov dec */
static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
static int month_tab[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
/* dec jan feb mrt apr may jun jul aug sep oct nov dec */
static int days_in_month_leap[13] = { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int days_in_month[13] = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
{
if (*a < start) {
*a += adj;
(*b)--;
return 1;
}
if (*a >= end) {
if (start == 0) {
(*b) += (*a / end);
(*a) -= (end * (*a / end));
return 0;
}
*a -= adj;
(*b)++;
return 1;
}
return 0;
}
static int do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d)
{
timelib_sll leapyear;
timelib_sll days_this_month;
timelib_sll next_month, next_year;
timelib_sll days_next_month;
do_range_limit(1, 13, 12, base_m, base_y);
leapyear = timelib_is_leap(*base_y);
days_this_month = leapyear ? days_in_month_leap[*base_m] : days_in_month[*base_m];
next_month = (*base_m) + 1;
if (next_month > 12) {
next_month -= 12;
next_year = (*base_y) + 1;
} else {
next_year = (*base_y);
}
leapyear = timelib_is_leap(next_year);
days_next_month = leapyear ? days_in_month_leap[next_month] : days_in_month[next_month];
if (*d < 0) {
*d += days_this_month;
(*m)--;
return 1;
}
if (*d > days_next_month) {
*d -= days_next_month;
(*m)++;
return 1;
}
return 0;
}
static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
{
timelib_sll leapyear;
timelib_sll days_this_month;
timelib_sll last_month, last_year;
timelib_sll days_last_month;
do_range_limit(1, 13, 12, m, y);
leapyear = timelib_is_leap(*y);
days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
last_month = (*m) - 1;
if (last_month < 1) {
last_month += 12;
last_year = (*y) - 1;
} else {
last_year = (*y);
}
leapyear = timelib_is_leap(last_year);
days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];
if (*d <= 0) {
*d += days_last_month;
(*m)--;
return 1;
}
if (*d > days_this_month) {
*d -= days_this_month;
(*m)++;
return 1;
}
return 0;
}
static void do_adjust_for_weekday(timelib_time* time)
{
timelib_sll current_dow, difference;
current_dow = timelib_day_of_week(time->y, time->m, time->d);
if (time->relative.weekday_behavior == 2)
{
if (time->relative.weekday == 0) {
time->relative.weekday = 7;
}
time->d -= current_dow;
time->d += time->relative.weekday;
return;
}
difference = time->relative.weekday - current_dow;
if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
difference += 7;
}
if (time->relative.weekday >= 0) {
time->d += difference;
} else {
time->d -= (7 - (abs(time->relative.weekday) - current_dow));
}
time->relative.have_weekday_relative = 0;
}
void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
{
do {} while (do_range_limit(0, 60, 60, &rt->s, &rt->i));
do {} while (do_range_limit(0, 60, 60, &rt->i, &rt->h));
do {} while (do_range_limit(0, 24, 24, &rt->h, &rt->d));
do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));
do {} while (do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d));
do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));
}
static void do_normalize(timelib_time* time)
{
do {} while (do_range_limit(0, 60, 60, &time->s, &time->i));
do {} while (do_range_limit(0, 60, 60, &time->i, &time->h));
do {} while (do_range_limit(0, 24, 24, &time->h, &time->d));
do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));
do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));
}
static void do_adjust_relative(timelib_time* time)
{
if (time->relative.have_weekday_relative) {
do_adjust_for_weekday(time);
}
do_normalize(time);
if (time->have_relative) {
time->s += time->relative.s;
time->i += time->relative.i;
time->h += time->relative.h;
time->d += time->relative.d;
time->m += time->relative.m;
time->y += time->relative.y;
}
switch (time->relative.first_last_day_of) {
case 1: // first
time->d = 1;
break;
case 2: // last
time->d = 0;
time->m++;
break;
}
do_normalize(time);
}
static void do_adjust_special_weekday(timelib_time* time)
{
timelib_sll current_dow, count;
count = time->relative.special.amount;
current_dow = timelib_day_of_week(time->y, time->m, time->d);
if (count == 0) {
// skip over saturday and sunday
if (current_dow == 6) {
time->d += 2;
}
// skip over sunday
if (current_dow == 0) {
time->d += 1;
}
} else if (count > 0) {
// skip over saturday and sunday
if (current_dow == 5) {
time->d += 2;
}
// skip over sunday
if (current_dow == 6) {
time->d += 1;
}
// add increments of 5 weekdays as a week
time->d += (count / 5) * 7;
// if current DOW plus the remainder > 5, add two days
current_dow = timelib_day_of_week(time->y, time->m, time->d);
time->d += (count % 5);
if ((count % 5) + current_dow > 5) {
time->d += 2;
}
} else if (count < 0) {
// skip over sunday and saturday
if (current_dow == 1) {
time->d -= 2;
}
// skip over satruday
if (current_dow == 0 ) {
time->d -= 1;
}
// subtract increments of 5 weekdays as a week
time->d += (count / 5) * 7;
// if current DOW minus the remainder < 0, subtract two days
current_dow = timelib_day_of_week(time->y, time->m, time->d);
time->d += (count % 5);
if ((count % 5) + current_dow < 1) {
time->d -= 2;
}
}
}
static void do_adjust_special(timelib_time* time)
{
if (time->relative.have_special_relative) {
switch (time->relative.special.type) {
case TIMELIB_SPECIAL_WEEKDAY:
do_adjust_special_weekday(time);
break;
}
}
do_normalize(time);
memset(&(time->relative.special), 0, sizeof(time->relative.special));
}
static void do_adjust_special_early(timelib_time* time)
{
if (time->relative.have_special_relative) {
switch (time->relative.special.type) {
case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
time->d = 1;
time->m += time->relative.m;
time->relative.m = 0;
break;
case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
time->d = 1;
time->m += time->relative.m + 1;
time->relative.m = 0;
break;
}
}
}
static timelib_sll do_years(timelib_sll year)
{
timelib_sll i;
timelib_sll res = 0;
timelib_sll eras;
eras = (year - 1970) / 400;
if (eras != 0) {
year = year - (eras * 400);
res += (SECS_PER_ERA * eras);
}
if (year >= 1970) {
for (i = year - 1; i >= 1970; i--) {
if (timelib_is_leap(i)) {
res += (DAYS_PER_LYEAR * SECS_PER_DAY);
} else {
res += (DAYS_PER_YEAR * SECS_PER_DAY);
}
}
} else {
for (i = 1969; i >= year; i--) {
if (timelib_is_leap(i)) {
res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
} else {
res -= (DAYS_PER_YEAR * SECS_PER_DAY);
}
}
}
return res;
}
static timelib_sll do_months(timelib_ull month, timelib_ull year)
{
if (timelib_is_leap(year)) {
return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
} else {
return ((month_tab[month - 1]) * SECS_PER_DAY);
}
}
static timelib_sll do_days(timelib_ull day)
{
return ((day - 1) * SECS_PER_DAY);
}
static timelib_sll do_time(timelib_ull hour, timelib_ull minute, timelib_ull second)
{
timelib_sll res = 0;
res += hour * 3600;
res += minute * 60;
res += second;
return res;
}
static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
{
switch (tz->zone_type) {
case TIMELIB_ZONETYPE_OFFSET:
tz->is_localtime = 1;
return tz->z * 60;
break;
case TIMELIB_ZONETYPE_ABBR: {
timelib_sll tmp;
tz->is_localtime = 1;
tmp = tz->z;
tmp -= tz->dst * 60;
tmp *= 60;
return tmp;
}
break;
case TIMELIB_ZONETYPE_ID:
tzi = tz->tz_info;
/* Break intentionally missing */
default:
/* No timezone in struct, fallback to reference if possible */
if (tzi) {
timelib_time_offset *before, *after;
timelib_sll tmp;
int in_transistion;
tz->is_localtime = 1;
before = timelib_get_time_zone_info(tz->sse, tzi);
after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
timelib_set_timezone(tz, tzi);
in_transistion = (
((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
((tz->sse - after->offset) < after->transistion_time)
);
if ((before->offset != after->offset) && !in_transistion) {
tmp = -after->offset;
} else {
tmp = -tz->z;
}
timelib_time_offset_dtor(before);
timelib_time_offset_dtor(after);
{
timelib_time_offset *gmt_offset;
gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
tz->z = gmt_offset->offset;
tz->dst = gmt_offset->is_dst;
if (tz->tz_abbr) {
free(tz->tz_abbr);
}
tz->tz_abbr = strdup(gmt_offset->abbr);
timelib_time_offset_dtor(gmt_offset);
}
return tmp;
}
}
return 0;
}
void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
{
timelib_sll res = 0;
do_adjust_special_early(time);
do_adjust_relative(time);
do_adjust_special(time);
res += do_years(time->y);
res += do_months(time->m, time->y);
res += do_days(time->d);
res += do_time(time->h, time->i, time->s);
time->sse = res;
res += do_adjust_timezone(time, tzi);
time->sse = res;
time->sse_uptodate = 1;
}
#if 0
int main(void)
{
timelib_sll res;
timelib_time time;
time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
if (time.have_relative) {
printf ("%3dY %3dM %3dD / %3dH %3dM %3dS",
time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
}
if (time.have_weekday_relative) {
printf (" / %d", time.relative.weekday);
}
res = time2unixtime(&time);
printf("%Ld\n", res);
return 0;
}
#endif
|