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);
59 * What value of "tz" was in effect back then at "time" in the
62 static int local_tzoffset(unsigned long time)
70 t_local = my_mktime(&tm);
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)
87 static char timebuf[200];
89 if (mode == DATE_RELATIVE) {
92 gettimeofday(&now, NULL);
93 if (now.tv_sec < time)
94 return "in the future";
95 diff = now.tv_sec - time;
97 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
100 /* Turn it into minutes */
101 diff = (diff + 30) / 60;
103 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
106 /* Turn it into hours */
107 diff = (diff + 30) / 60;
109 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
112 /* We deal with number of days from here on */
113 diff = (diff + 12) / 24;
115 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
118 /* Say weeks for the past 10 weeks or so */
120 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
123 /* Say months for the past 12 months or so */
125 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
128 /* Else fall back on absolute format.. */
131 if (mode == DATE_LOCAL)
132 tz = local_tzoffset(time);
134 tm = time_to_tm(time, tz);
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",
145 tm->tm_hour, tm->tm_min, tm->tm_sec,
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);
153 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
154 weekday_names[tm->tm_wday],
155 month_names[tm->tm_mon],
157 tm->tm_hour, tm->tm_min, tm->tm_sec,
159 (mode == DATE_LOCAL) ? 0 : ' ',
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
170 static const struct {
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", +12, 0, }, /* New Zealand */
217 { "NZST", +12, 0, }, /* New Zealand Standard */
218 { "NZDT", +12, 1, }, /* New Zealand Daylight */
219 { "IDLE", +12, 0, }, /* International Date Line East */
222 static int match_string(const char *date, const char *str)
226 for (i = 0; *date; date++, str++, i++) {
229 if (toupper(*date) == toupper(*str))
238 static int skip_alpha(const char *date)
243 } while (isalpha(date[i]));
248 * Parse month, weekday, or timezone name
250 static int match_alpha(const char *date, struct tm *tm, int *offset)
254 for (i = 0; i < 12; i++) {
255 int match = match_string(date, month_names[i]);
262 for (i = 0; i < 7; i++) {
263 int match = match_string(date, weekday_names[i]);
270 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
271 int match = match_string(date, timezone_names[i].name);
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 */
286 if (match_string(date, "PM") == 2) {
287 tm->tm_hour = (tm->tm_hour % 12) + 12;
291 if (match_string(date, "AM") == 2) {
292 tm->tm_hour = (tm->tm_hour % 12) + 0;
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);
307 r->tm_mon = month - 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)
319 r->tm_year = year + 100;
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)
333 tm->tm_mon = r->tm_mon;
334 tm->tm_mday = r->tm_mday;
336 tm->tm_year = r->tm_year;
342 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
346 struct tm *refuse_future;
349 num2 = strtol(end+1, &end, 10);
351 if (*end == c && isdigit(end[1]))
352 num3 = strtol(end+1, &end, 10);
359 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
371 refuse_future = NULL;
372 if (gmtime_r(&now, &now_tm))
373 refuse_future = &now_tm;
377 if (is_date(num, num2, num3, refuse_future, now, tm))
380 if (is_date(num, num3, num2, refuse_future, now, tm))
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 '.'
388 is_date(num3, num, num2, refuse_future, now, tm))
390 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
391 if (is_date(num3, num2, num, refuse_future, now, tm))
393 /* Funny European mm.dd.yy */
395 is_date(num3, num, num2, refuse_future, now, tm))
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)
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) {
420 if (gmtime_r(&time, tm)) {
427 * Check for special formats: num[-.:/]num[same]num
434 if (isdigit(end[1])) {
435 int match = match_multi_number(num, *end, date, end, tm);
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..
449 } while (isdigit(date[n]));
451 /* Four-digit year or a timezone? */
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;
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) {
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;
486 if (num > 0 && num < 32) {
488 } else if (num > 1900) {
489 tm->tm_year = num - 1900;
490 } else if (num > 70) {
492 } else if (num > 0 && num < 13) {
499 static int match_tz(const char *date, int *offp)
502 int offset = strtoul(date+1, &end, 10);
504 int n = end - date - 1;
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;
524 static int date_string(unsigned long date, int offset, char *buf, int len)
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)
543 memset(&tm, 0, sizeof(tm));
553 unsigned char c = *date;
555 /* Stop at end of string or newline */
560 match = match_alpha(date, &tm, &offset);
562 match = match_digit(date, &tm, &offset, &tm_gmt);
563 else if ((c == '-' || c == '+') && isdigit(date[1]))
564 match = match_tz(date, &offset);
574 /* mktime uses local timezone */
575 then = my_mktime(&tm);
577 offset = (then - mktime(&tm)) / 60;
584 return date_string(then, offset, result, maxlen);
587 enum date_mode parse_date_format(const char *format)
589 if (!strcmp(format, "relative"))
590 return DATE_RELATIVE;
591 else if (!strcmp(format, "iso8601") ||
592 !strcmp(format, "iso"))
594 else if (!strcmp(format, "rfc2822") ||
595 !strcmp(format, "rfc"))
597 else if (!strcmp(format, "short"))
599 else if (!strcmp(format, "local"))
601 else if (!strcmp(format, "default"))
604 die("unknown date format %s", format);
607 void datestamp(char *buf, int bufsize)
614 offset = my_mktime(localtime(&now)) - now;
617 date_string(now, offset, buf, bufsize);
620 static void update_tm(struct tm *tm, unsigned long sec)
622 time_t n = mktime(tm) - sec;
626 static void date_yesterday(struct tm *tm, int *num)
628 update_tm(tm, 24*60*60);
631 static void date_time(struct tm *tm, int hour)
633 if (tm->tm_hour < hour)
634 date_yesterday(tm, NULL);
640 static void date_midnight(struct tm *tm, int *num)
645 static void date_noon(struct tm *tm, int *num)
650 static void date_tea(struct tm *tm, int *num)
655 static void date_pm(struct tm *tm, int *num)
666 tm->tm_hour = (hour % 12) + 12;
669 static void date_am(struct tm *tm, int *num)
680 tm->tm_hour = (hour % 12);
683 static void date_never(struct tm *tm, int *num)
685 tm->tm_mon = tm->tm_wday = tm->tm_yday
686 = tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
691 static const struct special {
693 void (*fn)(struct tm *, int *);
695 { "yesterday", date_yesterday },
696 { "noon", date_noon },
697 { "midnight", date_midnight },
701 { "never", date_never },
705 static const char *number_name[] = {
706 "zero", "one", "two", "three", "four",
707 "five", "six", "seven", "eight", "nine", "ten",
710 static const struct typelen {
717 { "days", 24*60*60 },
718 { "weeks", 7*24*60*60 },
722 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
724 const struct typelen *tl;
725 const struct special *s;
726 const char *end = date;
729 while (isalpha(*++end));
732 for (i = 0; i < 12; i++) {
733 int match = match_string(date, month_names[i]);
740 for (s = special; s->name; s++) {
741 int len = strlen(s->name);
742 if (match_string(date, s->name) == len) {
749 for (i = 1; i < 11; i++) {
750 int len = strlen(number_name[i]);
751 if (match_string(date, number_name[i]) == len) {
756 if (match_string(date, "last") == 4)
763 int len = strlen(tl->type);
764 if (match_string(date, tl->type) >= len-1) {
765 update_tm(tm, tl->length * *num);
772 for (i = 0; i < 7; i++) {
773 int match = match_string(date, weekday_names[i]);
775 int diff, n = *num -1;
778 diff = tm->tm_wday - i;
783 update_tm(tm, diff * 24 * 60 * 60);
788 if (match_string(date, "months") >= 5) {
789 int n = tm->tm_mon - *num;
799 if (match_string(date, "years") >= 4) {
808 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
811 unsigned long number = strtoul(date, &end, 10);
818 if (isdigit(end[1])) {
819 int match = match_multi_number(number, *end, date, end, tm);
829 unsigned long approxidate(const char *date)
836 if (parse_date(date, buffer, sizeof(buffer)) > 0)
837 return strtoul(buffer, NULL, 10);
839 gettimeofday(&tv, NULL);
840 localtime_r(&tv.tv_sec, &tm);
843 unsigned char c = *date;
848 date = approxidate_digit(date-1, &tm, &number);
852 date = approxidate_alpha(date-1, &tm, &number);
854 if (number > 0 && number < 32)
856 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)