Support output ISO 8601 format dates
[git/mjg.git] / date.c
blob735d8f3bed88d179f964a6238a800d4914f147da
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
148 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
149 weekday_names[tm->tm_wday],
150 month_names[tm->tm_mon],
151 tm->tm_mday,
152 tm->tm_hour, tm->tm_min, tm->tm_sec,
153 tm->tm_year + 1900,
154 (mode == DATE_LOCAL) ? 0 : ' ',
155 tz);
156 return timebuf;
159 const char *show_rfc2822_date(unsigned long time, int tz)
161 struct tm *tm;
162 static char timebuf[200];
164 tm = time_to_tm(time, tz);
165 if (!tm)
166 return NULL;
167 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
168 weekday_names[tm->tm_wday], tm->tm_mday,
169 month_names[tm->tm_mon], tm->tm_year + 1900,
170 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
171 return timebuf;
175 * Check these. And note how it doesn't do the summer-time conversion.
177 * In my world, it's always summer, and things are probably a bit off
178 * in other ways too.
180 static const struct {
181 const char *name;
182 int offset;
183 int dst;
184 } timezone_names[] = {
185 { "IDLW", -12, 0, }, /* International Date Line West */
186 { "NT", -11, 0, }, /* Nome */
187 { "CAT", -10, 0, }, /* Central Alaska */
188 { "HST", -10, 0, }, /* Hawaii Standard */
189 { "HDT", -10, 1, }, /* Hawaii Daylight */
190 { "YST", -9, 0, }, /* Yukon Standard */
191 { "YDT", -9, 1, }, /* Yukon Daylight */
192 { "PST", -8, 0, }, /* Pacific Standard */
193 { "PDT", -8, 1, }, /* Pacific Daylight */
194 { "MST", -7, 0, }, /* Mountain Standard */
195 { "MDT", -7, 1, }, /* Mountain Daylight */
196 { "CST", -6, 0, }, /* Central Standard */
197 { "CDT", -6, 1, }, /* Central Daylight */
198 { "EST", -5, 0, }, /* Eastern Standard */
199 { "EDT", -5, 1, }, /* Eastern Daylight */
200 { "AST", -3, 0, }, /* Atlantic Standard */
201 { "ADT", -3, 1, }, /* Atlantic Daylight */
202 { "WAT", -1, 0, }, /* West Africa */
204 { "GMT", 0, 0, }, /* Greenwich Mean */
205 { "UTC", 0, 0, }, /* Universal (Coordinated) */
207 { "WET", 0, 0, }, /* Western European */
208 { "BST", 0, 1, }, /* British Summer */
209 { "CET", +1, 0, }, /* Central European */
210 { "MET", +1, 0, }, /* Middle European */
211 { "MEWT", +1, 0, }, /* Middle European Winter */
212 { "MEST", +1, 1, }, /* Middle European Summer */
213 { "CEST", +1, 1, }, /* Central European Summer */
214 { "MESZ", +1, 1, }, /* Middle European Summer */
215 { "FWT", +1, 0, }, /* French Winter */
216 { "FST", +1, 1, }, /* French Summer */
217 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
218 { "EEST", +2, 1, }, /* Eastern European Daylight */
219 { "WAST", +7, 0, }, /* West Australian Standard */
220 { "WADT", +7, 1, }, /* West Australian Daylight */
221 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
222 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
223 { "EAST", +10, 0, }, /* Eastern Australian Standard */
224 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
225 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
226 { "NZT", +11, 0, }, /* New Zealand */
227 { "NZST", +11, 0, }, /* New Zealand Standard */
228 { "NZDT", +11, 1, }, /* New Zealand Daylight */
229 { "IDLE", +12, 0, }, /* International Date Line East */
232 static int match_string(const char *date, const char *str)
234 int i = 0;
236 for (i = 0; *date; date++, str++, i++) {
237 if (*date == *str)
238 continue;
239 if (toupper(*date) == toupper(*str))
240 continue;
241 if (!isalnum(*date))
242 break;
243 return 0;
245 return i;
248 static int skip_alpha(const char *date)
250 int i = 0;
251 do {
252 i++;
253 } while (isalpha(date[i]));
254 return i;
258 * Parse month, weekday, or timezone name
260 static int match_alpha(const char *date, struct tm *tm, int *offset)
262 int i;
264 for (i = 0; i < 12; i++) {
265 int match = match_string(date, month_names[i]);
266 if (match >= 3) {
267 tm->tm_mon = i;
268 return match;
272 for (i = 0; i < 7; i++) {
273 int match = match_string(date, weekday_names[i]);
274 if (match >= 3) {
275 tm->tm_wday = i;
276 return match;
280 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
281 int match = match_string(date, timezone_names[i].name);
282 if (match >= 3) {
283 int off = timezone_names[i].offset;
285 /* This is bogus, but we like summer */
286 off += timezone_names[i].dst;
288 /* Only use the tz name offset if we don't have anything better */
289 if (*offset == -1)
290 *offset = 60*off;
292 return match;
296 if (match_string(date, "PM") == 2) {
297 tm->tm_hour = (tm->tm_hour % 12) + 12;
298 return 2;
301 if (match_string(date, "AM") == 2) {
302 tm->tm_hour = (tm->tm_hour % 12) + 0;
303 return 2;
306 /* BAD CRAP */
307 return skip_alpha(date);
310 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
312 if (month > 0 && month < 13 && day > 0 && day < 32) {
313 struct tm check = *tm;
314 struct tm *r = (now_tm ? &check : tm);
315 time_t specified;
317 r->tm_mon = month - 1;
318 r->tm_mday = day;
319 if (year == -1) {
320 if (!now_tm)
321 return 1;
322 r->tm_year = now_tm->tm_year;
324 else if (year >= 1970 && year < 2100)
325 r->tm_year = year - 1900;
326 else if (year > 70 && year < 100)
327 r->tm_year = year;
328 else if (year < 38)
329 r->tm_year = year + 100;
330 else
331 return 0;
332 if (!now_tm)
333 return 1;
335 specified = my_mktime(r);
337 /* Be it commit time or author time, it does not make
338 * sense to specify timestamp way into the future. Make
339 * sure it is not later than ten days from now...
341 if (now + 10*24*3600 < specified)
342 return 0;
343 tm->tm_mon = r->tm_mon;
344 tm->tm_mday = r->tm_mday;
345 if (year != -1)
346 tm->tm_year = r->tm_year;
347 return 1;
349 return 0;
352 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
354 time_t now;
355 struct tm now_tm;
356 struct tm *refuse_future;
357 long num2, num3;
359 num2 = strtol(end+1, &end, 10);
360 num3 = -1;
361 if (*end == c && isdigit(end[1]))
362 num3 = strtol(end+1, &end, 10);
364 /* Time? Date? */
365 switch (c) {
366 case ':':
367 if (num3 < 0)
368 num3 = 0;
369 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
370 tm->tm_hour = num;
371 tm->tm_min = num2;
372 tm->tm_sec = num3;
373 break;
375 return 0;
377 case '-':
378 case '/':
379 case '.':
380 now = time(NULL);
381 refuse_future = NULL;
382 if (gmtime_r(&now, &now_tm))
383 refuse_future = &now_tm;
385 if (num > 70) {
386 /* yyyy-mm-dd? */
387 if (is_date(num, num2, num3, refuse_future, now, tm))
388 break;
389 /* yyyy-dd-mm? */
390 if (is_date(num, num3, num2, refuse_future, now, tm))
391 break;
393 /* Our eastern European friends say dd.mm.yy[yy]
394 * is the norm there, so giving precedence to
395 * mm/dd/yy[yy] form only when separator is not '.'
397 if (c != '.' &&
398 is_date(num3, num, num2, refuse_future, now, tm))
399 break;
400 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
401 if (is_date(num3, num2, num, refuse_future, now, tm))
402 break;
403 /* Funny European mm.dd.yy */
404 if (c == '.' &&
405 is_date(num3, num, num2, refuse_future, now, tm))
406 break;
407 return 0;
409 return end - date;
413 * We've seen a digit. Time? Year? Date?
415 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
417 int n;
418 char *end;
419 unsigned long num;
421 num = strtoul(date, &end, 10);
424 * Seconds since 1970? We trigger on that for any numbers with
425 * more than 8 digits. This is because we don't want to rule out
426 * numbers like 20070606 as a YYYYMMDD date.
428 if (num >= 100000000) {
429 time_t time = num;
430 if (gmtime_r(&time, tm)) {
431 *tm_gmt = 1;
432 return end - date;
437 * Check for special formats: num[-.:/]num[same]num
439 switch (*end) {
440 case ':':
441 case '.':
442 case '/':
443 case '-':
444 if (isdigit(end[1])) {
445 int match = match_multi_number(num, *end, date, end, tm);
446 if (match)
447 return match;
452 * None of the special formats? Try to guess what
453 * the number meant. We use the number of digits
454 * to make a more educated guess..
456 n = 0;
457 do {
458 n++;
459 } while (isdigit(date[n]));
461 /* Four-digit year or a timezone? */
462 if (n == 4) {
463 if (num <= 1400 && *offset == -1) {
464 unsigned int minutes = num % 100;
465 unsigned int hours = num / 100;
466 *offset = hours*60 + minutes;
467 } else if (num > 1900 && num < 2100)
468 tm->tm_year = num - 1900;
469 return n;
473 * NOTE! We will give precedence to day-of-month over month or
474 * year numbers in the 1-12 range. So 05 is always "mday 5",
475 * unless we already have a mday..
477 * IOW, 01 Apr 05 parses as "April 1st, 2005".
479 if (num > 0 && num < 32 && tm->tm_mday < 0) {
480 tm->tm_mday = num;
481 return n;
484 /* Two-digit year? */
485 if (n == 2 && tm->tm_year < 0) {
486 if (num < 10 && tm->tm_mday >= 0) {
487 tm->tm_year = num + 100;
488 return n;
490 if (num >= 70) {
491 tm->tm_year = num;
492 return n;
496 if (num > 0 && num < 32) {
497 tm->tm_mday = num;
498 } else if (num > 1900) {
499 tm->tm_year = num - 1900;
500 } else if (num > 70) {
501 tm->tm_year = num;
502 } else if (num > 0 && num < 13) {
503 tm->tm_mon = num-1;
506 return n;
509 static int match_tz(const char *date, int *offp)
511 char *end;
512 int offset = strtoul(date+1, &end, 10);
513 int min, hour;
514 int n = end - date - 1;
516 min = offset % 100;
517 hour = offset / 100;
520 * Don't accept any random crap.. At least 3 digits, and
521 * a valid minute. We might want to check that the minutes
522 * are divisible by 30 or something too.
524 if (min < 60 && n > 2) {
525 offset = hour*60+min;
526 if (*date == '-')
527 offset = -offset;
529 *offp = offset;
531 return end - date;
534 static int date_string(unsigned long date, int offset, char *buf, int len)
536 int sign = '+';
538 if (offset < 0) {
539 offset = -offset;
540 sign = '-';
542 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
545 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
546 (i.e. English) day/month names, and it doesn't work correctly with %z. */
547 int parse_date(const char *date, char *result, int maxlen)
549 struct tm tm;
550 int offset, tm_gmt;
551 time_t then;
553 memset(&tm, 0, sizeof(tm));
554 tm.tm_year = -1;
555 tm.tm_mon = -1;
556 tm.tm_mday = -1;
557 tm.tm_isdst = -1;
558 offset = -1;
559 tm_gmt = 0;
561 for (;;) {
562 int match = 0;
563 unsigned char c = *date;
565 /* Stop at end of string or newline */
566 if (!c || c == '\n')
567 break;
569 if (isalpha(c))
570 match = match_alpha(date, &tm, &offset);
571 else if (isdigit(c))
572 match = match_digit(date, &tm, &offset, &tm_gmt);
573 else if ((c == '-' || c == '+') && isdigit(date[1]))
574 match = match_tz(date, &offset);
576 if (!match) {
577 /* BAD CRAP */
578 match = 1;
581 date += match;
584 /* mktime uses local timezone */
585 then = my_mktime(&tm);
586 if (offset == -1)
587 offset = (then - mktime(&tm)) / 60;
589 if (then == -1)
590 return -1;
592 if (!tm_gmt)
593 then -= offset * 60;
594 return date_string(then, offset, result, maxlen);
597 void datestamp(char *buf, int bufsize)
599 time_t now;
600 int offset;
602 time(&now);
604 offset = my_mktime(localtime(&now)) - now;
605 offset /= 60;
607 date_string(now, offset, buf, bufsize);
610 static void update_tm(struct tm *tm, unsigned long sec)
612 time_t n = mktime(tm) - sec;
613 localtime_r(&n, tm);
616 static void date_yesterday(struct tm *tm, int *num)
618 update_tm(tm, 24*60*60);
621 static void date_time(struct tm *tm, int hour)
623 if (tm->tm_hour < hour)
624 date_yesterday(tm, NULL);
625 tm->tm_hour = hour;
626 tm->tm_min = 0;
627 tm->tm_sec = 0;
630 static void date_midnight(struct tm *tm, int *num)
632 date_time(tm, 0);
635 static void date_noon(struct tm *tm, int *num)
637 date_time(tm, 12);
640 static void date_tea(struct tm *tm, int *num)
642 date_time(tm, 17);
645 static void date_pm(struct tm *tm, int *num)
647 int hour, n = *num;
648 *num = 0;
650 hour = tm->tm_hour;
651 if (n) {
652 hour = n;
653 tm->tm_min = 0;
654 tm->tm_sec = 0;
656 tm->tm_hour = (hour % 12) + 12;
659 static void date_am(struct tm *tm, int *num)
661 int hour, n = *num;
662 *num = 0;
664 hour = tm->tm_hour;
665 if (n) {
666 hour = n;
667 tm->tm_min = 0;
668 tm->tm_sec = 0;
670 tm->tm_hour = (hour % 12);
673 static const struct special {
674 const char *name;
675 void (*fn)(struct tm *, int *);
676 } special[] = {
677 { "yesterday", date_yesterday },
678 { "noon", date_noon },
679 { "midnight", date_midnight },
680 { "tea", date_tea },
681 { "PM", date_pm },
682 { "AM", date_am },
683 { NULL }
686 static const char *number_name[] = {
687 "zero", "one", "two", "three", "four",
688 "five", "six", "seven", "eight", "nine", "ten",
691 static const struct typelen {
692 const char *type;
693 int length;
694 } typelen[] = {
695 { "seconds", 1 },
696 { "minutes", 60 },
697 { "hours", 60*60 },
698 { "days", 24*60*60 },
699 { "weeks", 7*24*60*60 },
700 { NULL }
703 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
705 const struct typelen *tl;
706 const struct special *s;
707 const char *end = date;
708 int i;
710 while (isalpha(*++end));
713 for (i = 0; i < 12; i++) {
714 int match = match_string(date, month_names[i]);
715 if (match >= 3) {
716 tm->tm_mon = i;
717 return end;
721 for (s = special; s->name; s++) {
722 int len = strlen(s->name);
723 if (match_string(date, s->name) == len) {
724 s->fn(tm, num);
725 return end;
729 if (!*num) {
730 for (i = 1; i < 11; i++) {
731 int len = strlen(number_name[i]);
732 if (match_string(date, number_name[i]) == len) {
733 *num = i;
734 return end;
737 if (match_string(date, "last") == 4)
738 *num = 1;
739 return end;
742 tl = typelen;
743 while (tl->type) {
744 int len = strlen(tl->type);
745 if (match_string(date, tl->type) >= len-1) {
746 update_tm(tm, tl->length * *num);
747 *num = 0;
748 return end;
750 tl++;
753 for (i = 0; i < 7; i++) {
754 int match = match_string(date, weekday_names[i]);
755 if (match >= 3) {
756 int diff, n = *num -1;
757 *num = 0;
759 diff = tm->tm_wday - i;
760 if (diff <= 0)
761 n++;
762 diff += 7*n;
764 update_tm(tm, diff * 24 * 60 * 60);
765 return end;
769 if (match_string(date, "months") >= 5) {
770 int n = tm->tm_mon - *num;
771 *num = 0;
772 while (n < 0) {
773 n += 12;
774 tm->tm_year--;
776 tm->tm_mon = n;
777 return end;
780 if (match_string(date, "years") >= 4) {
781 tm->tm_year -= *num;
782 *num = 0;
783 return end;
786 return end;
789 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
791 char *end;
792 unsigned long number = strtoul(date, &end, 10);
794 switch (*end) {
795 case ':':
796 case '.':
797 case '/':
798 case '-':
799 if (isdigit(end[1])) {
800 int match = match_multi_number(number, *end, date, end, tm);
801 if (match)
802 return date + match;
806 *num = number;
807 return end;
810 unsigned long approxidate(const char *date)
812 int number = 0;
813 struct tm tm, now;
814 struct timeval tv;
815 char buffer[50];
817 if (parse_date(date, buffer, sizeof(buffer)) > 0)
818 return strtoul(buffer, NULL, 10);
820 gettimeofday(&tv, NULL);
821 localtime_r(&tv.tv_sec, &tm);
822 now = tm;
823 for (;;) {
824 unsigned char c = *date;
825 if (!c)
826 break;
827 date++;
828 if (isdigit(c)) {
829 date = approxidate_digit(date-1, &tm, &number);
830 continue;
832 if (isalpha(c))
833 date = approxidate_alpha(date-1, &tm, &number);
835 if (number > 0 && number < 32)
836 tm.tm_mday = number;
837 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
838 tm.tm_year--;
839 return mktime(&tm);