make it easier for people who just want to get rid of 'git gc --auto'
[git/spearce.git] / date.c
bloba74ed86422763e7d7e5dccf73530e52551a6929a
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
9 static time_t my_mktime(struct tm *tm)
11 static const int mdays[] = {
12 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
14 int year = tm->tm_year - 70;
15 int month = tm->tm_mon;
16 int day = tm->tm_mday;
18 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
19 return -1;
20 if (month < 0 || month > 11) /* array bounds */
21 return -1;
22 if (month < 2 || (year + 2) % 4)
23 day--;
24 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
25 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
28 static const char *month_names[] = {
29 "January", "February", "March", "April", "May", "June",
30 "July", "August", "September", "October", "November", "December"
33 static const char *weekday_names[] = {
34 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
37 static time_t gm_time_t(unsigned long time, int tz)
39 int minutes;
41 minutes = tz < 0 ? -tz : tz;
42 minutes = (minutes / 100)*60 + (minutes % 100);
43 minutes = tz < 0 ? -minutes : minutes;
44 return time + minutes * 60;
48 * The "tz" thing is passed in as this strange "decimal parse of tz"
49 * thing, which means that tz -0100 is passed in as the integer -100,
50 * even though it means "sixty minutes off"
52 static struct tm *time_to_tm(unsigned long time, int tz)
54 time_t t = gm_time_t(time, tz);
55 return gmtime(&t);
59 * What value of "tz" was in effect back then at "time" in the
60 * local timezone?
62 static int local_tzoffset(unsigned long time)
64 time_t t, t_local;
65 struct tm tm;
66 int offset, eastwest;
68 t = time;
69 localtime_r(&t, &tm);
70 t_local = my_mktime(&tm);
72 if (t_local < t) {
73 eastwest = -1;
74 offset = t - t_local;
75 } else {
76 eastwest = 1;
77 offset = t_local - t;
79 offset /= 60; /* in minutes */
80 offset = (offset % 60) + ((offset / 60) * 100);
81 return offset * eastwest;
84 const char *show_date(unsigned long time, int tz, enum date_mode mode)
86 struct tm *tm;
87 static char timebuf[200];
89 if (mode == DATE_RELATIVE) {
90 unsigned long diff;
91 struct timeval now;
92 gettimeofday(&now, NULL);
93 if (now.tv_sec < time)
94 return "in the future";
95 diff = now.tv_sec - time;
96 if (diff < 90) {
97 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
98 return timebuf;
100 /* Turn it into minutes */
101 diff = (diff + 30) / 60;
102 if (diff < 90) {
103 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
104 return timebuf;
106 /* Turn it into hours */
107 diff = (diff + 30) / 60;
108 if (diff < 36) {
109 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
110 return timebuf;
112 /* We deal with number of days from here on */
113 diff = (diff + 12) / 24;
114 if (diff < 14) {
115 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
116 return timebuf;
118 /* Say weeks for the past 10 weeks or so */
119 if (diff < 70) {
120 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
121 return timebuf;
123 /* Say months for the past 12 months or so */
124 if (diff < 360) {
125 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
126 return timebuf;
128 /* Else fall back on absolute format.. */
131 if (mode == DATE_LOCAL)
132 tz = local_tzoffset(time);
134 tm = time_to_tm(time, tz);
135 if (!tm)
136 return NULL;
137 if (mode == DATE_SHORT)
138 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
139 tm->tm_mon + 1, tm->tm_mday);
140 else if (mode == DATE_ISO8601)
141 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
142 tm->tm_year + 1900,
143 tm->tm_mon + 1,
144 tm->tm_mday,
145 tm->tm_hour, tm->tm_min, tm->tm_sec,
146 tz);
147 else if (mode == DATE_RFC2822)
148 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
149 weekday_names[tm->tm_wday], tm->tm_mday,
150 month_names[tm->tm_mon], tm->tm_year + 1900,
151 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
152 else
153 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
154 weekday_names[tm->tm_wday],
155 month_names[tm->tm_mon],
156 tm->tm_mday,
157 tm->tm_hour, tm->tm_min, tm->tm_sec,
158 tm->tm_year + 1900,
159 (mode == DATE_LOCAL) ? 0 : ' ',
160 tz);
161 return timebuf;
165 * Check these. And note how it doesn't do the summer-time conversion.
167 * In my world, it's always summer, and things are probably a bit off
168 * in other ways too.
170 static const struct {
171 const char *name;
172 int offset;
173 int dst;
174 } timezone_names[] = {
175 { "IDLW", -12, 0, }, /* International Date Line West */
176 { "NT", -11, 0, }, /* Nome */
177 { "CAT", -10, 0, }, /* Central Alaska */
178 { "HST", -10, 0, }, /* Hawaii Standard */
179 { "HDT", -10, 1, }, /* Hawaii Daylight */
180 { "YST", -9, 0, }, /* Yukon Standard */
181 { "YDT", -9, 1, }, /* Yukon Daylight */
182 { "PST", -8, 0, }, /* Pacific Standard */
183 { "PDT", -8, 1, }, /* Pacific Daylight */
184 { "MST", -7, 0, }, /* Mountain Standard */
185 { "MDT", -7, 1, }, /* Mountain Daylight */
186 { "CST", -6, 0, }, /* Central Standard */
187 { "CDT", -6, 1, }, /* Central Daylight */
188 { "EST", -5, 0, }, /* Eastern Standard */
189 { "EDT", -5, 1, }, /* Eastern Daylight */
190 { "AST", -3, 0, }, /* Atlantic Standard */
191 { "ADT", -3, 1, }, /* Atlantic Daylight */
192 { "WAT", -1, 0, }, /* West Africa */
194 { "GMT", 0, 0, }, /* Greenwich Mean */
195 { "UTC", 0, 0, }, /* Universal (Coordinated) */
197 { "WET", 0, 0, }, /* Western European */
198 { "BST", 0, 1, }, /* British Summer */
199 { "CET", +1, 0, }, /* Central European */
200 { "MET", +1, 0, }, /* Middle European */
201 { "MEWT", +1, 0, }, /* Middle European Winter */
202 { "MEST", +1, 1, }, /* Middle European Summer */
203 { "CEST", +1, 1, }, /* Central European Summer */
204 { "MESZ", +1, 1, }, /* Middle European Summer */
205 { "FWT", +1, 0, }, /* French Winter */
206 { "FST", +1, 1, }, /* French Summer */
207 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
208 { "EEST", +2, 1, }, /* Eastern European Daylight */
209 { "WAST", +7, 0, }, /* West Australian Standard */
210 { "WADT", +7, 1, }, /* West Australian Daylight */
211 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
212 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
213 { "EAST", +10, 0, }, /* Eastern Australian Standard */
214 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
215 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
216 { "NZT", +12, 0, }, /* New Zealand */
217 { "NZST", +12, 0, }, /* New Zealand Standard */
218 { "NZDT", +12, 1, }, /* New Zealand Daylight */
219 { "IDLE", +12, 0, }, /* International Date Line East */
222 static int match_string(const char *date, const char *str)
224 int i = 0;
226 for (i = 0; *date; date++, str++, i++) {
227 if (*date == *str)
228 continue;
229 if (toupper(*date) == toupper(*str))
230 continue;
231 if (!isalnum(*date))
232 break;
233 return 0;
235 return i;
238 static int skip_alpha(const char *date)
240 int i = 0;
241 do {
242 i++;
243 } while (isalpha(date[i]));
244 return i;
248 * Parse month, weekday, or timezone name
250 static int match_alpha(const char *date, struct tm *tm, int *offset)
252 int i;
254 for (i = 0; i < 12; i++) {
255 int match = match_string(date, month_names[i]);
256 if (match >= 3) {
257 tm->tm_mon = i;
258 return match;
262 for (i = 0; i < 7; i++) {
263 int match = match_string(date, weekday_names[i]);
264 if (match >= 3) {
265 tm->tm_wday = i;
266 return match;
270 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
271 int match = match_string(date, timezone_names[i].name);
272 if (match >= 3) {
273 int off = timezone_names[i].offset;
275 /* This is bogus, but we like summer */
276 off += timezone_names[i].dst;
278 /* Only use the tz name offset if we don't have anything better */
279 if (*offset == -1)
280 *offset = 60*off;
282 return match;
286 if (match_string(date, "PM") == 2) {
287 tm->tm_hour = (tm->tm_hour % 12) + 12;
288 return 2;
291 if (match_string(date, "AM") == 2) {
292 tm->tm_hour = (tm->tm_hour % 12) + 0;
293 return 2;
296 /* BAD CRAP */
297 return skip_alpha(date);
300 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
302 if (month > 0 && month < 13 && day > 0 && day < 32) {
303 struct tm check = *tm;
304 struct tm *r = (now_tm ? &check : tm);
305 time_t specified;
307 r->tm_mon = month - 1;
308 r->tm_mday = day;
309 if (year == -1) {
310 if (!now_tm)
311 return 1;
312 r->tm_year = now_tm->tm_year;
314 else if (year >= 1970 && year < 2100)
315 r->tm_year = year - 1900;
316 else if (year > 70 && year < 100)
317 r->tm_year = year;
318 else if (year < 38)
319 r->tm_year = year + 100;
320 else
321 return 0;
322 if (!now_tm)
323 return 1;
325 specified = my_mktime(r);
327 /* Be it commit time or author time, it does not make
328 * sense to specify timestamp way into the future. Make
329 * sure it is not later than ten days from now...
331 if (now + 10*24*3600 < specified)
332 return 0;
333 tm->tm_mon = r->tm_mon;
334 tm->tm_mday = r->tm_mday;
335 if (year != -1)
336 tm->tm_year = r->tm_year;
337 return 1;
339 return 0;
342 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
344 time_t now;
345 struct tm now_tm;
346 struct tm *refuse_future;
347 long num2, num3;
349 num2 = strtol(end+1, &end, 10);
350 num3 = -1;
351 if (*end == c && isdigit(end[1]))
352 num3 = strtol(end+1, &end, 10);
354 /* Time? Date? */
355 switch (c) {
356 case ':':
357 if (num3 < 0)
358 num3 = 0;
359 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
360 tm->tm_hour = num;
361 tm->tm_min = num2;
362 tm->tm_sec = num3;
363 break;
365 return 0;
367 case '-':
368 case '/':
369 case '.':
370 now = time(NULL);
371 refuse_future = NULL;
372 if (gmtime_r(&now, &now_tm))
373 refuse_future = &now_tm;
375 if (num > 70) {
376 /* yyyy-mm-dd? */
377 if (is_date(num, num2, num3, refuse_future, now, tm))
378 break;
379 /* yyyy-dd-mm? */
380 if (is_date(num, num3, num2, refuse_future, now, tm))
381 break;
383 /* Our eastern European friends say dd.mm.yy[yy]
384 * is the norm there, so giving precedence to
385 * mm/dd/yy[yy] form only when separator is not '.'
387 if (c != '.' &&
388 is_date(num3, num, num2, refuse_future, now, tm))
389 break;
390 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
391 if (is_date(num3, num2, num, refuse_future, now, tm))
392 break;
393 /* Funny European mm.dd.yy */
394 if (c == '.' &&
395 is_date(num3, num, num2, refuse_future, now, tm))
396 break;
397 return 0;
399 return end - date;
403 * We've seen a digit. Time? Year? Date?
405 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
407 int n;
408 char *end;
409 unsigned long num;
411 num = strtoul(date, &end, 10);
414 * Seconds since 1970? We trigger on that for any numbers with
415 * more than 8 digits. This is because we don't want to rule out
416 * numbers like 20070606 as a YYYYMMDD date.
418 if (num >= 100000000) {
419 time_t time = num;
420 if (gmtime_r(&time, tm)) {
421 *tm_gmt = 1;
422 return end - date;
427 * Check for special formats: num[-.:/]num[same]num
429 switch (*end) {
430 case ':':
431 case '.':
432 case '/':
433 case '-':
434 if (isdigit(end[1])) {
435 int match = match_multi_number(num, *end, date, end, tm);
436 if (match)
437 return match;
442 * None of the special formats? Try to guess what
443 * the number meant. We use the number of digits
444 * to make a more educated guess..
446 n = 0;
447 do {
448 n++;
449 } while (isdigit(date[n]));
451 /* Four-digit year or a timezone? */
452 if (n == 4) {
453 if (num <= 1400 && *offset == -1) {
454 unsigned int minutes = num % 100;
455 unsigned int hours = num / 100;
456 *offset = hours*60 + minutes;
457 } else if (num > 1900 && num < 2100)
458 tm->tm_year = num - 1900;
459 return n;
463 * NOTE! We will give precedence to day-of-month over month or
464 * year numbers in the 1-12 range. So 05 is always "mday 5",
465 * unless we already have a mday..
467 * IOW, 01 Apr 05 parses as "April 1st, 2005".
469 if (num > 0 && num < 32 && tm->tm_mday < 0) {
470 tm->tm_mday = num;
471 return n;
474 /* Two-digit year? */
475 if (n == 2 && tm->tm_year < 0) {
476 if (num < 10 && tm->tm_mday >= 0) {
477 tm->tm_year = num + 100;
478 return n;
480 if (num >= 70) {
481 tm->tm_year = num;
482 return n;
486 if (num > 0 && num < 32) {
487 tm->tm_mday = num;
488 } else if (num > 1900) {
489 tm->tm_year = num - 1900;
490 } else if (num > 70) {
491 tm->tm_year = num;
492 } else if (num > 0 && num < 13) {
493 tm->tm_mon = num-1;
496 return n;
499 static int match_tz(const char *date, int *offp)
501 char *end;
502 int offset = strtoul(date+1, &end, 10);
503 int min, hour;
504 int n = end - date - 1;
506 min = offset % 100;
507 hour = offset / 100;
510 * Don't accept any random crap.. At least 3 digits, and
511 * a valid minute. We might want to check that the minutes
512 * are divisible by 30 or something too.
514 if (min < 60 && n > 2) {
515 offset = hour*60+min;
516 if (*date == '-')
517 offset = -offset;
519 *offp = offset;
521 return end - date;
524 static int date_string(unsigned long date, int offset, char *buf, int len)
526 int sign = '+';
528 if (offset < 0) {
529 offset = -offset;
530 sign = '-';
532 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
535 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
536 (i.e. English) day/month names, and it doesn't work correctly with %z. */
537 int parse_date(const char *date, char *result, int maxlen)
539 struct tm tm;
540 int offset, tm_gmt;
541 time_t then;
543 memset(&tm, 0, sizeof(tm));
544 tm.tm_year = -1;
545 tm.tm_mon = -1;
546 tm.tm_mday = -1;
547 tm.tm_isdst = -1;
548 offset = -1;
549 tm_gmt = 0;
551 for (;;) {
552 int match = 0;
553 unsigned char c = *date;
555 /* Stop at end of string or newline */
556 if (!c || c == '\n')
557 break;
559 if (isalpha(c))
560 match = match_alpha(date, &tm, &offset);
561 else if (isdigit(c))
562 match = match_digit(date, &tm, &offset, &tm_gmt);
563 else if ((c == '-' || c == '+') && isdigit(date[1]))
564 match = match_tz(date, &offset);
566 if (!match) {
567 /* BAD CRAP */
568 match = 1;
571 date += match;
574 /* mktime uses local timezone */
575 then = my_mktime(&tm);
576 if (offset == -1)
577 offset = (then - mktime(&tm)) / 60;
579 if (then == -1)
580 return -1;
582 if (!tm_gmt)
583 then -= offset * 60;
584 return date_string(then, offset, result, maxlen);
587 enum date_mode parse_date_format(const char *format)
589 if (!strcmp(format, "relative"))
590 return DATE_RELATIVE;
591 else if (!strcmp(format, "iso8601") ||
592 !strcmp(format, "iso"))
593 return DATE_ISO8601;
594 else if (!strcmp(format, "rfc2822") ||
595 !strcmp(format, "rfc"))
596 return DATE_RFC2822;
597 else if (!strcmp(format, "short"))
598 return DATE_SHORT;
599 else if (!strcmp(format, "local"))
600 return DATE_LOCAL;
601 else if (!strcmp(format, "default"))
602 return DATE_NORMAL;
603 else
604 die("unknown date format %s", format);
607 void datestamp(char *buf, int bufsize)
609 time_t now;
610 int offset;
612 time(&now);
614 offset = my_mktime(localtime(&now)) - now;
615 offset /= 60;
617 date_string(now, offset, buf, bufsize);
620 static void update_tm(struct tm *tm, unsigned long sec)
622 time_t n = mktime(tm) - sec;
623 localtime_r(&n, tm);
626 static void date_yesterday(struct tm *tm, int *num)
628 update_tm(tm, 24*60*60);
631 static void date_time(struct tm *tm, int hour)
633 if (tm->tm_hour < hour)
634 date_yesterday(tm, NULL);
635 tm->tm_hour = hour;
636 tm->tm_min = 0;
637 tm->tm_sec = 0;
640 static void date_midnight(struct tm *tm, int *num)
642 date_time(tm, 0);
645 static void date_noon(struct tm *tm, int *num)
647 date_time(tm, 12);
650 static void date_tea(struct tm *tm, int *num)
652 date_time(tm, 17);
655 static void date_pm(struct tm *tm, int *num)
657 int hour, n = *num;
658 *num = 0;
660 hour = tm->tm_hour;
661 if (n) {
662 hour = n;
663 tm->tm_min = 0;
664 tm->tm_sec = 0;
666 tm->tm_hour = (hour % 12) + 12;
669 static void date_am(struct tm *tm, int *num)
671 int hour, n = *num;
672 *num = 0;
674 hour = tm->tm_hour;
675 if (n) {
676 hour = n;
677 tm->tm_min = 0;
678 tm->tm_sec = 0;
680 tm->tm_hour = (hour % 12);
683 static void date_never(struct tm *tm, int *num)
685 tm->tm_mon = tm->tm_wday = tm->tm_yday
686 = tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
687 tm->tm_year = 70;
688 tm->tm_mday = 1;
691 static const struct special {
692 const char *name;
693 void (*fn)(struct tm *, int *);
694 } special[] = {
695 { "yesterday", date_yesterday },
696 { "noon", date_noon },
697 { "midnight", date_midnight },
698 { "tea", date_tea },
699 { "PM", date_pm },
700 { "AM", date_am },
701 { "never", date_never },
702 { NULL }
705 static const char *number_name[] = {
706 "zero", "one", "two", "three", "four",
707 "five", "six", "seven", "eight", "nine", "ten",
710 static const struct typelen {
711 const char *type;
712 int length;
713 } typelen[] = {
714 { "seconds", 1 },
715 { "minutes", 60 },
716 { "hours", 60*60 },
717 { "days", 24*60*60 },
718 { "weeks", 7*24*60*60 },
719 { NULL }
722 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
724 const struct typelen *tl;
725 const struct special *s;
726 const char *end = date;
727 int i;
729 while (isalpha(*++end));
732 for (i = 0; i < 12; i++) {
733 int match = match_string(date, month_names[i]);
734 if (match >= 3) {
735 tm->tm_mon = i;
736 return end;
740 for (s = special; s->name; s++) {
741 int len = strlen(s->name);
742 if (match_string(date, s->name) == len) {
743 s->fn(tm, num);
744 return end;
748 if (!*num) {
749 for (i = 1; i < 11; i++) {
750 int len = strlen(number_name[i]);
751 if (match_string(date, number_name[i]) == len) {
752 *num = i;
753 return end;
756 if (match_string(date, "last") == 4)
757 *num = 1;
758 return end;
761 tl = typelen;
762 while (tl->type) {
763 int len = strlen(tl->type);
764 if (match_string(date, tl->type) >= len-1) {
765 update_tm(tm, tl->length * *num);
766 *num = 0;
767 return end;
769 tl++;
772 for (i = 0; i < 7; i++) {
773 int match = match_string(date, weekday_names[i]);
774 if (match >= 3) {
775 int diff, n = *num -1;
776 *num = 0;
778 diff = tm->tm_wday - i;
779 if (diff <= 0)
780 n++;
781 diff += 7*n;
783 update_tm(tm, diff * 24 * 60 * 60);
784 return end;
788 if (match_string(date, "months") >= 5) {
789 int n = tm->tm_mon - *num;
790 *num = 0;
791 while (n < 0) {
792 n += 12;
793 tm->tm_year--;
795 tm->tm_mon = n;
796 return end;
799 if (match_string(date, "years") >= 4) {
800 tm->tm_year -= *num;
801 *num = 0;
802 return end;
805 return end;
808 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
810 char *end;
811 unsigned long number = strtoul(date, &end, 10);
813 switch (*end) {
814 case ':':
815 case '.':
816 case '/':
817 case '-':
818 if (isdigit(end[1])) {
819 int match = match_multi_number(number, *end, date, end, tm);
820 if (match)
821 return date + match;
825 *num = number;
826 return end;
829 unsigned long approxidate(const char *date)
831 int number = 0;
832 struct tm tm, now;
833 struct timeval tv;
834 char buffer[50];
836 if (parse_date(date, buffer, sizeof(buffer)) > 0)
837 return strtoul(buffer, NULL, 10);
839 gettimeofday(&tv, NULL);
840 localtime_r(&tv.tv_sec, &tm);
841 now = tm;
842 for (;;) {
843 unsigned char c = *date;
844 if (!c)
845 break;
846 date++;
847 if (isdigit(c)) {
848 date = approxidate_digit(date-1, &tm, &number);
849 continue;
851 if (isalpha(c))
852 date = approxidate_alpha(date-1, &tm, &number);
854 if (number > 0 && number < 32)
855 tm.tm_mday = number;
856 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
857 tm.tm_year--;
858 return mktime(&tm);