-
Notifications
You must be signed in to change notification settings - Fork 1
/
datetime.c
executable file
·514 lines (424 loc) · 9.31 KB
/
datetime.c
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* $Id: datetime.c,v 1.4 2003/01/28 14:59:46 chuck Exp $
* Libmcal - Modular Calendar Access Library
* Copyright (C) 1999 Mark Musone and Andrew Skalski
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Contact Information:
*
* Mark Musone
* musone@chek.com
*
* Andrew Skalski
* askalski@chek.com
*
* mcal@lists.chek.com
*/
#include <time.h>
#include <string.h>
#include "datetime.h"
static const int doylookup[2][13] = {
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }};
bool
isleapyear(int year)
{
/* Leap year every 4th year, except every 100th year,
* not including every 400th year.
*/
return !(year % 4) && ((year % 100) || !(year % 400));
}
int
daysinmonth(int month, bool leap)
{
/* Validate the month. */
if (month < JANUARY || month > DECEMBER)
return -1;
/* Return 28, 29, 30, or 31 based on month/leap. */
switch (month) {
case FEBRUARY:
return leap ? 29 : 28;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
default:
return 31;
}
}
bool
datevalid(int year, int mon, int mday)
{
/* Years may be between YEAR_MIN-YEAR_MAX; months JANUARY-DECEMBER,
* and days must be validated based on year and month.
*/
if (year < YEAR_MIN || year > YEAR_MAX)
return false;
if (mon < JANUARY || mon > DECEMBER)
return false;
if (mday < 1 || mday > daysinmonth(mon, isleapyear(year)))
return false;
return true;
}
bool
timevalid(int hour, int min, int sec)
{
/* Hours must be 0-23, minutes 0-59, seconds 0-59. */
if (hour < 0 || hour > 23)
return false;
if (min < 0 || min > 59)
return false;
if (sec < 0 || sec > 59)
return false;
return true;
}
void
dt_init(datetime_t *dt)
{
/* Clear out the date and time fields, zero everything else. */
memset(dt, 0, sizeof(*dt));
dt->has_date = false;
dt->has_time = false;
}
#define LT -1
#define EQ 0
#define GT 1
#define CMP(field) if (a->field < b->field) return LT; \
if (a->field > b->field) return GT;
int
dt_compare(const datetime_t *a, const datetime_t *b)
{
/* Fields have the precedence:
* year > month > mday > hour > minute > second
* Missing field < present field
* missing field == missing field
* Returns (-1 if a<b) (0 if a==b) (1 if a>b)
*/
if (a->has_date) {
if (!b->has_date)
return GT;
CMP(year);
CMP(mon);
CMP(mday);
}
else if (b->has_date)
return LT;
if (a->has_time) {
if (!b->has_time)
return GT;
CMP(hour);
CMP(min);
CMP(sec);
}
else if (b->has_time)
return LT;
return EQ;
}
#undef LT
#undef EQ
#undef GT
#undef CMP
bool
dt_now(datetime_t *dt)
{
struct tm *tm;
time_t t;
/* Fetch the system time and break it down into GMT fields. */
if ( (t = time(NULL)) == -1 ||
(tm = gmtime(&t)) == NULL)
{
return false;
}
return dt_settm(dt, tm);
}
bool
dt_settm(datetime_t *dt, const struct tm *tm)
{
/* Validate the date and time. */
if (!datevalid(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday))
return false;
if (!timevalid(tm->tm_hour, tm->tm_min, tm->tm_sec))
return false;
/* Set the fields. */
dt->has_date = true;
dt->year = tm->tm_year + 1900;
dt->mon = tm->tm_mon + 1;
dt->mday = tm->tm_mday;
dt->has_time = true;
dt->hour = tm->tm_hour;
dt->min = tm->tm_min;
dt->sec = tm->tm_sec;
return true;
}
bool
dt_hasdate(const datetime_t *dt)
{
return dt->has_date;
}
void
dt_cleardate(datetime_t *dt)
{
dt->has_date = false;
}
bool
dt_setdate(datetime_t *dt, int year, int mon, int mday)
{
/* Validate the fields. */
if (!datevalid(year, mon, mday))
return false;
/* Set the date fields. */
dt->has_date = true;
dt->year = year;
dt->mon = mon;
dt->mday = mday;
return true;
}
bool
dt_hastime(const datetime_t *dt)
{
return dt->has_time;
}
void
dt_cleartime(datetime_t *dt)
{
dt->has_time = false;
}
bool
dt_settime(datetime_t *dt, int hour, int min, int sec)
{
/* Validate the fields. */
if (!timevalid(hour, min, sec))
return false;
/* Set the time fields. */
dt->has_time = true;
dt->hour = hour;
dt->min = min;
dt->sec = sec;
return true;
}
bool
dt_empty(const datetime_t *dt)
{
return !dt->has_date && !dt->has_time;
}
int
dt_dayofyear(const datetime_t *dt)
{
/* Check for invalidated year field. */
if (!dt->has_date)
return -1;
/* Return day of year. */
return dt->mday + doylookup[isleapyear(dt->year) ? 1 : 0][dt->mon - 1];
}
int
dt_dayofweek(const datetime_t *dt)
{
return dt_dayofepoch(dt) % 7;
}
/*
char*
dt_dayofweekstr(const datetime_t *dt)
{
char output[3];
switch (dt_dayofepoch(dt) % 7) {
case 0:
strcpy (output, "SU");
break;
case 1:
strcpy (output, "MO");
break;
case 2:
strcpy (output, "TU");
break;
case 3:
strcpy (output, "WE");
break;
case 4:
strcpy (output, "TH");
break;
case 5:
strcpy (output, "FR");
break;
case 6:
strcpy (output, "SA");
break;
}
return output;
}
*/
int
dt_dayofepoch(const datetime_t *dt)
{
int doe;
int era, cent, quad, rest;
if (!dt->has_date)
return -1;
/* break down the year into 400, 100, 4, and 1 year multiples */
rest = dt->year - 1;
quad = rest / 4; rest %= 4;
cent = quad / 25; quad %= 25;
era = cent / 4; cent %= 4;
/* set up doe */
doe = dt_dayofyear(dt);
doe += era * (400 * 365 + 97);
doe += cent * (100 * 365 + 24);
doe += quad * (4 * 365 + 1);
doe += rest * 365;
return doe;
}
bool
dt_setdoe(datetime_t *dt, int doe)
{
int leap;
int year;
int mon;
int mday;
bool dec31;
/* bounds check */
if (doe < DOE_MIN || doe > DOE_MAX)
return false;
/* make sure day 366 of leap year does not roll over */
dec31 = (dt->mon == DECEMBER && dt->mday == 31);
/* be careful with this code.. any small modification may
* introduce problems. it is highly recommended to test
* for every single date from 1-1-1 to 9999-12-31
*/
doe--;
if (dec31) doe--;
year = 400 * (doe / (400 * 365 + 97)); doe %= (400 * 365 + 97);
year += 100 * (doe / (100 * 365 + 24)); doe %= (100 * 365 + 24);
year += 4 * (doe / (4 * 365 + 1)); doe %= (4 * 365 + 1);
year += (doe / 365) + 1; doe %= 365;
if (dec31) doe++;
leap = isleapyear(year) ? 1 : 0;
mon = doe / 31;
if (doylookup[leap][mon+1] <= doe)
mon++;
mday = doe - doylookup[leap][mon] + 1;
return dt_setdate(dt, year, mon+1, mday);
}
bool
dt_roll_time(datetime_t *dt, int hour, int min, int sec)
{
int spillage;
int doe;
if (!dt->has_time) {
return false;
}
sec += dt->sec;
min += dt->min + (sec - (sec < 0 ? 59 : 0)) / 60;
hour += dt->hour + (min - (min < 0 ? 59 : 0)) / 60;
spillage = (hour - (hour < 0 ? 23 : 0)) / 24;
if ((sec %= 60) < 0) sec += 60;
if ((min %= 60) < 0) min += 60;
if ((hour %= 24) < 0) hour += 24;
if (dt->has_date) {
doe = dt_dayofepoch(dt);
spillage += doe;
if (spillage < DOE_MIN || spillage > DOE_MAX) {
return false;
}
if (!dt_setdoe(dt, spillage)) {
return false;
}
}
if (!dt_settime(dt, hour, min, sec)) {
if (dt->has_date)
dt_setdoe(dt, doe);
return false;
}
return true;
}
bool
dt_setweekof( datetime_t *dt, const datetime_t *ref,
weekday_t weekstart, weekday_t wday)
{
int doe;
int n_base;
int n_want;
doe = dt_dayofepoch(ref);
if (doe == -1)
return false;
/* monkey with the days so they are in proper week-order
* based on <weekstart> as the first day in the week
*/
if ((n_base = doe % 7) < weekstart)
n_base += 7;
if ((n_want = wday) < weekstart)
n_want += 7;
/* adjust doe by the offset */
doe += n_want - n_base;
/* bounds check */
if (doe < DOE_MIN || doe > DOE_MAX)
return false;
return dt_setdoe(dt, doe);
}
bool
dt_setnthwday(datetime_t *dt, int year, int mon, int nth, int wday)
{
weekday_t first;
datetime_t tmp = DT_INIT;
if (wday < SUNDAY || wday > SATURDAY)
return false;
if (!dt_setdate(&tmp, year, mon, 1))
return false;
first = dt_dayofweek(&tmp);
if (wday < first)
tmp.mday = 7 + wday - first;
else
tmp.mday = wday - first;
tmp.mday += 7 * nth - 6;
return dt_setdate(dt, year, mon, tmp.mday);
}
int
julian(int d, int m, int y)
{
int n1, n2;
n1 = 12 * y + m - 3;
n2 = n1/12;
return (734 * n1 + 15)/24 - 2 * n2 + n2/4 - n2/100 + n2/400 + d + 1721119;
}
int
dt_weekofyear(int d, int m, int y)
{
int n1, n2, w;
n1 = julian(d, m, y);
n2 = 7 * (n1/7) + 10;
y++;
while ((w = (n2 - julian(1, 1, y))/7) <= 0) {
y--;
}
return w;
}
int
dt_orderofmonth( const datetime_t *dt, const direction_t direction)
{
datetime_t temp_dt = DT_INIT;
int temp_int;
switch (direction) {
case DT_FORWARD:
dt_setnthwday(&temp_dt, dt->year, dt->mon, 1, dt_dayofweek(dt) );
temp_int = (dt->mday - temp_dt.mday)/7;
temp_int ++; //start count at 1, not zero
break;
case DT_BACKWARD:
// not built
return false;
}
return temp_int;
}