2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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 */
20 if (month < 0 || month > 11) /* array bounds */
22 if (month < 2 || (year + 2) % 4)
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)
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);
58 const char *show_date(unsigned long time, int tz, int relative)
61 static char timebuf[200];
66 gettimeofday(&now, NULL);
67 if (now.tv_sec < time)
68 return "in the future";
69 diff = now.tv_sec - time;
71 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
74 /* Turn it into minutes */
75 diff = (diff + 30) / 60;
77 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
80 /* Turn it into hours */
81 diff = (diff + 30) / 60;
83 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
86 /* We deal with number of days from here on */
87 diff = (diff + 12) / 24;
89 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
92 /* Say weeks for the past 10 weeks or so */
94 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
97 /* Say months for the past 12 months or so */
99 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
102 /* Else fall back on absolute format.. */
105 tm = time_to_tm(time, tz);
108 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
109 weekday_names[tm->tm_wday],
110 month_names[tm->tm_mon],
112 tm->tm_hour, tm->tm_min, tm->tm_sec,
113 tm->tm_year + 1900, tz);
117 const char *show_rfc2822_date(unsigned long time, int tz)
120 static char timebuf[200];
122 tm = time_to_tm(time, tz);
125 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
126 weekday_names[tm->tm_wday], tm->tm_mday,
127 month_names[tm->tm_mon], tm->tm_year + 1900,
128 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
133 * Check these. And note how it doesn't do the summer-time conversion.
135 * In my world, it's always summer, and things are probably a bit off
138 static const struct {
142 } timezone_names[] = {
143 { "IDLW", -12, 0, }, /* International Date Line West */
144 { "NT", -11, 0, }, /* Nome */
145 { "CAT", -10, 0, }, /* Central Alaska */
146 { "HST", -10, 0, }, /* Hawaii Standard */
147 { "HDT", -10, 1, }, /* Hawaii Daylight */
148 { "YST", -9, 0, }, /* Yukon Standard */
149 { "YDT", -9, 1, }, /* Yukon Daylight */
150 { "PST", -8, 0, }, /* Pacific Standard */
151 { "PDT", -8, 1, }, /* Pacific Daylight */
152 { "MST", -7, 0, }, /* Mountain Standard */
153 { "MDT", -7, 1, }, /* Mountain Daylight */
154 { "CST", -6, 0, }, /* Central Standard */
155 { "CDT", -6, 1, }, /* Central Daylight */
156 { "EST", -5, 0, }, /* Eastern Standard */
157 { "EDT", -5, 1, }, /* Eastern Daylight */
158 { "AST", -3, 0, }, /* Atlantic Standard */
159 { "ADT", -3, 1, }, /* Atlantic Daylight */
160 { "WAT", -1, 0, }, /* West Africa */
162 { "GMT", 0, 0, }, /* Greenwich Mean */
163 { "UTC", 0, 0, }, /* Universal (Coordinated) */
165 { "WET", 0, 0, }, /* Western European */
166 { "BST", 0, 1, }, /* British Summer */
167 { "CET", +1, 0, }, /* Central European */
168 { "MET", +1, 0, }, /* Middle European */
169 { "MEWT", +1, 0, }, /* Middle European Winter */
170 { "MEST", +1, 1, }, /* Middle European Summer */
171 { "CEST", +1, 1, }, /* Central European Summer */
172 { "MESZ", +1, 1, }, /* Middle European Summer */
173 { "FWT", +1, 0, }, /* French Winter */
174 { "FST", +1, 1, }, /* French Summer */
175 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
176 { "EEST", +2, 1, }, /* Eastern European Daylight */
177 { "WAST", +7, 0, }, /* West Australian Standard */
178 { "WADT", +7, 1, }, /* West Australian Daylight */
179 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
180 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
181 { "EAST", +10, 0, }, /* Eastern Australian Standard */
182 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
183 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
184 { "NZT", +11, 0, }, /* New Zealand */
185 { "NZST", +11, 0, }, /* New Zealand Standard */
186 { "NZDT", +11, 1, }, /* New Zealand Daylight */
187 { "IDLE", +12, 0, }, /* International Date Line East */
190 static int match_string(const char *date, const char *str)
194 for (i = 0; *date; date++, str++, i++) {
197 if (toupper(*date) == toupper(*str))
206 static int skip_alpha(const char *date)
211 } while (isalpha(date[i]));
216 * Parse month, weekday, or timezone name
218 static int match_alpha(const char *date, struct tm *tm, int *offset)
222 for (i = 0; i < 12; i++) {
223 int match = match_string(date, month_names[i]);
230 for (i = 0; i < 7; i++) {
231 int match = match_string(date, weekday_names[i]);
238 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
239 int match = match_string(date, timezone_names[i].name);
241 int off = timezone_names[i].offset;
243 /* This is bogus, but we like summer */
244 off += timezone_names[i].dst;
246 /* Only use the tz name offset if we don't have anything better */
254 if (match_string(date, "PM") == 2) {
255 tm->tm_hour = (tm->tm_hour % 12) + 12;
259 if (match_string(date, "AM") == 2) {
260 tm->tm_hour = (tm->tm_hour % 12) + 0;
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);
275 r->tm_mon = month - 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)
287 r->tm_year = year + 100;
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)
301 tm->tm_mon = r->tm_mon;
302 tm->tm_mday = r->tm_mday;
304 tm->tm_year = r->tm_year;
310 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
314 struct tm *refuse_future;
317 num2 = strtol(end+1, &end, 10);
319 if (*end == c && isdigit(end[1]))
320 num3 = strtol(end+1, &end, 10);
327 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
339 refuse_future = NULL;
340 if (gmtime_r(&now, &now_tm))
341 refuse_future = &now_tm;
345 if (is_date(num, num2, num3, refuse_future, now, tm))
348 if (is_date(num, num3, num2, refuse_future, now, tm))
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 '.'
356 is_date(num3, num, num2, refuse_future, now, tm))
358 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
359 if (is_date(num3, num2, num, refuse_future, now, tm))
361 /* Funny European mm.dd.yy */
363 is_date(num3, num, num2, refuse_future, now, tm))
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)
379 num = strtoul(date, &end, 10);
382 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
384 if (num > 946684800) {
386 if (gmtime_r(&time, tm)) {
393 * Check for special formats: num[-.:/]num[same]num
400 if (isdigit(end[1])) {
401 int match = match_multi_number(num, *end, date, end, tm);
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..
415 } while (isdigit(date[n]));
417 /* Four-digit year or a timezone? */
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;
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) {
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;
452 if (num > 0 && num < 32) {
454 } else if (num > 1900) {
455 tm->tm_year = num - 1900;
456 } else if (num > 70) {
458 } else if (num > 0 && num < 13) {
465 static int match_tz(const char *date, int *offp)
468 int offset = strtoul(date+1, &end, 10);
470 int n = end - date - 1;
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;
490 static int date_string(unsigned long date, int offset, char *buf, int len)
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)
509 memset(&tm, 0, sizeof(tm));
519 unsigned char c = *date;
521 /* Stop at end of string or newline */
526 match = match_alpha(date, &tm, &offset);
528 match = match_digit(date, &tm, &offset, &tm_gmt);
529 else if ((c == '-' || c == '+') && isdigit(date[1]))
530 match = match_tz(date, &offset);
540 /* mktime uses local timezone */
541 then = my_mktime(&tm);
543 offset = (then - mktime(&tm)) / 60;
550 return date_string(then, offset, result, maxlen);
553 void datestamp(char *buf, int bufsize)
560 offset = my_mktime(localtime(&now)) - now;
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;
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);
586 static void date_midnight(struct tm *tm, int *num)
591 static void date_noon(struct tm *tm, int *num)
596 static void date_tea(struct tm *tm, int *num)
601 static void date_pm(struct tm *tm, int *num)
612 tm->tm_hour = (hour % 12) + 12;
615 static void date_am(struct tm *tm, int *num)
626 tm->tm_hour = (hour % 12);
629 static const struct special {
631 void (*fn)(struct tm *, int *);
633 { "yesterday", date_yesterday },
634 { "noon", date_noon },
635 { "midnight", date_midnight },
642 static const char *number_name[] = {
643 "zero", "one", "two", "three", "four",
644 "five", "six", "seven", "eight", "nine", "ten",
647 static const struct typelen {
654 { "days", 24*60*60 },
655 { "weeks", 7*24*60*60 },
659 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
661 const struct typelen *tl;
662 const struct special *s;
663 const char *end = date;
666 while (isalpha(*++end));
669 for (i = 0; i < 12; i++) {
670 int match = match_string(date, month_names[i]);
677 for (s = special; s->name; s++) {
678 int len = strlen(s->name);
679 if (match_string(date, s->name) == len) {
686 for (i = 1; i < 11; i++) {
687 int len = strlen(number_name[i]);
688 if (match_string(date, number_name[i]) == len) {
693 if (match_string(date, "last") == 4)
700 int len = strlen(tl->type);
701 if (match_string(date, tl->type) >= len-1) {
702 update_tm(tm, tl->length * *num);
709 for (i = 0; i < 7; i++) {
710 int match = match_string(date, weekday_names[i]);
712 int diff, n = *num -1;
715 diff = tm->tm_wday - i;
720 update_tm(tm, diff * 24 * 60 * 60);
725 if (match_string(date, "months") >= 5) {
726 int n = tm->tm_mon - *num;
736 if (match_string(date, "years") >= 4) {
745 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
748 unsigned long number = strtoul(date, &end, 10);
755 if (isdigit(end[1])) {
756 int match = match_multi_number(number, *end, date, end, tm);
766 unsigned long approxidate(const char *date)
773 if (parse_date(date, buffer, sizeof(buffer)) > 0)
774 return strtoul(buffer, NULL, 10);
776 gettimeofday(&tv, NULL);
777 localtime_r(&tv.tv_sec, &tm);
780 unsigned char c = *date;
785 date = approxidate_digit(date-1, &tm, &number);
789 date = approxidate_alpha(date-1, &tm, &number);
791 if (number > 0 && number < 32)
793 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)