2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 * This is like mktime, but without normalization of tm_wday and tm_yday.
12 time_t tm_to_time_t(const 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);
62 * What value of "tz" was in effect back then at "time" in the
65 static int local_tzoffset(unsigned long time)
73 t_local = tm_to_time_t(&tm);
82 offset /= 60; /* in minutes */
83 offset = (offset % 60) + ((offset / 60) * 100);
84 return offset * eastwest;
87 const char *show_date(unsigned long time, int tz, enum date_mode mode)
90 static char timebuf[200];
92 if (mode == DATE_RELATIVE) {
95 gettimeofday(&now, NULL);
96 if (now.tv_sec < time)
97 return "in the future";
98 diff = now.tv_sec - time;
100 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
103 /* Turn it into minutes */
104 diff = (diff + 30) / 60;
106 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
109 /* Turn it into hours */
110 diff = (diff + 30) / 60;
112 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
115 /* We deal with number of days from here on */
116 diff = (diff + 12) / 24;
118 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
121 /* Say weeks for the past 10 weeks or so */
123 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
126 /* Say months for the past 12 months or so */
128 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
131 /* Else fall back on absolute format.. */
134 if (mode == DATE_LOCAL)
135 tz = local_tzoffset(time);
137 tm = time_to_tm(time, tz);
140 if (mode == DATE_SHORT)
141 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
142 tm->tm_mon + 1, tm->tm_mday);
143 else if (mode == DATE_ISO8601)
144 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
148 tm->tm_hour, tm->tm_min, tm->tm_sec,
150 else if (mode == DATE_RFC2822)
151 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
152 weekday_names[tm->tm_wday], tm->tm_mday,
153 month_names[tm->tm_mon], tm->tm_year + 1900,
154 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
156 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
157 weekday_names[tm->tm_wday],
158 month_names[tm->tm_mon],
160 tm->tm_hour, tm->tm_min, tm->tm_sec,
162 (mode == DATE_LOCAL) ? 0 : ' ',
168 * Check these. And note how it doesn't do the summer-time conversion.
170 * In my world, it's always summer, and things are probably a bit off
173 static const struct {
177 } timezone_names[] = {
178 { "IDLW", -12, 0, }, /* International Date Line West */
179 { "NT", -11, 0, }, /* Nome */
180 { "CAT", -10, 0, }, /* Central Alaska */
181 { "HST", -10, 0, }, /* Hawaii Standard */
182 { "HDT", -10, 1, }, /* Hawaii Daylight */
183 { "YST", -9, 0, }, /* Yukon Standard */
184 { "YDT", -9, 1, }, /* Yukon Daylight */
185 { "PST", -8, 0, }, /* Pacific Standard */
186 { "PDT", -8, 1, }, /* Pacific Daylight */
187 { "MST", -7, 0, }, /* Mountain Standard */
188 { "MDT", -7, 1, }, /* Mountain Daylight */
189 { "CST", -6, 0, }, /* Central Standard */
190 { "CDT", -6, 1, }, /* Central Daylight */
191 { "EST", -5, 0, }, /* Eastern Standard */
192 { "EDT", -5, 1, }, /* Eastern Daylight */
193 { "AST", -3, 0, }, /* Atlantic Standard */
194 { "ADT", -3, 1, }, /* Atlantic Daylight */
195 { "WAT", -1, 0, }, /* West Africa */
197 { "GMT", 0, 0, }, /* Greenwich Mean */
198 { "UTC", 0, 0, }, /* Universal (Coordinated) */
200 { "WET", 0, 0, }, /* Western European */
201 { "BST", 0, 1, }, /* British Summer */
202 { "CET", +1, 0, }, /* Central European */
203 { "MET", +1, 0, }, /* Middle European */
204 { "MEWT", +1, 0, }, /* Middle European Winter */
205 { "MEST", +1, 1, }, /* Middle European Summer */
206 { "CEST", +1, 1, }, /* Central European Summer */
207 { "MESZ", +1, 1, }, /* Middle European Summer */
208 { "FWT", +1, 0, }, /* French Winter */
209 { "FST", +1, 1, }, /* French Summer */
210 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
211 { "EEST", +2, 1, }, /* Eastern European Daylight */
212 { "WAST", +7, 0, }, /* West Australian Standard */
213 { "WADT", +7, 1, }, /* West Australian Daylight */
214 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
215 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
216 { "EAST", +10, 0, }, /* Eastern Australian Standard */
217 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
218 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
219 { "NZT", +12, 0, }, /* New Zealand */
220 { "NZST", +12, 0, }, /* New Zealand Standard */
221 { "NZDT", +12, 1, }, /* New Zealand Daylight */
222 { "IDLE", +12, 0, }, /* International Date Line East */
225 static int match_string(const char *date, const char *str)
229 for (i = 0; *date; date++, str++, i++) {
232 if (toupper(*date) == toupper(*str))
241 static int skip_alpha(const char *date)
246 } while (isalpha(date[i]));
251 * Parse month, weekday, or timezone name
253 static int match_alpha(const char *date, struct tm *tm, int *offset)
257 for (i = 0; i < 12; i++) {
258 int match = match_string(date, month_names[i]);
265 for (i = 0; i < 7; i++) {
266 int match = match_string(date, weekday_names[i]);
273 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
274 int match = match_string(date, timezone_names[i].name);
276 int off = timezone_names[i].offset;
278 /* This is bogus, but we like summer */
279 off += timezone_names[i].dst;
281 /* Only use the tz name offset if we don't have anything better */
289 if (match_string(date, "PM") == 2) {
290 tm->tm_hour = (tm->tm_hour % 12) + 12;
294 if (match_string(date, "AM") == 2) {
295 tm->tm_hour = (tm->tm_hour % 12) + 0;
300 return skip_alpha(date);
303 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
305 if (month > 0 && month < 13 && day > 0 && day < 32) {
306 struct tm check = *tm;
307 struct tm *r = (now_tm ? &check : tm);
310 r->tm_mon = month - 1;
315 r->tm_year = now_tm->tm_year;
317 else if (year >= 1970 && year < 2100)
318 r->tm_year = year - 1900;
319 else if (year > 70 && year < 100)
322 r->tm_year = year + 100;
328 specified = tm_to_time_t(r);
330 /* Be it commit time or author time, it does not make
331 * sense to specify timestamp way into the future. Make
332 * sure it is not later than ten days from now...
334 if (now + 10*24*3600 < specified)
336 tm->tm_mon = r->tm_mon;
337 tm->tm_mday = r->tm_mday;
339 tm->tm_year = r->tm_year;
345 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
349 struct tm *refuse_future;
352 num2 = strtol(end+1, &end, 10);
354 if (*end == c && isdigit(end[1]))
355 num3 = strtol(end+1, &end, 10);
362 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
374 refuse_future = NULL;
375 if (gmtime_r(&now, &now_tm))
376 refuse_future = &now_tm;
380 if (is_date(num, num2, num3, refuse_future, now, tm))
383 if (is_date(num, num3, num2, refuse_future, now, tm))
386 /* Our eastern European friends say dd.mm.yy[yy]
387 * is the norm there, so giving precedence to
388 * mm/dd/yy[yy] form only when separator is not '.'
391 is_date(num3, num, num2, refuse_future, now, tm))
393 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
394 if (is_date(num3, num2, num, refuse_future, now, tm))
396 /* Funny European mm.dd.yy */
398 is_date(num3, num, num2, refuse_future, now, tm))
406 * We've seen a digit. Time? Year? Date?
408 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
414 num = strtoul(date, &end, 10);
417 * Seconds since 1970? We trigger on that for any numbers with
418 * more than 8 digits. This is because we don't want to rule out
419 * numbers like 20070606 as a YYYYMMDD date.
421 if (num >= 100000000) {
423 if (gmtime_r(&time, tm)) {
430 * Check for special formats: num[-.:/]num[same]num
437 if (isdigit(end[1])) {
438 int match = match_multi_number(num, *end, date, end, tm);
445 * None of the special formats? Try to guess what
446 * the number meant. We use the number of digits
447 * to make a more educated guess..
452 } while (isdigit(date[n]));
454 /* Four-digit year or a timezone? */
456 if (num <= 1400 && *offset == -1) {
457 unsigned int minutes = num % 100;
458 unsigned int hours = num / 100;
459 *offset = hours*60 + minutes;
460 } else if (num > 1900 && num < 2100)
461 tm->tm_year = num - 1900;
466 * NOTE! We will give precedence to day-of-month over month or
467 * year numbers in the 1-12 range. So 05 is always "mday 5",
468 * unless we already have a mday..
470 * IOW, 01 Apr 05 parses as "April 1st, 2005".
472 if (num > 0 && num < 32 && tm->tm_mday < 0) {
477 /* Two-digit year? */
478 if (n == 2 && tm->tm_year < 0) {
479 if (num < 10 && tm->tm_mday >= 0) {
480 tm->tm_year = num + 100;
489 if (num > 0 && num < 32) {
491 } else if (num > 1900) {
492 tm->tm_year = num - 1900;
493 } else if (num > 70) {
495 } else if (num > 0 && num < 13) {
502 static int match_tz(const char *date, int *offp)
505 int offset = strtoul(date+1, &end, 10);
507 int n = end - date - 1;
513 * Don't accept any random crap.. At least 3 digits, and
514 * a valid minute. We might want to check that the minutes
515 * are divisible by 30 or something too.
517 if (min < 60 && n > 2) {
518 offset = hour*60+min;
527 static int date_string(unsigned long date, int offset, char *buf, int len)
535 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
538 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
539 (i.e. English) day/month names, and it doesn't work correctly with %z. */
540 int parse_date(const char *date, char *result, int maxlen)
546 memset(&tm, 0, sizeof(tm));
556 unsigned char c = *date;
558 /* Stop at end of string or newline */
563 match = match_alpha(date, &tm, &offset);
565 match = match_digit(date, &tm, &offset, &tm_gmt);
566 else if ((c == '-' || c == '+') && isdigit(date[1]))
567 match = match_tz(date, &offset);
577 /* mktime uses local timezone */
578 then = tm_to_time_t(&tm);
580 offset = (then - mktime(&tm)) / 60;
587 return date_string(then, offset, result, maxlen);
590 enum date_mode parse_date_format(const char *format)
592 if (!strcmp(format, "relative"))
593 return DATE_RELATIVE;
594 else if (!strcmp(format, "iso8601") ||
595 !strcmp(format, "iso"))
597 else if (!strcmp(format, "rfc2822") ||
598 !strcmp(format, "rfc"))
600 else if (!strcmp(format, "short"))
602 else if (!strcmp(format, "local"))
604 else if (!strcmp(format, "default"))
607 die("unknown date format %s", format);
610 void datestamp(char *buf, int bufsize)
617 offset = tm_to_time_t(localtime(&now)) - now;
620 date_string(now, offset, buf, bufsize);
623 static void update_tm(struct tm *tm, unsigned long sec)
625 time_t n = mktime(tm) - sec;
629 static void date_yesterday(struct tm *tm, int *num)
631 update_tm(tm, 24*60*60);
634 static void date_time(struct tm *tm, int hour)
636 if (tm->tm_hour < hour)
637 date_yesterday(tm, NULL);
643 static void date_midnight(struct tm *tm, int *num)
648 static void date_noon(struct tm *tm, int *num)
653 static void date_tea(struct tm *tm, int *num)
658 static void date_pm(struct tm *tm, int *num)
669 tm->tm_hour = (hour % 12) + 12;
672 static void date_am(struct tm *tm, int *num)
683 tm->tm_hour = (hour % 12);
686 static void date_never(struct tm *tm, int *num)
692 static const struct special {
694 void (*fn)(struct tm *, int *);
696 { "yesterday", date_yesterday },
697 { "noon", date_noon },
698 { "midnight", date_midnight },
702 { "never", date_never },
706 static const char *number_name[] = {
707 "zero", "one", "two", "three", "four",
708 "five", "six", "seven", "eight", "nine", "ten",
711 static const struct typelen {
718 { "days", 24*60*60 },
719 { "weeks", 7*24*60*60 },
723 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
725 const struct typelen *tl;
726 const struct special *s;
727 const char *end = date;
730 while (isalpha(*++end));
733 for (i = 0; i < 12; i++) {
734 int match = match_string(date, month_names[i]);
741 for (s = special; s->name; s++) {
742 int len = strlen(s->name);
743 if (match_string(date, s->name) == len) {
750 for (i = 1; i < 11; i++) {
751 int len = strlen(number_name[i]);
752 if (match_string(date, number_name[i]) == len) {
757 if (match_string(date, "last") == 4)
764 int len = strlen(tl->type);
765 if (match_string(date, tl->type) >= len-1) {
766 update_tm(tm, tl->length * *num);
773 for (i = 0; i < 7; i++) {
774 int match = match_string(date, weekday_names[i]);
776 int diff, n = *num -1;
779 diff = tm->tm_wday - i;
784 update_tm(tm, diff * 24 * 60 * 60);
789 if (match_string(date, "months") >= 5) {
790 int n = tm->tm_mon - *num;
800 if (match_string(date, "years") >= 4) {
809 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
812 unsigned long number = strtoul(date, &end, 10);
819 if (isdigit(end[1])) {
820 int match = match_multi_number(number, *end, date, end, tm);
830 unsigned long approxidate(const char *date)
837 if (parse_date(date, buffer, sizeof(buffer)) > 0)
838 return strtoul(buffer, NULL, 10);
840 gettimeofday(&tv, NULL);
841 localtime_r(&tv.tv_sec, &tm);
844 unsigned char c = *date;
849 date = approxidate_digit(date-1, &tm, &number);
853 date = approxidate_alpha(date-1, &tm, &number);
855 if (number > 0 && number < 32)
857 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)