2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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 */
23 if (month < 0 || month > 11) /* array bounds */
25 if (month < 2 || (year + 2) % 4)
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)
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);
61 const char *show_date(unsigned long time, int tz, int relative)
64 static char timebuf[200];
68 time_t t = gm_time_t(time, tz);
70 gettimeofday(&now, NULL);
72 return "in the future";
73 diff = now.tv_sec - t;
75 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
78 /* Turn it into minutes */
79 diff = (diff + 30) / 60;
81 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
84 /* Turn it into hours */
85 diff = (diff + 30) / 60;
87 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
90 /* We deal with number of days from here on */
91 diff = (diff + 12) / 24;
93 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
96 /* Say weeks for the past 10 weeks or so */
98 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
101 /* Say months for the past 12 months or so */
103 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
106 /* Else fall back on absolute format.. */
109 tm = time_to_tm(time, tz);
112 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
113 weekday_names[tm->tm_wday],
114 month_names[tm->tm_mon],
116 tm->tm_hour, tm->tm_min, tm->tm_sec,
117 tm->tm_year + 1900, tz);
121 const char *show_rfc2822_date(unsigned long time, int tz)
124 static char timebuf[200];
126 tm = time_to_tm(time, tz);
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);
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
142 static const struct {
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)
198 for (i = 0; *date; date++, str++, i++) {
201 if (toupper(*date) == toupper(*str))
210 static int skip_alpha(const char *date)
215 } while (isalpha(date[i]));
220 * Parse month, weekday, or timezone name
222 static int match_alpha(const char *date, struct tm *tm, int *offset)
226 for (i = 0; i < 12; i++) {
227 int match = match_string(date, month_names[i]);
234 for (i = 0; i < 7; i++) {
235 int match = match_string(date, weekday_names[i]);
242 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
243 int match = match_string(date, timezone_names[i].name);
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 */
258 if (match_string(date, "PM") == 2) {
259 tm->tm_hour = (tm->tm_hour % 12) + 12;
263 if (match_string(date, "AM") == 2) {
264 tm->tm_hour = (tm->tm_hour % 12) + 0;
269 return skip_alpha(date);
272 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
274 if (month > 0 && month < 13 && day > 0 && day < 32) {
275 struct tm check = *tm;
276 struct tm *r = (now_tm ? &check : tm);
279 r->tm_mon = month - 1;
284 r->tm_year = now_tm->tm_year;
286 else if (year >= 1970 && year < 2100)
287 r->tm_year = year - 1900;
288 else if (year > 70 && year < 100)
291 r->tm_year = year + 100;
297 specified = my_mktime(r);
299 /* Be it commit time or author time, it does not make
300 * sense to specify timestamp way into the future. Make
301 * sure it is not later than ten days from now...
303 if (now + 10*24*3600 < specified)
305 tm->tm_mon = r->tm_mon;
306 tm->tm_mday = r->tm_mday;
308 tm->tm_year = r->tm_year;
314 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
318 struct tm *refuse_future;
321 num2 = strtol(end+1, &end, 10);
323 if (*end == c && isdigit(end[1]))
324 num3 = strtol(end+1, &end, 10);
331 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
343 refuse_future = NULL;
344 if (gmtime_r(&now, &now_tm))
345 refuse_future = &now_tm;
349 if (is_date(num, num2, num3, refuse_future, now, tm))
352 if (is_date(num, num3, num2, refuse_future, now, tm))
355 /* Our eastern European friends say dd.mm.yy[yy]
356 * is the norm there, so giving precedence to
357 * mm/dd/yy[yy] form only when separator is not '.'
360 is_date(num3, num, num2, refuse_future, now, tm))
362 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
363 if (is_date(num3, num2, num, refuse_future, now, tm))
365 /* Funny European mm.dd.yy */
367 is_date(num3, num, num2, refuse_future, now, tm))
375 * We've seen a digit. Time? Year? Date?
377 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
383 num = strtoul(date, &end, 10);
386 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
388 if (num > 946684800) {
390 if (gmtime_r(&time, tm)) {
397 * Check for special formats: num[-.:/]num[same]num
404 if (isdigit(end[1])) {
405 int match = match_multi_number(num, *end, date, end, tm);
412 * None of the special formats? Try to guess what
413 * the number meant. We use the number of digits
414 * to make a more educated guess..
419 } while (isdigit(date[n]));
421 /* Four-digit year or a timezone? */
423 if (num <= 1400 && *offset == -1) {
424 unsigned int minutes = num % 100;
425 unsigned int hours = num / 100;
426 *offset = hours*60 + minutes;
427 } else if (num > 1900 && num < 2100)
428 tm->tm_year = num - 1900;
433 * NOTE! We will give precedence to day-of-month over month or
434 * year numbers in the 1-12 range. So 05 is always "mday 5",
435 * unless we already have a mday..
437 * IOW, 01 Apr 05 parses as "April 1st, 2005".
439 if (num > 0 && num < 32 && tm->tm_mday < 0) {
444 /* Two-digit year? */
445 if (n == 2 && tm->tm_year < 0) {
446 if (num < 10 && tm->tm_mday >= 0) {
447 tm->tm_year = num + 100;
456 if (num > 0 && num < 32) {
458 } else if (num > 1900) {
459 tm->tm_year = num - 1900;
460 } else if (num > 70) {
462 } else if (num > 0 && num < 13) {
469 static int match_tz(const char *date, int *offp)
472 int offset = strtoul(date+1, &end, 10);
474 int n = end - date - 1;
480 * Don't accept any random crap.. At least 3 digits, and
481 * a valid minute. We might want to check that the minutes
482 * are divisible by 30 or something too.
484 if (min < 60 && n > 2) {
485 offset = hour*60+min;
494 static int date_string(unsigned long date, int offset, char *buf, int len)
502 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
505 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
506 (i.e. English) day/month names, and it doesn't work correctly with %z. */
507 int parse_date(const char *date, char *result, int maxlen)
513 memset(&tm, 0, sizeof(tm));
523 unsigned char c = *date;
525 /* Stop at end of string or newline */
530 match = match_alpha(date, &tm, &offset);
532 match = match_digit(date, &tm, &offset, &tm_gmt);
533 else if ((c == '-' || c == '+') && isdigit(date[1]))
534 match = match_tz(date, &offset);
544 /* mktime uses local timezone */
545 then = my_mktime(&tm);
547 offset = (then - mktime(&tm)) / 60;
554 return date_string(then, offset, result, maxlen);
557 void datestamp(char *buf, int bufsize)
564 offset = my_mktime(localtime(&now)) - now;
567 date_string(now, offset, buf, bufsize);
570 static void update_tm(struct tm *tm, unsigned long sec)
572 time_t n = mktime(tm) - sec;
576 static void date_yesterday(struct tm *tm, int *num)
578 update_tm(tm, 24*60*60);
581 static void date_time(struct tm *tm, int hour)
583 if (tm->tm_hour < hour)
584 date_yesterday(tm, NULL);
590 static void date_midnight(struct tm *tm, int *num)
595 static void date_noon(struct tm *tm, int *num)
600 static void date_tea(struct tm *tm, int *num)
605 static void date_pm(struct tm *tm, int *num)
616 tm->tm_hour = (hour % 12) + 12;
619 static void date_am(struct tm *tm, int *num)
630 tm->tm_hour = (hour % 12);
633 static const struct special {
635 void (*fn)(struct tm *, int *);
637 { "yesterday", date_yesterday },
638 { "noon", date_noon },
639 { "midnight", date_midnight },
646 static const char *number_name[] = {
647 "zero", "one", "two", "three", "four",
648 "five", "six", "seven", "eight", "nine", "ten",
651 static const struct typelen {
658 { "days", 24*60*60 },
659 { "weeks", 7*24*60*60 },
663 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
665 const struct typelen *tl;
666 const struct special *s;
667 const char *end = date;
670 while (isalpha(*++end));
673 for (i = 0; i < 12; i++) {
674 int match = match_string(date, month_names[i]);
681 for (s = special; s->name; s++) {
682 int len = strlen(s->name);
683 if (match_string(date, s->name) == len) {
690 for (i = 1; i < 11; i++) {
691 int len = strlen(number_name[i]);
692 if (match_string(date, number_name[i]) == len) {
697 if (match_string(date, "last") == 4)
704 int len = strlen(tl->type);
705 if (match_string(date, tl->type) >= len-1) {
706 update_tm(tm, tl->length * *num);
713 for (i = 0; i < 7; i++) {
714 int match = match_string(date, weekday_names[i]);
716 int diff, n = *num -1;
719 diff = tm->tm_wday - i;
724 update_tm(tm, diff * 24 * 60 * 60);
729 if (match_string(date, "months") >= 5) {
730 int n = tm->tm_mon - *num;
740 if (match_string(date, "years") >= 4) {
749 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
752 unsigned long number = strtoul(date, &end, 10);
759 if (isdigit(end[1])) {
760 int match = match_multi_number(number, *end, date, end, tm);
770 unsigned long approxidate(const char *date)
777 if (parse_date(date, buffer, sizeof(buffer)) > 0)
778 return strtoul(buffer, NULL, 10);
780 gettimeofday(&tv, NULL);
781 localtime_r(&tv.tv_sec, &tm);
784 unsigned char c = *date;
789 date = approxidate_digit(date-1, &tm, &number);
793 date = approxidate_alpha(date-1, &tm, &number);
795 if (number > 0 && number < 32)
797 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)