log_ref_write() -- do not chomp reflog message at the first LF
[git/repo.git] / date.c
blob93bef6efbe38cb8983fdda14b75ce772f90e1b6a
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", +11, 0, }, /* New Zealand */
217 { "NZST", +11, 0, }, /* New Zealand Standard */
218 { "NZDT", +11, 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 void datestamp(char *buf, int bufsize)
589 time_t now;
590 int offset;
592 time(&now);
594 offset = my_mktime(localtime(&now)) - now;
595 offset /= 60;
597 date_string(now, offset, buf, bufsize);
600 static void update_tm(struct tm *tm, unsigned long sec)
602 time_t n = mktime(tm) - sec;
603 localtime_r(&n, tm);
606 static void date_yesterday(struct tm *tm, int *num)
608 update_tm(tm, 24*60*60);
611 static void date_time(struct tm *tm, int hour)
613 if (tm->tm_hour < hour)
614 date_yesterday(tm, NULL);
615 tm->tm_hour = hour;
616 tm->tm_min = 0;
617 tm->tm_sec = 0;
620 static void date_midnight(struct tm *tm, int *num)
622 date_time(tm, 0);
625 static void date_noon(struct tm *tm, int *num)
627 date_time(tm, 12);
630 static void date_tea(struct tm *tm, int *num)
632 date_time(tm, 17);
635 static void date_pm(struct tm *tm, int *num)
637 int hour, n = *num;
638 *num = 0;
640 hour = tm->tm_hour;
641 if (n) {
642 hour = n;
643 tm->tm_min = 0;
644 tm->tm_sec = 0;
646 tm->tm_hour = (hour % 12) + 12;
649 static void date_am(struct tm *tm, int *num)
651 int hour, n = *num;
652 *num = 0;
654 hour = tm->tm_hour;
655 if (n) {
656 hour = n;
657 tm->tm_min = 0;
658 tm->tm_sec = 0;
660 tm->tm_hour = (hour % 12);
663 static void date_never(struct tm *tm, int *num)
665 tm->tm_mon = tm->tm_wday = tm->tm_yday
666 = tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
667 tm->tm_year = 70;
668 tm->tm_mday = 1;
671 static const struct special {
672 const char *name;
673 void (*fn)(struct tm *, int *);
674 } special[] = {
675 { "yesterday", date_yesterday },
676 { "noon", date_noon },
677 { "midnight", date_midnight },
678 { "tea", date_tea },
679 { "PM", date_pm },
680 { "AM", date_am },
681 { "never", date_never },
682 { NULL }
685 static const char *number_name[] = {
686 "zero", "one", "two", "three", "four",
687 "five", "six", "seven", "eight", "nine", "ten",
690 static const struct typelen {
691 const char *type;
692 int length;
693 } typelen[] = {
694 { "seconds", 1 },
695 { "minutes", 60 },
696 { "hours", 60*60 },
697 { "days", 24*60*60 },
698 { "weeks", 7*24*60*60 },
699 { NULL }
702 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
704 const struct typelen *tl;
705 const struct special *s;
706 const char *end = date;
707 int i;
709 while (isalpha(*++end));
712 for (i = 0; i < 12; i++) {
713 int match = match_string(date, month_names[i]);
714 if (match >= 3) {
715 tm->tm_mon = i;
716 return end;
720 for (s = special; s->name; s++) {
721 int len = strlen(s->name);
722 if (match_string(date, s->name) == len) {
723 s->fn(tm, num);
724 return end;
728 if (!*num) {
729 for (i = 1; i < 11; i++) {
730 int len = strlen(number_name[i]);
731 if (match_string(date, number_name[i]) == len) {
732 *num = i;
733 return end;
736 if (match_string(date, "last") == 4)
737 *num = 1;
738 return end;
741 tl = typelen;
742 while (tl->type) {
743 int len = strlen(tl->type);
744 if (match_string(date, tl->type) >= len-1) {
745 update_tm(tm, tl->length * *num);
746 *num = 0;
747 return end;
749 tl++;
752 for (i = 0; i < 7; i++) {
753 int match = match_string(date, weekday_names[i]);
754 if (match >= 3) {
755 int diff, n = *num -1;
756 *num = 0;
758 diff = tm->tm_wday - i;
759 if (diff <= 0)
760 n++;
761 diff += 7*n;
763 update_tm(tm, diff * 24 * 60 * 60);
764 return end;
768 if (match_string(date, "months") >= 5) {
769 int n = tm->tm_mon - *num;
770 *num = 0;
771 while (n < 0) {
772 n += 12;
773 tm->tm_year--;
775 tm->tm_mon = n;
776 return end;
779 if (match_string(date, "years") >= 4) {
780 tm->tm_year -= *num;
781 *num = 0;
782 return end;
785 return end;
788 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
790 char *end;
791 unsigned long number = strtoul(date, &end, 10);
793 switch (*end) {
794 case ':':
795 case '.':
796 case '/':
797 case '-':
798 if (isdigit(end[1])) {
799 int match = match_multi_number(number, *end, date, end, tm);
800 if (match)
801 return date + match;
805 *num = number;
806 return end;
809 unsigned long approxidate(const char *date)
811 int number = 0;
812 struct tm tm, now;
813 struct timeval tv;
814 char buffer[50];
816 if (parse_date(date, buffer, sizeof(buffer)) > 0)
817 return strtoul(buffer, NULL, 10);
819 gettimeofday(&tv, NULL);
820 localtime_r(&tv.tv_sec, &tm);
821 now = tm;
822 for (;;) {
823 unsigned char c = *date;
824 if (!c)
825 break;
826 date++;
827 if (isdigit(c)) {
828 date = approxidate_digit(date-1, &tm, &number);
829 continue;
831 if (isalpha(c))
832 date = approxidate_alpha(date-1, &tm, &number);
834 if (number > 0 && number < 32)
835 tm.tm_mday = number;
836 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
837 tm.tm_year--;
838 return mktime(&tm);