]> git.ipfire.org Git - thirdparty/git.git/blame - date.c
Tweaks to make asciidoc play nice.
[thirdparty/git.git] / date.c
CommitLineData
ecee9d9e
ET
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
ecee9d9e 7#include <time.h>
3c07b1d1 8#include <sys/time.h>
ecee9d9e 9
e99d59ff
LT
10#include "cache.h"
11
ecee9d9e
ET
12static time_t my_mktime(struct tm *tm)
13{
14 static const int mdays[] = {
15 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
16 };
17 int year = tm->tm_year - 70;
18 int month = tm->tm_mon;
19 int day = tm->tm_mday;
20
21 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
22 return -1;
23 if (month < 0 || month > 11) /* array bounds */
24 return -1;
25 if (month < 2 || (year + 2) % 4)
26 day--;
27 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
28 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
29}
30
31static const char *month_names[] = {
89967023
LT
32 "January", "February", "March", "April", "May", "June",
33 "July", "August", "September", "October", "November", "December"
ecee9d9e
ET
34};
35
36static const char *weekday_names[] = {
6b7b0427 37 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
ecee9d9e
ET
38};
39
f80cd783
LT
40/*
41 * The "tz" thing is passed in as this strange "decimal parse of tz"
42 * thing, which means that tz -0100 is passed in as the integer -100,
43 * even though it means "sixty minutes off"
44 */
45const char *show_date(unsigned long time, int tz)
46{
47 struct tm *tm;
48 time_t t;
49 static char timebuf[200];
50 int minutes;
51
52 minutes = tz < 0 ? -tz : tz;
fbab835c 53 minutes = (minutes / 100)*60 + (minutes % 100);
f80cd783 54 minutes = tz < 0 ? -minutes : minutes;
fbab835c 55 t = time + minutes * 60;
f80cd783
LT
56 tm = gmtime(&t);
57 if (!tm)
58 return NULL;
59 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
60 weekday_names[tm->tm_wday],
61 month_names[tm->tm_mon],
62 tm->tm_mday,
63 tm->tm_hour, tm->tm_min, tm->tm_sec,
64 tm->tm_year + 1900, tz);
65 return timebuf;
66}
67
89967023
LT
68/*
69 * Check these. And note how it doesn't do the summer-time conversion.
70 *
71 * In my world, it's always summer, and things are probably a bit off
72 * in other ways too.
73 */
74static const struct {
75 const char *name;
76 int offset;
5e2a78a4 77 int dst;
89967023 78} timezone_names[] = {
5e2a78a4
LT
79 { "IDLW", -12, 0, }, /* International Date Line West */
80 { "NT", -11, 0, }, /* Nome */
81 { "CAT", -10, 0, }, /* Central Alaska */
82 { "HST", -10, 0, }, /* Hawaii Standard */
83 { "HDT", -10, 1, }, /* Hawaii Daylight */
84 { "YST", -9, 0, }, /* Yukon Standard */
85 { "YDT", -9, 1, }, /* Yukon Daylight */
86 { "PST", -8, 0, }, /* Pacific Standard */
87 { "PDT", -8, 1, }, /* Pacific Daylight */
88 { "MST", -7, 0, }, /* Mountain Standard */
89 { "MDT", -7, 1, }, /* Mountain Daylight */
90 { "CST", -6, 0, }, /* Central Standard */
91 { "CDT", -6, 1, }, /* Central Daylight */
92 { "EST", -5, 0, }, /* Eastern Standard */
93 { "EDT", -5, 1, }, /* Eastern Daylight */
94 { "AST", -3, 0, }, /* Atlantic Standard */
95 { "ADT", -3, 1, }, /* Atlantic Daylight */
96 { "WAT", -1, 0, }, /* West Africa */
97
98 { "GMT", 0, 0, }, /* Greenwich Mean */
99 { "UTC", 0, 0, }, /* Universal (Coordinated) */
100
101 { "WET", 0, 0, }, /* Western European */
102 { "BST", 0, 1, }, /* British Summer */
103 { "CET", +1, 0, }, /* Central European */
104 { "MET", +1, 0, }, /* Middle European */
105 { "MEWT", +1, 0, }, /* Middle European Winter */
106 { "MEST", +1, 1, }, /* Middle European Summer */
107 { "CEST", +1, 1, }, /* Central European Summer */
108 { "MESZ", +1, 1, }, /* Middle European Summer */
109 { "FWT", +1, 0, }, /* French Winter */
110 { "FST", +1, 1, }, /* French Summer */
111 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
92e2311b 112 { "EEST", +2, 1, }, /* Eastern European Daylight */
5e2a78a4
LT
113 { "WAST", +7, 0, }, /* West Australian Standard */
114 { "WADT", +7, 1, }, /* West Australian Daylight */
115 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
116 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
117 { "EAST", +10, 0, }, /* Eastern Australian Standard */
118 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
119 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
120 { "NZT", +11, 0, }, /* New Zealand */
121 { "NZST", +11, 0, }, /* New Zealand Standard */
122 { "NZDT", +11, 1, }, /* New Zealand Daylight */
123 { "IDLE", +12, 0, }, /* International Date Line East */
89967023 124};
ecee9d9e 125
89967023 126static int match_string(const char *date, const char *str)
ecee9d9e 127{
89967023
LT
128 int i = 0;
129
130 for (i = 0; *date; date++, str++, i++) {
131 if (*date == *str)
132 continue;
133 if (toupper(*date) == toupper(*str))
134 continue;
135 if (!isalnum(*date))
136 break;
137 return 0;
138 }
139 return i;
ecee9d9e
ET
140}
141
68849b54
LT
142static int skip_alpha(const char *date)
143{
144 int i = 0;
145 do {
146 i++;
147 } while (isalpha(date[i]));
148 return i;
149}
150
89967023
LT
151/*
152* Parse month, weekday, or timezone name
153*/
154static int match_alpha(const char *date, struct tm *tm, int *offset)
ecee9d9e 155{
89967023 156 int i;
ecee9d9e 157
89967023
LT
158 for (i = 0; i < 12; i++) {
159 int match = match_string(date, month_names[i]);
160 if (match >= 3) {
161 tm->tm_mon = i;
162 return match;
ecee9d9e 163 }
89967023 164 }
ecee9d9e 165
89967023
LT
166 for (i = 0; i < 7; i++) {
167 int match = match_string(date, weekday_names[i]);
168 if (match >= 3) {
169 tm->tm_wday = i;
170 return match;
171 }
172 }
ecee9d9e 173
b4f2a6ac 174 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
89967023
LT
175 int match = match_string(date, timezone_names[i].name);
176 if (match >= 3) {
5e2a78a4
LT
177 int off = timezone_names[i].offset;
178
179 /* This is bogus, but we like summer */
180 off += timezone_names[i].dst;
181
92e2311b
LT
182 /* Only use the tz name offset if we don't have anything better */
183 if (*offset == -1)
184 *offset = 60*off;
185
89967023 186 return match;
ecee9d9e
ET
187 }
188 }
ecee9d9e 189
68849b54
LT
190 if (match_string(date, "PM") == 2) {
191 if (tm->tm_hour > 0 && tm->tm_hour < 12)
192 tm->tm_hour += 12;
193 return 2;
194 }
195
89967023 196 /* BAD CRAP */
68849b54 197 return skip_alpha(date);
89967023 198}
ecee9d9e 199
198b0fb6 200static int is_date(int year, int month, int day, struct tm *tm)
89967023 201{
198b0fb6
LT
202 if (month > 0 && month < 13 && day > 0 && day < 32) {
203 if (year == -1) {
204 tm->tm_mon = month-1;
205 tm->tm_mday = day;
206 return 1;
89967023 207 }
198b0fb6
LT
208 if (year >= 1970 && year < 2100) {
209 year -= 1900;
210 } else if (year > 70 && year < 100) {
211 /* ok */
212 } else if (year < 38) {
213 year += 100;
214 } else
215 return 0;
216
217 tm->tm_mon = month-1;
218 tm->tm_mday = day;
219 tm->tm_year = year;
220 return 1;
89967023 221 }
198b0fb6
LT
222 return 0;
223}
ecee9d9e 224
26a2d8ae 225static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
198b0fb6
LT
226{
227 long num2, num3;
228
229 num2 = strtol(end+1, &end, 10);
230 num3 = -1;
231 if (*end == c && isdigit(end[1]))
232 num3 = strtol(end+1, &end, 10);
233
234 /* Time? Date? */
89967023 235 switch (c) {
198b0fb6
LT
236 case ':':
237 if (num3 < 0)
238 num3 = 0;
239 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
240 tm->tm_hour = num;
241 tm->tm_min = num2;
242 tm->tm_sec = num3;
89967023
LT
243 break;
244 }
198b0fb6 245 return 0;
89967023
LT
246
247 case '-':
248 case '/':
198b0fb6
LT
249 if (num > 70) {
250 /* yyyy-mm-dd? */
251 if (is_date(num, num2, num3, tm))
252 break;
253 /* yyyy-dd-mm? */
254 if (is_date(num, num3, num2, tm))
89967023 255 break;
198b0fb6
LT
256 }
257 /* mm/dd/yy ? */
fa0cdab5 258 if (is_date(num3, num, num2, tm))
89967023 259 break;
198b0fb6 260 /* dd/mm/yy ? */
fa0cdab5 261 if (is_date(num3, num2, num, tm))
198b0fb6
LT
262 break;
263 return 0;
264 }
265 return end - date;
266}
267
268/*
269 * We've seen a digit. Time? Year? Date?
270 */
26a2d8ae 271static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
198b0fb6
LT
272{
273 int n;
274 char *end;
275 unsigned long num;
276
277 num = strtoul(date, &end, 10);
278
279 /*
280 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
281 */
282 if (num > 946684800) {
283 time_t time = num;
9cb480f2
JH
284 if (gmtime_r(&time, tm)) {
285 *tm_gmt = 1;
198b0fb6 286 return end - date;
9cb480f2 287 }
198b0fb6
LT
288 }
289
290 /*
291 * Check for special formats: num[:-/]num[same]num
292 */
293 switch (*end) {
294 case ':':
295 case '/':
296 case '-':
297 if (isdigit(end[1])) {
298 int match = match_multi_number(num, *end, date, end, tm);
299 if (match)
300 return match;
89967023
LT
301 }
302 }
198b0fb6
LT
303
304 /*
305 * None of the special formats? Try to guess what
306 * the number meant. We use the number of digits
307 * to make a more educated guess..
308 */
309 n = 0;
310 do {
311 n++;
312 } while (isdigit(date[n]));
313
314 /* Four-digit year or a timezone? */
315 if (n == 4) {
316 if (num <= 1200 && *offset == -1) {
317 unsigned int minutes = num % 100;
318 unsigned int hours = num / 100;
319 *offset = hours*60 + minutes;
320 } else if (num > 1900 && num < 2100)
321 tm->tm_year = num - 1900;
322 return n;
323 }
324
325 /*
326 * NOTE! We will give precedence to day-of-month over month or
82f9d58a 327 * year numbers in the 1-12 range. So 05 is always "mday 5",
198b0fb6
LT
328 * unless we already have a mday..
329 *
330 * IOW, 01 Apr 05 parses as "April 1st, 2005".
331 */
332 if (num > 0 && num < 32 && tm->tm_mday < 0) {
333 tm->tm_mday = num;
334 return n;
335 }
336
337 /* Two-digit year? */
338 if (n == 2 && tm->tm_year < 0) {
339 if (num < 10 && tm->tm_mday >= 0) {
340 tm->tm_year = num + 100;
341 return n;
342 }
343 if (num >= 70) {
344 tm->tm_year = num;
345 return n;
346 }
347 }
348
349 if (num > 0 && num < 32) {
350 tm->tm_mday = num;
351 } else if (num > 1900) {
352 tm->tm_year = num - 1900;
353 } else if (num > 70) {
354 tm->tm_year = num;
355 } else if (num > 0 && num < 13) {
356 tm->tm_mon = num-1;
357 }
ecee9d9e 358
198b0fb6 359 return n;
89967023 360}
ecee9d9e 361
26a2d8ae 362static int match_tz(const char *date, int *offp)
89967023
LT
363{
364 char *end;
365 int offset = strtoul(date+1, &end, 10);
366 int min, hour;
198b0fb6 367 int n = end - date - 1;
ecee9d9e 368
89967023
LT
369 min = offset % 100;
370 hour = offset / 100;
ecee9d9e 371
198b0fb6
LT
372 /*
373 * Don't accept any random crap.. At least 3 digits, and
374 * a valid minute. We might want to check that the minutes
375 * are divisible by 30 or something too.
376 */
68849b54
LT
377 if (min < 60 && n > 2) {
378 offset = hour*60+min;
379 if (*date == '-')
380 offset = -offset;
ecee9d9e 381
68849b54
LT
382 *offp = offset;
383 }
89967023
LT
384 return end - date;
385}
ecee9d9e 386
01c6ad29
LT
387static int date_string(unsigned long date, int offset, char *buf, int len)
388{
389 int sign = '+';
390
391 if (offset < 0) {
392 offset = -offset;
393 sign = '-';
394 }
395 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
396}
397
89967023
LT
398/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
399 (i.e. English) day/month names, and it doesn't work correctly with %z. */
2a39064c 400int parse_date(const char *date, char *result, int maxlen)
89967023
LT
401{
402 struct tm tm;
01c6ad29 403 int offset, tm_gmt;
89967023 404 time_t then;
ecee9d9e 405
89967023
LT
406 memset(&tm, 0, sizeof(tm));
407 tm.tm_year = -1;
408 tm.tm_mon = -1;
409 tm.tm_mday = -1;
eaa85129
LT
410 tm.tm_isdst = -1;
411 offset = -1;
9cb480f2 412 tm_gmt = 0;
89967023
LT
413
414 for (;;) {
415 int match = 0;
416 unsigned char c = *date;
417
418 /* Stop at end of string or newline */
419 if (!c || c == '\n')
420 break;
421
422 if (isalpha(c))
423 match = match_alpha(date, &tm, &offset);
424 else if (isdigit(c))
9cb480f2 425 match = match_digit(date, &tm, &offset, &tm_gmt);
89967023
LT
426 else if ((c == '-' || c == '+') && isdigit(date[1]))
427 match = match_tz(date, &offset);
428
429 if (!match) {
430 /* BAD CRAP */
431 match = 1;
432 }
433
434 date += match;
435 }
ecee9d9e 436
eaa85129
LT
437 /* mktime uses local timezone */
438 then = my_mktime(&tm);
439 if (offset == -1)
440 offset = (then - mktime(&tm)) / 60;
441
ecee9d9e 442 if (then == -1)
2a39064c 443 return -1;
ecee9d9e 444
9cb480f2
JH
445 if (!tm_gmt)
446 then -= offset * 60;
01c6ad29 447 return date_string(then, offset, result, maxlen);
ecee9d9e
ET
448}
449
450void datestamp(char *buf, int bufsize)
451{
452 time_t now;
453 int offset;
454
455 time(&now);
456
457 offset = my_mktime(localtime(&now)) - now;
458 offset /= 60;
459
01c6ad29 460 date_string(now, offset, buf, bufsize);
ecee9d9e 461}
3c07b1d1
LT
462
463static void update_tm(struct tm *tm, unsigned long sec)
464{
465 time_t n = mktime(tm) - sec;
466 localtime_r(&n, tm);
467}
468
a8aca418
LT
469static void date_yesterday(struct tm *tm, int *num)
470{
471 update_tm(tm, 24*60*60);
472}
473
474static void date_time(struct tm *tm, int hour)
475{
476 if (tm->tm_hour < hour)
477 date_yesterday(tm, NULL);
478 tm->tm_hour = hour;
479 tm->tm_min = 0;
480 tm->tm_sec = 0;
481}
482
483static void date_midnight(struct tm *tm, int *num)
484{
485 date_time(tm, 0);
486}
487
488static void date_noon(struct tm *tm, int *num)
489{
490 date_time(tm, 12);
491}
492
493static void date_tea(struct tm *tm, int *num)
494{
495 date_time(tm, 17);
496}
497
498static const struct special {
499 const char *name;
500 void (*fn)(struct tm *, int *);
501} special[] = {
502 { "yesterday", date_yesterday },
503 { "noon", date_noon },
504 { "midnight", date_midnight },
505 { "tea", date_tea },
506 { NULL }
507};
508
3c07b1d1
LT
509static const char *number_name[] = {
510 "zero", "one", "two", "three", "four",
511 "five", "six", "seven", "eight", "nine", "ten",
512};
513
a8aca418 514static const struct typelen {
3c07b1d1
LT
515 const char *type;
516 int length;
517} typelen[] = {
518 { "seconds", 1 },
519 { "minutes", 60 },
520 { "hours", 60*60 },
521 { "days", 24*60*60 },
522 { "weeks", 7*24*60*60 },
523 { NULL }
524};
525
526static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
527{
a8aca418
LT
528 const struct typelen *tl;
529 const struct special *s;
3c07b1d1
LT
530 const char *end = date;
531 int n = 1, i;
532
533 while (isalpha(*++end))
534 n++;
535
536 for (i = 0; i < 12; i++) {
537 int match = match_string(date, month_names[i]);
538 if (match >= 3) {
539 tm->tm_mon = i;
540 return end;
541 }
542 }
543
a8aca418
LT
544 for (s = special; s->name; s++) {
545 int len = strlen(s->name);
546 if (match_string(date, s->name) == len) {
547 s->fn(tm, num);
548 return end;
549 }
3c07b1d1
LT
550 }
551
552 if (!*num) {
553 for (i = 1; i < 11; i++) {
554 int len = strlen(number_name[i]);
555 if (match_string(date, number_name[i]) == len) {
556 *num = i;
557 return end;
558 }
559 }
560 if (match_string(date, "last") == 4)
561 *num = 1;
562 return end;
563 }
564
565 tl = typelen;
566 while (tl->type) {
567 int len = strlen(tl->type);
568 if (match_string(date, tl->type) >= len-1) {
569 update_tm(tm, tl->length * *num);
570 *num = 0;
571 return end;
572 }
573 tl++;
574 }
575
6b7b0427
LT
576 for (i = 0; i < 7; i++) {
577 int match = match_string(date, weekday_names[i]);
578 if (match >= 3) {
579 int diff, n = *num -1;
580 *num = 0;
581
582 diff = tm->tm_wday - i;
583 if (diff <= 0)
584 n++;
585 diff += 7*n;
586
587 update_tm(tm, diff * 24 * 60 * 60);
588 return end;
589 }
590 }
591
3c07b1d1
LT
592 if (match_string(date, "months") >= 5) {
593 int n = tm->tm_mon - *num;
594 *num = 0;
595 while (n < 0) {
596 n += 12;
597 tm->tm_year--;
598 }
599 tm->tm_mon = n;
600 return end;
601 }
602
603 if (match_string(date, "years") >= 4) {
604 tm->tm_year -= *num;
605 *num = 0;
606 return end;
607 }
608
609 return end;
610}
611
612unsigned long approxidate(const char *date)
613{
614 int number = 0;
615 struct tm tm, now;
616 struct timeval tv;
617 char buffer[50];
618
619 if (parse_date(date, buffer, sizeof(buffer)) > 0)
620 return strtoul(buffer, NULL, 10);
621
622 gettimeofday(&tv, NULL);
623 localtime_r(&tv.tv_sec, &tm);
624 now = tm;
625 for (;;) {
626 unsigned char c = *date;
627 if (!c)
628 break;
629 date++;
630 if (isdigit(c)) {
631 char *end;
632 number = strtoul(date-1, &end, 10);
633 date = end;
634 continue;
635 }
636 if (isalpha(c))
637 date = approxidate_alpha(date-1, &tm, &number);
638 }
639 if (number > 0 && number < 32)
640 tm.tm_mday = number;
b73cebf4 641 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
3c07b1d1
LT
642 tm.tm_year--;
643 return mktime(&tm);
644}