gitweb.cgi: git_blame2: an alternative simple working git blame
[git/jnareb-git/bp-gitweb.git] / date.c
blob66be23ab21ef855ecbadeceae1dea2972efd4c2f
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include <time.h>
8 #include <sys/time.h>
10 #include "cache.h"
12 static time_t my_mktime(struct tm *tm)
14 static const int mdays[] = {
15 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17 int year = tm->tm_year - 70;
18 int month = tm->tm_mon;
19 int day = tm->tm_mday;
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;
31 static const char *month_names[] = {
32 "January", "February", "March", "April", "May", "June",
33 "July", "August", "September", "October", "November", "December"
36 static const char *weekday_names[] = {
37 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
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"
45 static struct tm *time_to_tm(unsigned long time, int tz)
47 time_t t;
48 int minutes;
50 minutes = tz < 0 ? -tz : tz;
51 minutes = (minutes / 100)*60 + (minutes % 100);
52 minutes = tz < 0 ? -minutes : minutes;
53 t = time + minutes * 60;
54 return gmtime(&t);
57 const char *show_date(unsigned long time, int tz)
59 struct tm *tm;
60 static char timebuf[200];
62 tm = time_to_tm(time, tz);
63 if (!tm)
64 return NULL;
65 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
66 weekday_names[tm->tm_wday],
67 month_names[tm->tm_mon],
68 tm->tm_mday,
69 tm->tm_hour, tm->tm_min, tm->tm_sec,
70 tm->tm_year + 1900, tz);
71 return timebuf;
74 const char *show_rfc2822_date(unsigned long time, int tz)
76 struct tm *tm;
77 static char timebuf[200];
79 tm = time_to_tm(time, tz);
80 if (!tm)
81 return NULL;
82 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
83 weekday_names[tm->tm_wday], tm->tm_mday,
84 month_names[tm->tm_mon], tm->tm_year + 1900,
85 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
86 return timebuf;
90 * Check these. And note how it doesn't do the summer-time conversion.
92 * In my world, it's always summer, and things are probably a bit off
93 * in other ways too.
95 static const struct {
96 const char *name;
97 int offset;
98 int dst;
99 } timezone_names[] = {
100 { "IDLW", -12, 0, }, /* International Date Line West */
101 { "NT", -11, 0, }, /* Nome */
102 { "CAT", -10, 0, }, /* Central Alaska */
103 { "HST", -10, 0, }, /* Hawaii Standard */
104 { "HDT", -10, 1, }, /* Hawaii Daylight */
105 { "YST", -9, 0, }, /* Yukon Standard */
106 { "YDT", -9, 1, }, /* Yukon Daylight */
107 { "PST", -8, 0, }, /* Pacific Standard */
108 { "PDT", -8, 1, }, /* Pacific Daylight */
109 { "MST", -7, 0, }, /* Mountain Standard */
110 { "MDT", -7, 1, }, /* Mountain Daylight */
111 { "CST", -6, 0, }, /* Central Standard */
112 { "CDT", -6, 1, }, /* Central Daylight */
113 { "EST", -5, 0, }, /* Eastern Standard */
114 { "EDT", -5, 1, }, /* Eastern Daylight */
115 { "AST", -3, 0, }, /* Atlantic Standard */
116 { "ADT", -3, 1, }, /* Atlantic Daylight */
117 { "WAT", -1, 0, }, /* West Africa */
119 { "GMT", 0, 0, }, /* Greenwich Mean */
120 { "UTC", 0, 0, }, /* Universal (Coordinated) */
122 { "WET", 0, 0, }, /* Western European */
123 { "BST", 0, 1, }, /* British Summer */
124 { "CET", +1, 0, }, /* Central European */
125 { "MET", +1, 0, }, /* Middle European */
126 { "MEWT", +1, 0, }, /* Middle European Winter */
127 { "MEST", +1, 1, }, /* Middle European Summer */
128 { "CEST", +1, 1, }, /* Central European Summer */
129 { "MESZ", +1, 1, }, /* Middle European Summer */
130 { "FWT", +1, 0, }, /* French Winter */
131 { "FST", +1, 1, }, /* French Summer */
132 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
133 { "EEST", +2, 1, }, /* Eastern European Daylight */
134 { "WAST", +7, 0, }, /* West Australian Standard */
135 { "WADT", +7, 1, }, /* West Australian Daylight */
136 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
137 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
138 { "EAST", +10, 0, }, /* Eastern Australian Standard */
139 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
140 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
141 { "NZT", +11, 0, }, /* New Zealand */
142 { "NZST", +11, 0, }, /* New Zealand Standard */
143 { "NZDT", +11, 1, }, /* New Zealand Daylight */
144 { "IDLE", +12, 0, }, /* International Date Line East */
147 static int match_string(const char *date, const char *str)
149 int i = 0;
151 for (i = 0; *date; date++, str++, i++) {
152 if (*date == *str)
153 continue;
154 if (toupper(*date) == toupper(*str))
155 continue;
156 if (!isalnum(*date))
157 break;
158 return 0;
160 return i;
163 static int skip_alpha(const char *date)
165 int i = 0;
166 do {
167 i++;
168 } while (isalpha(date[i]));
169 return i;
173 * Parse month, weekday, or timezone name
175 static int match_alpha(const char *date, struct tm *tm, int *offset)
177 int i;
179 for (i = 0; i < 12; i++) {
180 int match = match_string(date, month_names[i]);
181 if (match >= 3) {
182 tm->tm_mon = i;
183 return match;
187 for (i = 0; i < 7; i++) {
188 int match = match_string(date, weekday_names[i]);
189 if (match >= 3) {
190 tm->tm_wday = i;
191 return match;
195 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
196 int match = match_string(date, timezone_names[i].name);
197 if (match >= 3) {
198 int off = timezone_names[i].offset;
200 /* This is bogus, but we like summer */
201 off += timezone_names[i].dst;
203 /* Only use the tz name offset if we don't have anything better */
204 if (*offset == -1)
205 *offset = 60*off;
207 return match;
211 if (match_string(date, "PM") == 2) {
212 if (tm->tm_hour > 0 && tm->tm_hour < 12)
213 tm->tm_hour += 12;
214 return 2;
217 /* BAD CRAP */
218 return skip_alpha(date);
221 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
223 if (month > 0 && month < 13 && day > 0 && day < 32) {
224 struct tm check = *tm;
225 struct tm *r = (now_tm ? &check : tm);
226 time_t specified;
228 r->tm_mon = month - 1;
229 r->tm_mday = day;
230 if (year == -1) {
231 if (!now_tm)
232 return 1;
233 r->tm_year = now_tm->tm_year;
235 else if (year >= 1970 && year < 2100)
236 r->tm_year = year - 1900;
237 else if (year > 70 && year < 100)
238 r->tm_year = year;
239 else if (year < 38)
240 r->tm_year = year + 100;
241 else
242 return 0;
243 if (!now_tm)
244 return 1;
246 specified = my_mktime(r);
248 /* Be it commit time or author time, it does not make
249 * sense to specify timestamp way into the future. Make
250 * sure it is not later than ten days from now...
252 if (now + 10*24*3600 < specified)
253 return 0;
254 tm->tm_mon = r->tm_mon;
255 tm->tm_mday = r->tm_mday;
256 if (year != -1)
257 tm->tm_year = r->tm_year;
258 return 1;
260 return 0;
263 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
265 time_t now;
266 struct tm now_tm;
267 struct tm *refuse_future;
268 long num2, num3;
270 num2 = strtol(end+1, &end, 10);
271 num3 = -1;
272 if (*end == c && isdigit(end[1]))
273 num3 = strtol(end+1, &end, 10);
275 /* Time? Date? */
276 switch (c) {
277 case ':':
278 if (num3 < 0)
279 num3 = 0;
280 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
281 tm->tm_hour = num;
282 tm->tm_min = num2;
283 tm->tm_sec = num3;
284 break;
286 return 0;
288 case '-':
289 case '/':
290 case '.':
291 now = time(NULL);
292 refuse_future = NULL;
293 if (gmtime_r(&now, &now_tm))
294 refuse_future = &now_tm;
296 if (num > 70) {
297 /* yyyy-mm-dd? */
298 if (is_date(num, num2, num3, refuse_future, now, tm))
299 break;
300 /* yyyy-dd-mm? */
301 if (is_date(num, num3, num2, refuse_future, now, tm))
302 break;
304 /* Our eastern European friends say dd.mm.yy[yy]
305 * is the norm there, so giving precedence to
306 * mm/dd/yy[yy] form only when separator is not '.'
308 if (c != '.' &&
309 is_date(num3, num, num2, refuse_future, now, tm))
310 break;
311 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
312 if (is_date(num3, num2, num, refuse_future, now, tm))
313 break;
314 /* Funny European mm.dd.yy */
315 if (c == '.' &&
316 is_date(num3, num, num2, refuse_future, now, tm))
317 break;
318 return 0;
320 return end - date;
324 * We've seen a digit. Time? Year? Date?
326 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
328 int n;
329 char *end;
330 unsigned long num;
332 num = strtoul(date, &end, 10);
335 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
337 if (num > 946684800) {
338 time_t time = num;
339 if (gmtime_r(&time, tm)) {
340 *tm_gmt = 1;
341 return end - date;
346 * Check for special formats: num[-.:/]num[same]num
348 switch (*end) {
349 case ':':
350 case '.':
351 case '/':
352 case '-':
353 if (isdigit(end[1])) {
354 int match = match_multi_number(num, *end, date, end, tm);
355 if (match)
356 return match;
361 * None of the special formats? Try to guess what
362 * the number meant. We use the number of digits
363 * to make a more educated guess..
365 n = 0;
366 do {
367 n++;
368 } while (isdigit(date[n]));
370 /* Four-digit year or a timezone? */
371 if (n == 4) {
372 if (num <= 1400 && *offset == -1) {
373 unsigned int minutes = num % 100;
374 unsigned int hours = num / 100;
375 *offset = hours*60 + minutes;
376 } else if (num > 1900 && num < 2100)
377 tm->tm_year = num - 1900;
378 return n;
382 * NOTE! We will give precedence to day-of-month over month or
383 * year numbers in the 1-12 range. So 05 is always "mday 5",
384 * unless we already have a mday..
386 * IOW, 01 Apr 05 parses as "April 1st, 2005".
388 if (num > 0 && num < 32 && tm->tm_mday < 0) {
389 tm->tm_mday = num;
390 return n;
393 /* Two-digit year? */
394 if (n == 2 && tm->tm_year < 0) {
395 if (num < 10 && tm->tm_mday >= 0) {
396 tm->tm_year = num + 100;
397 return n;
399 if (num >= 70) {
400 tm->tm_year = num;
401 return n;
405 if (num > 0 && num < 32) {
406 tm->tm_mday = num;
407 } else if (num > 1900) {
408 tm->tm_year = num - 1900;
409 } else if (num > 70) {
410 tm->tm_year = num;
411 } else if (num > 0 && num < 13) {
412 tm->tm_mon = num-1;
415 return n;
418 static int match_tz(const char *date, int *offp)
420 char *end;
421 int offset = strtoul(date+1, &end, 10);
422 int min, hour;
423 int n = end - date - 1;
425 min = offset % 100;
426 hour = offset / 100;
429 * Don't accept any random crap.. At least 3 digits, and
430 * a valid minute. We might want to check that the minutes
431 * are divisible by 30 or something too.
433 if (min < 60 && n > 2) {
434 offset = hour*60+min;
435 if (*date == '-')
436 offset = -offset;
438 *offp = offset;
440 return end - date;
443 static int date_string(unsigned long date, int offset, char *buf, int len)
445 int sign = '+';
447 if (offset < 0) {
448 offset = -offset;
449 sign = '-';
451 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
454 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
455 (i.e. English) day/month names, and it doesn't work correctly with %z. */
456 int parse_date(const char *date, char *result, int maxlen)
458 struct tm tm;
459 int offset, tm_gmt;
460 time_t then;
462 memset(&tm, 0, sizeof(tm));
463 tm.tm_year = -1;
464 tm.tm_mon = -1;
465 tm.tm_mday = -1;
466 tm.tm_isdst = -1;
467 offset = -1;
468 tm_gmt = 0;
470 for (;;) {
471 int match = 0;
472 unsigned char c = *date;
474 /* Stop at end of string or newline */
475 if (!c || c == '\n')
476 break;
478 if (isalpha(c))
479 match = match_alpha(date, &tm, &offset);
480 else if (isdigit(c))
481 match = match_digit(date, &tm, &offset, &tm_gmt);
482 else if ((c == '-' || c == '+') && isdigit(date[1]))
483 match = match_tz(date, &offset);
485 if (!match) {
486 /* BAD CRAP */
487 match = 1;
490 date += match;
493 /* mktime uses local timezone */
494 then = my_mktime(&tm);
495 if (offset == -1)
496 offset = (then - mktime(&tm)) / 60;
498 if (then == -1)
499 return -1;
501 if (!tm_gmt)
502 then -= offset * 60;
503 return date_string(then, offset, result, maxlen);
506 void datestamp(char *buf, int bufsize)
508 time_t now;
509 int offset;
511 time(&now);
513 offset = my_mktime(localtime(&now)) - now;
514 offset /= 60;
516 date_string(now, offset, buf, bufsize);
519 static void update_tm(struct tm *tm, unsigned long sec)
521 time_t n = mktime(tm) - sec;
522 localtime_r(&n, tm);
525 static void date_yesterday(struct tm *tm, int *num)
527 update_tm(tm, 24*60*60);
530 static void date_time(struct tm *tm, int hour)
532 if (tm->tm_hour < hour)
533 date_yesterday(tm, NULL);
534 tm->tm_hour = hour;
535 tm->tm_min = 0;
536 tm->tm_sec = 0;
539 static void date_midnight(struct tm *tm, int *num)
541 date_time(tm, 0);
544 static void date_noon(struct tm *tm, int *num)
546 date_time(tm, 12);
549 static void date_tea(struct tm *tm, int *num)
551 date_time(tm, 17);
554 static const struct special {
555 const char *name;
556 void (*fn)(struct tm *, int *);
557 } special[] = {
558 { "yesterday", date_yesterday },
559 { "noon", date_noon },
560 { "midnight", date_midnight },
561 { "tea", date_tea },
562 { NULL }
565 static const char *number_name[] = {
566 "zero", "one", "two", "three", "four",
567 "five", "six", "seven", "eight", "nine", "ten",
570 static const struct typelen {
571 const char *type;
572 int length;
573 } typelen[] = {
574 { "seconds", 1 },
575 { "minutes", 60 },
576 { "hours", 60*60 },
577 { "days", 24*60*60 },
578 { "weeks", 7*24*60*60 },
579 { NULL }
582 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
584 const struct typelen *tl;
585 const struct special *s;
586 const char *end = date;
587 int n = 1, i;
589 while (isalpha(*++end))
590 n++;
592 for (i = 0; i < 12; i++) {
593 int match = match_string(date, month_names[i]);
594 if (match >= 3) {
595 tm->tm_mon = i;
596 return end;
600 for (s = special; s->name; s++) {
601 int len = strlen(s->name);
602 if (match_string(date, s->name) == len) {
603 s->fn(tm, num);
604 return end;
608 if (!*num) {
609 for (i = 1; i < 11; i++) {
610 int len = strlen(number_name[i]);
611 if (match_string(date, number_name[i]) == len) {
612 *num = i;
613 return end;
616 if (match_string(date, "last") == 4)
617 *num = 1;
618 return end;
621 tl = typelen;
622 while (tl->type) {
623 int len = strlen(tl->type);
624 if (match_string(date, tl->type) >= len-1) {
625 update_tm(tm, tl->length * *num);
626 *num = 0;
627 return end;
629 tl++;
632 for (i = 0; i < 7; i++) {
633 int match = match_string(date, weekday_names[i]);
634 if (match >= 3) {
635 int diff, n = *num -1;
636 *num = 0;
638 diff = tm->tm_wday - i;
639 if (diff <= 0)
640 n++;
641 diff += 7*n;
643 update_tm(tm, diff * 24 * 60 * 60);
644 return end;
648 if (match_string(date, "months") >= 5) {
649 int n = tm->tm_mon - *num;
650 *num = 0;
651 while (n < 0) {
652 n += 12;
653 tm->tm_year--;
655 tm->tm_mon = n;
656 return end;
659 if (match_string(date, "years") >= 4) {
660 tm->tm_year -= *num;
661 *num = 0;
662 return end;
665 return end;
668 unsigned long approxidate(const char *date)
670 int number = 0;
671 struct tm tm, now;
672 struct timeval tv;
673 char buffer[50];
675 if (parse_date(date, buffer, sizeof(buffer)) > 0)
676 return strtoul(buffer, NULL, 10);
678 gettimeofday(&tv, NULL);
679 localtime_r(&tv.tv_sec, &tm);
680 now = tm;
681 for (;;) {
682 unsigned char c = *date;
683 if (!c)
684 break;
685 date++;
686 if (isdigit(c)) {
687 char *end;
688 number = strtoul(date-1, &end, 10);
689 date = end;
690 continue;
692 if (isalpha(c))
693 date = approxidate_alpha(date-1, &tm, &number);
695 if (number > 0 && number < 32)
696 tm.tm_mday = number;
697 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
698 tm.tm_year--;
699 return mktime(&tm);