Remove uneeded #include
[alt-git.git] / date.c
blobe387dcd3976d8e12afdce5d7600ca0022f4ca835
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"
40 static time_t gm_time_t(unsigned long time, int tz)
42 int minutes;
44 minutes = tz < 0 ? -tz : tz;
45 minutes = (minutes / 100)*60 + (minutes % 100);
46 minutes = tz < 0 ? -minutes : minutes;
47 return time + minutes * 60;
51 * The "tz" thing is passed in as this strange "decimal parse of tz"
52 * thing, which means that tz -0100 is passed in as the integer -100,
53 * even though it means "sixty minutes off"
55 static struct tm *time_to_tm(unsigned long time, int tz)
57 time_t t = gm_time_t(time, tz);
58 return gmtime(&t);
61 const char *show_date(unsigned long time, int tz, int relative)
63 struct tm *tm;
64 static char timebuf[200];
66 if (relative) {
67 unsigned long diff;
68 time_t t = gm_time_t(time, tz);
69 struct timeval now;
70 gettimeofday(&now, NULL);
71 if (now.tv_sec < t)
72 return "in the future";
73 diff = now.tv_sec - t;
74 if (diff < 90) {
75 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
76 return timebuf;
78 /* Turn it into minutes */
79 diff = (diff + 30) / 60;
80 if (diff < 90) {
81 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
82 return timebuf;
84 /* Turn it into hours */
85 diff = (diff + 30) / 60;
86 if (diff < 36) {
87 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
88 return timebuf;
90 /* We deal with number of days from here on */
91 diff = (diff + 12) / 24;
92 if (diff < 14) {
93 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
94 return timebuf;
96 /* Say weeks for the past 10 weeks or so */
97 if (diff < 70) {
98 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
99 return timebuf;
101 /* Say months for the past 12 months or so */
102 if (diff < 360) {
103 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
104 return timebuf;
106 /* Else fall back on absolute format.. */
109 tm = time_to_tm(time, tz);
110 if (!tm)
111 return NULL;
112 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
113 weekday_names[tm->tm_wday],
114 month_names[tm->tm_mon],
115 tm->tm_mday,
116 tm->tm_hour, tm->tm_min, tm->tm_sec,
117 tm->tm_year + 1900, tz);
118 return timebuf;
121 const char *show_rfc2822_date(unsigned long time, int tz)
123 struct tm *tm;
124 static char timebuf[200];
126 tm = time_to_tm(time, tz);
127 if (!tm)
128 return NULL;
129 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
130 weekday_names[tm->tm_wday], tm->tm_mday,
131 month_names[tm->tm_mon], tm->tm_year + 1900,
132 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
133 return timebuf;
137 * Check these. And note how it doesn't do the summer-time conversion.
139 * In my world, it's always summer, and things are probably a bit off
140 * in other ways too.
142 static const struct {
143 const char *name;
144 int offset;
145 int dst;
146 } timezone_names[] = {
147 { "IDLW", -12, 0, }, /* International Date Line West */
148 { "NT", -11, 0, }, /* Nome */
149 { "CAT", -10, 0, }, /* Central Alaska */
150 { "HST", -10, 0, }, /* Hawaii Standard */
151 { "HDT", -10, 1, }, /* Hawaii Daylight */
152 { "YST", -9, 0, }, /* Yukon Standard */
153 { "YDT", -9, 1, }, /* Yukon Daylight */
154 { "PST", -8, 0, }, /* Pacific Standard */
155 { "PDT", -8, 1, }, /* Pacific Daylight */
156 { "MST", -7, 0, }, /* Mountain Standard */
157 { "MDT", -7, 1, }, /* Mountain Daylight */
158 { "CST", -6, 0, }, /* Central Standard */
159 { "CDT", -6, 1, }, /* Central Daylight */
160 { "EST", -5, 0, }, /* Eastern Standard */
161 { "EDT", -5, 1, }, /* Eastern Daylight */
162 { "AST", -3, 0, }, /* Atlantic Standard */
163 { "ADT", -3, 1, }, /* Atlantic Daylight */
164 { "WAT", -1, 0, }, /* West Africa */
166 { "GMT", 0, 0, }, /* Greenwich Mean */
167 { "UTC", 0, 0, }, /* Universal (Coordinated) */
169 { "WET", 0, 0, }, /* Western European */
170 { "BST", 0, 1, }, /* British Summer */
171 { "CET", +1, 0, }, /* Central European */
172 { "MET", +1, 0, }, /* Middle European */
173 { "MEWT", +1, 0, }, /* Middle European Winter */
174 { "MEST", +1, 1, }, /* Middle European Summer */
175 { "CEST", +1, 1, }, /* Central European Summer */
176 { "MESZ", +1, 1, }, /* Middle European Summer */
177 { "FWT", +1, 0, }, /* French Winter */
178 { "FST", +1, 1, }, /* French Summer */
179 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
180 { "EEST", +2, 1, }, /* Eastern European Daylight */
181 { "WAST", +7, 0, }, /* West Australian Standard */
182 { "WADT", +7, 1, }, /* West Australian Daylight */
183 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
184 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
185 { "EAST", +10, 0, }, /* Eastern Australian Standard */
186 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
187 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
188 { "NZT", +11, 0, }, /* New Zealand */
189 { "NZST", +11, 0, }, /* New Zealand Standard */
190 { "NZDT", +11, 1, }, /* New Zealand Daylight */
191 { "IDLE", +12, 0, }, /* International Date Line East */
194 static int match_string(const char *date, const char *str)
196 int i = 0;
198 for (i = 0; *date; date++, str++, i++) {
199 if (*date == *str)
200 continue;
201 if (toupper(*date) == toupper(*str))
202 continue;
203 if (!isalnum(*date))
204 break;
205 return 0;
207 return i;
210 static int skip_alpha(const char *date)
212 int i = 0;
213 do {
214 i++;
215 } while (isalpha(date[i]));
216 return i;
220 * Parse month, weekday, or timezone name
222 static int match_alpha(const char *date, struct tm *tm, int *offset)
224 int i;
226 for (i = 0; i < 12; i++) {
227 int match = match_string(date, month_names[i]);
228 if (match >= 3) {
229 tm->tm_mon = i;
230 return match;
234 for (i = 0; i < 7; i++) {
235 int match = match_string(date, weekday_names[i]);
236 if (match >= 3) {
237 tm->tm_wday = i;
238 return match;
242 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
243 int match = match_string(date, timezone_names[i].name);
244 if (match >= 3) {
245 int off = timezone_names[i].offset;
247 /* This is bogus, but we like summer */
248 off += timezone_names[i].dst;
250 /* Only use the tz name offset if we don't have anything better */
251 if (*offset == -1)
252 *offset = 60*off;
254 return match;
258 if (match_string(date, "PM") == 2) {
259 if (tm->tm_hour > 0 && tm->tm_hour < 12)
260 tm->tm_hour += 12;
261 return 2;
264 /* BAD CRAP */
265 return skip_alpha(date);
268 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
270 if (month > 0 && month < 13 && day > 0 && day < 32) {
271 struct tm check = *tm;
272 struct tm *r = (now_tm ? &check : tm);
273 time_t specified;
275 r->tm_mon = month - 1;
276 r->tm_mday = day;
277 if (year == -1) {
278 if (!now_tm)
279 return 1;
280 r->tm_year = now_tm->tm_year;
282 else if (year >= 1970 && year < 2100)
283 r->tm_year = year - 1900;
284 else if (year > 70 && year < 100)
285 r->tm_year = year;
286 else if (year < 38)
287 r->tm_year = year + 100;
288 else
289 return 0;
290 if (!now_tm)
291 return 1;
293 specified = my_mktime(r);
295 /* Be it commit time or author time, it does not make
296 * sense to specify timestamp way into the future. Make
297 * sure it is not later than ten days from now...
299 if (now + 10*24*3600 < specified)
300 return 0;
301 tm->tm_mon = r->tm_mon;
302 tm->tm_mday = r->tm_mday;
303 if (year != -1)
304 tm->tm_year = r->tm_year;
305 return 1;
307 return 0;
310 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
312 time_t now;
313 struct tm now_tm;
314 struct tm *refuse_future;
315 long num2, num3;
317 num2 = strtol(end+1, &end, 10);
318 num3 = -1;
319 if (*end == c && isdigit(end[1]))
320 num3 = strtol(end+1, &end, 10);
322 /* Time? Date? */
323 switch (c) {
324 case ':':
325 if (num3 < 0)
326 num3 = 0;
327 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
328 tm->tm_hour = num;
329 tm->tm_min = num2;
330 tm->tm_sec = num3;
331 break;
333 return 0;
335 case '-':
336 case '/':
337 case '.':
338 now = time(NULL);
339 refuse_future = NULL;
340 if (gmtime_r(&now, &now_tm))
341 refuse_future = &now_tm;
343 if (num > 70) {
344 /* yyyy-mm-dd? */
345 if (is_date(num, num2, num3, refuse_future, now, tm))
346 break;
347 /* yyyy-dd-mm? */
348 if (is_date(num, num3, num2, refuse_future, now, tm))
349 break;
351 /* Our eastern European friends say dd.mm.yy[yy]
352 * is the norm there, so giving precedence to
353 * mm/dd/yy[yy] form only when separator is not '.'
355 if (c != '.' &&
356 is_date(num3, num, num2, refuse_future, now, tm))
357 break;
358 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
359 if (is_date(num3, num2, num, refuse_future, now, tm))
360 break;
361 /* Funny European mm.dd.yy */
362 if (c == '.' &&
363 is_date(num3, num, num2, refuse_future, now, tm))
364 break;
365 return 0;
367 return end - date;
371 * We've seen a digit. Time? Year? Date?
373 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
375 int n;
376 char *end;
377 unsigned long num;
379 num = strtoul(date, &end, 10);
382 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
384 if (num > 946684800) {
385 time_t time = num;
386 if (gmtime_r(&time, tm)) {
387 *tm_gmt = 1;
388 return end - date;
393 * Check for special formats: num[-.:/]num[same]num
395 switch (*end) {
396 case ':':
397 case '.':
398 case '/':
399 case '-':
400 if (isdigit(end[1])) {
401 int match = match_multi_number(num, *end, date, end, tm);
402 if (match)
403 return match;
408 * None of the special formats? Try to guess what
409 * the number meant. We use the number of digits
410 * to make a more educated guess..
412 n = 0;
413 do {
414 n++;
415 } while (isdigit(date[n]));
417 /* Four-digit year or a timezone? */
418 if (n == 4) {
419 if (num <= 1400 && *offset == -1) {
420 unsigned int minutes = num % 100;
421 unsigned int hours = num / 100;
422 *offset = hours*60 + minutes;
423 } else if (num > 1900 && num < 2100)
424 tm->tm_year = num - 1900;
425 return n;
429 * NOTE! We will give precedence to day-of-month over month or
430 * year numbers in the 1-12 range. So 05 is always "mday 5",
431 * unless we already have a mday..
433 * IOW, 01 Apr 05 parses as "April 1st, 2005".
435 if (num > 0 && num < 32 && tm->tm_mday < 0) {
436 tm->tm_mday = num;
437 return n;
440 /* Two-digit year? */
441 if (n == 2 && tm->tm_year < 0) {
442 if (num < 10 && tm->tm_mday >= 0) {
443 tm->tm_year = num + 100;
444 return n;
446 if (num >= 70) {
447 tm->tm_year = num;
448 return n;
452 if (num > 0 && num < 32) {
453 tm->tm_mday = num;
454 } else if (num > 1900) {
455 tm->tm_year = num - 1900;
456 } else if (num > 70) {
457 tm->tm_year = num;
458 } else if (num > 0 && num < 13) {
459 tm->tm_mon = num-1;
462 return n;
465 static int match_tz(const char *date, int *offp)
467 char *end;
468 int offset = strtoul(date+1, &end, 10);
469 int min, hour;
470 int n = end - date - 1;
472 min = offset % 100;
473 hour = offset / 100;
476 * Don't accept any random crap.. At least 3 digits, and
477 * a valid minute. We might want to check that the minutes
478 * are divisible by 30 or something too.
480 if (min < 60 && n > 2) {
481 offset = hour*60+min;
482 if (*date == '-')
483 offset = -offset;
485 *offp = offset;
487 return end - date;
490 static int date_string(unsigned long date, int offset, char *buf, int len)
492 int sign = '+';
494 if (offset < 0) {
495 offset = -offset;
496 sign = '-';
498 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
501 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
502 (i.e. English) day/month names, and it doesn't work correctly with %z. */
503 int parse_date(const char *date, char *result, int maxlen)
505 struct tm tm;
506 int offset, tm_gmt;
507 time_t then;
509 memset(&tm, 0, sizeof(tm));
510 tm.tm_year = -1;
511 tm.tm_mon = -1;
512 tm.tm_mday = -1;
513 tm.tm_isdst = -1;
514 offset = -1;
515 tm_gmt = 0;
517 for (;;) {
518 int match = 0;
519 unsigned char c = *date;
521 /* Stop at end of string or newline */
522 if (!c || c == '\n')
523 break;
525 if (isalpha(c))
526 match = match_alpha(date, &tm, &offset);
527 else if (isdigit(c))
528 match = match_digit(date, &tm, &offset, &tm_gmt);
529 else if ((c == '-' || c == '+') && isdigit(date[1]))
530 match = match_tz(date, &offset);
532 if (!match) {
533 /* BAD CRAP */
534 match = 1;
537 date += match;
540 /* mktime uses local timezone */
541 then = my_mktime(&tm);
542 if (offset == -1)
543 offset = (then - mktime(&tm)) / 60;
545 if (then == -1)
546 return -1;
548 if (!tm_gmt)
549 then -= offset * 60;
550 return date_string(then, offset, result, maxlen);
553 void datestamp(char *buf, int bufsize)
555 time_t now;
556 int offset;
558 time(&now);
560 offset = my_mktime(localtime(&now)) - now;
561 offset /= 60;
563 date_string(now, offset, buf, bufsize);
566 static void update_tm(struct tm *tm, unsigned long sec)
568 time_t n = mktime(tm) - sec;
569 localtime_r(&n, tm);
572 static void date_yesterday(struct tm *tm, int *num)
574 update_tm(tm, 24*60*60);
577 static void date_time(struct tm *tm, int hour)
579 if (tm->tm_hour < hour)
580 date_yesterday(tm, NULL);
581 tm->tm_hour = hour;
582 tm->tm_min = 0;
583 tm->tm_sec = 0;
586 static void date_midnight(struct tm *tm, int *num)
588 date_time(tm, 0);
591 static void date_noon(struct tm *tm, int *num)
593 date_time(tm, 12);
596 static void date_tea(struct tm *tm, int *num)
598 date_time(tm, 17);
601 static const struct special {
602 const char *name;
603 void (*fn)(struct tm *, int *);
604 } special[] = {
605 { "yesterday", date_yesterday },
606 { "noon", date_noon },
607 { "midnight", date_midnight },
608 { "tea", date_tea },
609 { NULL }
612 static const char *number_name[] = {
613 "zero", "one", "two", "three", "four",
614 "five", "six", "seven", "eight", "nine", "ten",
617 static const struct typelen {
618 const char *type;
619 int length;
620 } typelen[] = {
621 { "seconds", 1 },
622 { "minutes", 60 },
623 { "hours", 60*60 },
624 { "days", 24*60*60 },
625 { "weeks", 7*24*60*60 },
626 { NULL }
629 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
631 const struct typelen *tl;
632 const struct special *s;
633 const char *end = date;
634 int i;
636 while (isalpha(*++end));
639 for (i = 0; i < 12; i++) {
640 int match = match_string(date, month_names[i]);
641 if (match >= 3) {
642 tm->tm_mon = i;
643 return end;
647 for (s = special; s->name; s++) {
648 int len = strlen(s->name);
649 if (match_string(date, s->name) == len) {
650 s->fn(tm, num);
651 return end;
655 if (!*num) {
656 for (i = 1; i < 11; i++) {
657 int len = strlen(number_name[i]);
658 if (match_string(date, number_name[i]) == len) {
659 *num = i;
660 return end;
663 if (match_string(date, "last") == 4)
664 *num = 1;
665 return end;
668 tl = typelen;
669 while (tl->type) {
670 int len = strlen(tl->type);
671 if (match_string(date, tl->type) >= len-1) {
672 update_tm(tm, tl->length * *num);
673 *num = 0;
674 return end;
676 tl++;
679 for (i = 0; i < 7; i++) {
680 int match = match_string(date, weekday_names[i]);
681 if (match >= 3) {
682 int diff, n = *num -1;
683 *num = 0;
685 diff = tm->tm_wday - i;
686 if (diff <= 0)
687 n++;
688 diff += 7*n;
690 update_tm(tm, diff * 24 * 60 * 60);
691 return end;
695 if (match_string(date, "months") >= 5) {
696 int n = tm->tm_mon - *num;
697 *num = 0;
698 while (n < 0) {
699 n += 12;
700 tm->tm_year--;
702 tm->tm_mon = n;
703 return end;
706 if (match_string(date, "years") >= 4) {
707 tm->tm_year -= *num;
708 *num = 0;
709 return end;
712 return end;
715 unsigned long approxidate(const char *date)
717 int number = 0;
718 struct tm tm, now;
719 struct timeval tv;
720 char buffer[50];
722 if (parse_date(date, buffer, sizeof(buffer)) > 0)
723 return strtoul(buffer, NULL, 10);
725 gettimeofday(&tv, NULL);
726 localtime_r(&tv.tv_sec, &tm);
727 now = tm;
728 for (;;) {
729 unsigned char c = *date;
730 if (!c)
731 break;
732 date++;
733 if (isdigit(c)) {
734 char *end;
735 number = strtoul(date-1, &end, 10);
736 date = end;
737 continue;
739 if (isalpha(c))
740 date = approxidate_alpha(date-1, &tm, &number);
742 if (number > 0 && number < 32)
743 tm.tm_mday = number;
744 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
745 tm.tm_year--;
746 return mktime(&tm);