Code

git-add: warn when adding an ignored file with an explicit request.
[git.git] / date.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
7 #include "cache.h"
9 static time_t my_mktime(struct tm *tm)
10 {
11         static const int mdays[] = {
12             0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
13         };
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;
26 }
28 static const char *month_names[] = {
29         "January", "February", "March", "April", "May", "June",
30         "July", "August", "September", "October", "November", "December"
31 };
33 static const char *weekday_names[] = {
34         "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
35 };
37 static time_t gm_time_t(unsigned long time, int tz)
38 {
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;
45 }
47 /*
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"
51  */
52 static struct tm *time_to_tm(unsigned long time, int tz)
53 {
54         time_t t = gm_time_t(time, tz);
55         return gmtime(&t);
56 }
58 const char *show_date(unsigned long time, int tz, int relative)
59 {
60         struct tm *tm;
61         static char timebuf[200];
63         if (relative) {
64                 unsigned long diff;
65                 time_t t = gm_time_t(time, tz);
66                 struct timeval now;
67                 gettimeofday(&now, NULL);
68                 if (now.tv_sec < t)
69                         return "in the future";
70                 diff = now.tv_sec - t;
71                 if (diff < 90) {
72                         snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
73                         return timebuf;
74                 }
75                 /* Turn it into minutes */
76                 diff = (diff + 30) / 60;
77                 if (diff < 90) {
78                         snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
79                         return timebuf;
80                 }
81                 /* Turn it into hours */
82                 diff = (diff + 30) / 60;
83                 if (diff < 36) {
84                         snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
85                         return timebuf;
86                 }
87                 /* We deal with number of days from here on */
88                 diff = (diff + 12) / 24;
89                 if (diff < 14) {
90                         snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
91                         return timebuf;
92                 }
93                 /* Say weeks for the past 10 weeks or so */
94                 if (diff < 70) {
95                         snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
96                         return timebuf;
97                 }
98                 /* Say months for the past 12 months or so */
99                 if (diff < 360) {
100                         snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
101                         return timebuf;
102                 }
103                 /* Else fall back on absolute format.. */
104         }
106         tm = time_to_tm(time, tz);
107         if (!tm)
108                 return NULL;
109         sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
110                 weekday_names[tm->tm_wday],
111                 month_names[tm->tm_mon],
112                 tm->tm_mday,
113                 tm->tm_hour, tm->tm_min, tm->tm_sec,
114                 tm->tm_year + 1900, tz);
115         return timebuf;
118 const char *show_rfc2822_date(unsigned long time, int tz)
120         struct tm *tm;
121         static char timebuf[200];
123         tm = time_to_tm(time, tz);
124         if (!tm)
125                 return NULL;
126         sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
127                 weekday_names[tm->tm_wday], tm->tm_mday,
128                 month_names[tm->tm_mon], tm->tm_year + 1900,
129                 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
130         return timebuf;
133 /*
134  * Check these. And note how it doesn't do the summer-time conversion.
135  *
136  * In my world, it's always summer, and things are probably a bit off
137  * in other ways too.
138  */
139 static const struct {
140         const char *name;
141         int offset;
142         int dst;
143 } timezone_names[] = {
144         { "IDLW", -12, 0, },    /* International Date Line West */
145         { "NT",   -11, 0, },    /* Nome */
146         { "CAT",  -10, 0, },    /* Central Alaska */
147         { "HST",  -10, 0, },    /* Hawaii Standard */
148         { "HDT",  -10, 1, },    /* Hawaii Daylight */
149         { "YST",   -9, 0, },    /* Yukon Standard */
150         { "YDT",   -9, 1, },    /* Yukon Daylight */
151         { "PST",   -8, 0, },    /* Pacific Standard */
152         { "PDT",   -8, 1, },    /* Pacific Daylight */
153         { "MST",   -7, 0, },    /* Mountain Standard */
154         { "MDT",   -7, 1, },    /* Mountain Daylight */
155         { "CST",   -6, 0, },    /* Central Standard */
156         { "CDT",   -6, 1, },    /* Central Daylight */
157         { "EST",   -5, 0, },    /* Eastern Standard */
158         { "EDT",   -5, 1, },    /* Eastern Daylight */
159         { "AST",   -3, 0, },    /* Atlantic Standard */
160         { "ADT",   -3, 1, },    /* Atlantic Daylight */
161         { "WAT",   -1, 0, },    /* West Africa */
163         { "GMT",    0, 0, },    /* Greenwich Mean */
164         { "UTC",    0, 0, },    /* Universal (Coordinated) */
166         { "WET",    0, 0, },    /* Western European */
167         { "BST",    0, 1, },    /* British Summer */
168         { "CET",   +1, 0, },    /* Central European */
169         { "MET",   +1, 0, },    /* Middle European */
170         { "MEWT",  +1, 0, },    /* Middle European Winter */
171         { "MEST",  +1, 1, },    /* Middle European Summer */
172         { "CEST",  +1, 1, },    /* Central European Summer */
173         { "MESZ",  +1, 1, },    /* Middle European Summer */
174         { "FWT",   +1, 0, },    /* French Winter */
175         { "FST",   +1, 1, },    /* French Summer */
176         { "EET",   +2, 0, },    /* Eastern Europe, USSR Zone 1 */
177         { "EEST",  +2, 1, },    /* Eastern European Daylight */
178         { "WAST",  +7, 0, },    /* West Australian Standard */
179         { "WADT",  +7, 1, },    /* West Australian Daylight */
180         { "CCT",   +8, 0, },    /* China Coast, USSR Zone 7 */
181         { "JST",   +9, 0, },    /* Japan Standard, USSR Zone 8 */
182         { "EAST", +10, 0, },    /* Eastern Australian Standard */
183         { "EADT", +10, 1, },    /* Eastern Australian Daylight */
184         { "GST",  +10, 0, },    /* Guam Standard, USSR Zone 9 */
185         { "NZT",  +11, 0, },    /* New Zealand */
186         { "NZST", +11, 0, },    /* New Zealand Standard */
187         { "NZDT", +11, 1, },    /* New Zealand Daylight */
188         { "IDLE", +12, 0, },    /* International Date Line East */
189 };
191 static int match_string(const char *date, const char *str)
193         int i = 0;
195         for (i = 0; *date; date++, str++, i++) {
196                 if (*date == *str)
197                         continue;
198                 if (toupper(*date) == toupper(*str))
199                         continue;
200                 if (!isalnum(*date))
201                         break;
202                 return 0;
203         }
204         return i;
207 static int skip_alpha(const char *date)
209         int i = 0;
210         do {
211                 i++;
212         } while (isalpha(date[i]));
213         return i;
216 /*
217 * Parse month, weekday, or timezone name
218 */
219 static int match_alpha(const char *date, struct tm *tm, int *offset)
221         int i;
223         for (i = 0; i < 12; i++) {
224                 int match = match_string(date, month_names[i]);
225                 if (match >= 3) {
226                         tm->tm_mon = i;
227                         return match;
228                 }
229         }
231         for (i = 0; i < 7; i++) {
232                 int match = match_string(date, weekday_names[i]);
233                 if (match >= 3) {
234                         tm->tm_wday = i;
235                         return match;
236                 }
237         }
239         for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
240                 int match = match_string(date, timezone_names[i].name);
241                 if (match >= 3) {
242                         int off = timezone_names[i].offset;
244                         /* This is bogus, but we like summer */
245                         off += timezone_names[i].dst;
247                         /* Only use the tz name offset if we don't have anything better */
248                         if (*offset == -1)
249                                 *offset = 60*off;
251                         return match;
252                 }
253         }
255         if (match_string(date, "PM") == 2) {
256                 tm->tm_hour = (tm->tm_hour % 12) + 12;
257                 return 2;
258         }
260         if (match_string(date, "AM") == 2) {
261                 tm->tm_hour = (tm->tm_hour % 12) + 0;
262                 return 2;
263         }
265         /* BAD CRAP */
266         return skip_alpha(date);
269 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
271         if (month > 0 && month < 13 && day > 0 && day < 32) {
272                 struct tm check = *tm;
273                 struct tm *r = (now_tm ? &check : tm);
274                 time_t specified;
276                 r->tm_mon = month - 1;
277                 r->tm_mday = day;
278                 if (year == -1) {
279                         if (!now_tm)
280                                 return 1;
281                         r->tm_year = now_tm->tm_year;
282                 }
283                 else if (year >= 1970 && year < 2100)
284                         r->tm_year = year - 1900;
285                 else if (year > 70 && year < 100)
286                         r->tm_year = year;
287                 else if (year < 38)
288                         r->tm_year = year + 100;
289                 else
290                         return 0;
291                 if (!now_tm)
292                         return 1;
294                 specified = my_mktime(r);
296                 /* Be it commit time or author time, it does not make
297                  * sense to specify timestamp way into the future.  Make
298                  * sure it is not later than ten days from now...
299                  */
300                 if (now + 10*24*3600 < specified)
301                         return 0;
302                 tm->tm_mon = r->tm_mon;
303                 tm->tm_mday = r->tm_mday;
304                 if (year != -1)
305                         tm->tm_year = r->tm_year;
306                 return 1;
307         }
308         return 0;
311 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
313         time_t now;
314         struct tm now_tm;
315         struct tm *refuse_future;
316         long num2, num3;
318         num2 = strtol(end+1, &end, 10);
319         num3 = -1;
320         if (*end == c && isdigit(end[1]))
321                 num3 = strtol(end+1, &end, 10);
323         /* Time? Date? */
324         switch (c) {
325         case ':':
326                 if (num3 < 0)
327                         num3 = 0;
328                 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
329                         tm->tm_hour = num;
330                         tm->tm_min = num2;
331                         tm->tm_sec = num3;
332                         break;
333                 }
334                 return 0;
336         case '-':
337         case '/':
338         case '.':
339                 now = time(NULL);
340                 refuse_future = NULL;
341                 if (gmtime_r(&now, &now_tm))
342                         refuse_future = &now_tm;
344                 if (num > 70) {
345                         /* yyyy-mm-dd? */
346                         if (is_date(num, num2, num3, refuse_future, now, tm))
347                                 break;
348                         /* yyyy-dd-mm? */
349                         if (is_date(num, num3, num2, refuse_future, now, tm))
350                                 break;
351                 }
352                 /* Our eastern European friends say dd.mm.yy[yy]
353                  * is the norm there, so giving precedence to
354                  * mm/dd/yy[yy] form only when separator is not '.'
355                  */
356                 if (c != '.' &&
357                     is_date(num3, num, num2, refuse_future, now, tm))
358                         break;
359                 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
360                 if (is_date(num3, num2, num, refuse_future, now, tm))
361                         break;
362                 /* Funny European mm.dd.yy */
363                 if (c == '.' &&
364                     is_date(num3, num, num2, refuse_future, now, tm))
365                         break;
366                 return 0;
367         }
368         return end - date;
371 /*
372  * We've seen a digit. Time? Year? Date? 
373  */
374 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
376         int n;
377         char *end;
378         unsigned long num;
380         num = strtoul(date, &end, 10);
382         /*
383          * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
384          */
385         if (num > 946684800) {
386                 time_t time = num;
387                 if (gmtime_r(&time, tm)) {
388                         *tm_gmt = 1;
389                         return end - date;
390                 }
391         }
393         /*
394          * Check for special formats: num[-.:/]num[same]num
395          */
396         switch (*end) {
397         case ':':
398         case '.':
399         case '/':
400         case '-':
401                 if (isdigit(end[1])) {
402                         int match = match_multi_number(num, *end, date, end, tm);
403                         if (match)
404                                 return match;
405                 }
406         }
408         /*
409          * None of the special formats? Try to guess what
410          * the number meant. We use the number of digits
411          * to make a more educated guess..
412          */
413         n = 0;
414         do {
415                 n++;
416         } while (isdigit(date[n]));
418         /* Four-digit year or a timezone? */
419         if (n == 4) {
420                 if (num <= 1400 && *offset == -1) {
421                         unsigned int minutes = num % 100;
422                         unsigned int hours = num / 100;
423                         *offset = hours*60 + minutes;
424                 } else if (num > 1900 && num < 2100)
425                         tm->tm_year = num - 1900;
426                 return n;
427         }
429         /*
430          * NOTE! We will give precedence to day-of-month over month or
431          * year numbers in the 1-12 range. So 05 is always "mday 5",
432          * unless we already have a mday..
433          *
434          * IOW, 01 Apr 05 parses as "April 1st, 2005".
435          */
436         if (num > 0 && num < 32 && tm->tm_mday < 0) {
437                 tm->tm_mday = num;
438                 return n;
439         }
441         /* Two-digit year? */
442         if (n == 2 && tm->tm_year < 0) {
443                 if (num < 10 && tm->tm_mday >= 0) {
444                         tm->tm_year = num + 100;
445                         return n;
446                 }
447                 if (num >= 70) {
448                         tm->tm_year = num;
449                         return n;
450                 }
451         }
453         if (num > 0 && num < 32) {
454                 tm->tm_mday = num;
455         } else if (num > 1900) {
456                 tm->tm_year = num - 1900;
457         } else if (num > 70) {
458                 tm->tm_year = num;
459         } else if (num > 0 && num < 13) {
460                 tm->tm_mon = num-1;
461         }
462                 
463         return n;
466 static int match_tz(const char *date, int *offp)
468         char *end;
469         int offset = strtoul(date+1, &end, 10);
470         int min, hour;
471         int n = end - date - 1;
473         min = offset % 100;
474         hour = offset / 100;
476         /*
477          * Don't accept any random crap.. At least 3 digits, and
478          * a valid minute. We might want to check that the minutes
479          * are divisible by 30 or something too.
480          */
481         if (min < 60 && n > 2) {
482                 offset = hour*60+min;
483                 if (*date == '-')
484                         offset = -offset;
486                 *offp = offset;
487         }
488         return end - date;
491 static int date_string(unsigned long date, int offset, char *buf, int len)
493         int sign = '+';
495         if (offset < 0) {
496                 offset = -offset;
497                 sign = '-';
498         }
499         return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
502 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
503    (i.e. English) day/month names, and it doesn't work correctly with %z. */
504 int parse_date(const char *date, char *result, int maxlen)
506         struct tm tm;
507         int offset, tm_gmt;
508         time_t then;
510         memset(&tm, 0, sizeof(tm));
511         tm.tm_year = -1;
512         tm.tm_mon = -1;
513         tm.tm_mday = -1;
514         tm.tm_isdst = -1;
515         offset = -1;
516         tm_gmt = 0;
518         for (;;) {
519                 int match = 0;
520                 unsigned char c = *date;
522                 /* Stop at end of string or newline */
523                 if (!c || c == '\n')
524                         break;
526                 if (isalpha(c))
527                         match = match_alpha(date, &tm, &offset);
528                 else if (isdigit(c))
529                         match = match_digit(date, &tm, &offset, &tm_gmt);
530                 else if ((c == '-' || c == '+') && isdigit(date[1]))
531                         match = match_tz(date, &offset);
533                 if (!match) {
534                         /* BAD CRAP */
535                         match = 1;
536                 }       
538                 date += match;
539         }
541         /* mktime uses local timezone */
542         then = my_mktime(&tm); 
543         if (offset == -1)
544                 offset = (then - mktime(&tm)) / 60;
546         if (then == -1)
547                 return -1;
549         if (!tm_gmt)
550                 then -= offset * 60;
551         return date_string(then, offset, result, maxlen);
554 void datestamp(char *buf, int bufsize)
556         time_t now;
557         int offset;
559         time(&now);
561         offset = my_mktime(localtime(&now)) - now;
562         offset /= 60;
564         date_string(now, offset, buf, bufsize);
567 static void update_tm(struct tm *tm, unsigned long sec)
569         time_t n = mktime(tm) - sec;
570         localtime_r(&n, tm);
573 static void date_yesterday(struct tm *tm, int *num)
575         update_tm(tm, 24*60*60);
578 static void date_time(struct tm *tm, int hour)
580         if (tm->tm_hour < hour)
581                 date_yesterday(tm, NULL);
582         tm->tm_hour = hour;
583         tm->tm_min = 0;
584         tm->tm_sec = 0;
587 static void date_midnight(struct tm *tm, int *num)
589         date_time(tm, 0);
592 static void date_noon(struct tm *tm, int *num)
594         date_time(tm, 12);
597 static void date_tea(struct tm *tm, int *num)
599         date_time(tm, 17);
602 static void date_pm(struct tm *tm, int *num)
604         int hour, n = *num;
605         *num = 0;
607         hour = tm->tm_hour;
608         if (n) {
609                 hour = n;
610                 tm->tm_min = 0;
611                 tm->tm_sec = 0;
612         }
613         tm->tm_hour = (hour % 12) + 12;
616 static void date_am(struct tm *tm, int *num)
618         int hour, n = *num;
619         *num = 0;
621         hour = tm->tm_hour;
622         if (n) {
623                 hour = n;
624                 tm->tm_min = 0;
625                 tm->tm_sec = 0;
626         }
627         tm->tm_hour = (hour % 12);
630 static const struct special {
631         const char *name;
632         void (*fn)(struct tm *, int *);
633 } special[] = {
634         { "yesterday", date_yesterday },
635         { "noon", date_noon },
636         { "midnight", date_midnight },
637         { "tea", date_tea },
638         { "PM", date_pm },
639         { "AM", date_am },
640         { NULL }
641 };
643 static const char *number_name[] = {
644         "zero", "one", "two", "three", "four",
645         "five", "six", "seven", "eight", "nine", "ten",
646 };
648 static const struct typelen {
649         const char *type;
650         int length;
651 } typelen[] = {
652         { "seconds", 1 },
653         { "minutes", 60 },
654         { "hours", 60*60 },
655         { "days", 24*60*60 },
656         { "weeks", 7*24*60*60 },
657         { NULL }
658 };      
660 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
662         const struct typelen *tl;
663         const struct special *s;
664         const char *end = date;
665         int i;
667         while (isalpha(*++end));
668                 ;
670         for (i = 0; i < 12; i++) {
671                 int match = match_string(date, month_names[i]);
672                 if (match >= 3) {
673                         tm->tm_mon = i;
674                         return end;
675                 }
676         }
678         for (s = special; s->name; s++) {
679                 int len = strlen(s->name);
680                 if (match_string(date, s->name) == len) {
681                         s->fn(tm, num);
682                         return end;
683                 }
684         }
686         if (!*num) {
687                 for (i = 1; i < 11; i++) {
688                         int len = strlen(number_name[i]);
689                         if (match_string(date, number_name[i]) == len) {
690                                 *num = i;
691                                 return end;
692                         }
693                 }
694                 if (match_string(date, "last") == 4)
695                         *num = 1;
696                 return end;
697         }
699         tl = typelen;
700         while (tl->type) {
701                 int len = strlen(tl->type);
702                 if (match_string(date, tl->type) >= len-1) {
703                         update_tm(tm, tl->length * *num);
704                         *num = 0;
705                         return end;
706                 }
707                 tl++;
708         }
710         for (i = 0; i < 7; i++) {
711                 int match = match_string(date, weekday_names[i]);
712                 if (match >= 3) {
713                         int diff, n = *num -1;
714                         *num = 0;
716                         diff = tm->tm_wday - i;
717                         if (diff <= 0)
718                                 n++;
719                         diff += 7*n;
721                         update_tm(tm, diff * 24 * 60 * 60);
722                         return end;
723                 }
724         }
726         if (match_string(date, "months") >= 5) {
727                 int n = tm->tm_mon - *num;
728                 *num = 0;
729                 while (n < 0) {
730                         n += 12;
731                         tm->tm_year--;
732                 }
733                 tm->tm_mon = n;
734                 return end;
735         }
737         if (match_string(date, "years") >= 4) {
738                 tm->tm_year -= *num;
739                 *num = 0;
740                 return end;
741         }
743         return end;
746 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
748         char *end;
749         unsigned long number = strtoul(date, &end, 10);
751         switch (*end) {
752         case ':':
753         case '.':
754         case '/':
755         case '-':
756                 if (isdigit(end[1])) {
757                         int match = match_multi_number(number, *end, date, end, tm);
758                         if (match)
759                                 return date + match;
760                 }
761         }
763         *num = number;
764         return end;
767 unsigned long approxidate(const char *date)
769         int number = 0;
770         struct tm tm, now;
771         struct timeval tv;
772         char buffer[50];
774         if (parse_date(date, buffer, sizeof(buffer)) > 0)
775                 return strtoul(buffer, NULL, 10);
777         gettimeofday(&tv, NULL);
778         localtime_r(&tv.tv_sec, &tm);
779         now = tm;
780         for (;;) {
781                 unsigned char c = *date;
782                 if (!c)
783                         break;
784                 date++;
785                 if (isdigit(c)) {
786                         date = approxidate_digit(date-1, &tm, &number);
787                         continue;
788                 }
789                 if (isalpha(c))
790                         date = approxidate_alpha(date-1, &tm, &number);
791         }
792         if (number > 0 && number < 32)
793                 tm.tm_mday = number;
794         if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
795                 tm.tm_year--;
796         return mktime(&tm);