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_RAW) {
93 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
97 if (mode == DATE_RELATIVE) {
100 gettimeofday(&now, NULL);
101 if (now.tv_sec < time)
102 return "in the future";
103 diff = now.tv_sec - time;
105 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
108 /* Turn it into minutes */
109 diff = (diff + 30) / 60;
111 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
114 /* Turn it into hours */
115 diff = (diff + 30) / 60;
117 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
120 /* We deal with number of days from here on */
121 diff = (diff + 12) / 24;
123 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
126 /* Say weeks for the past 10 weeks or so */
128 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
131 /* Say months for the past 12 months or so */
133 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
136 /* Give years and months for 5 years or so */
138 unsigned long years = diff / 365;
139 unsigned long months = (diff % 365 + 15) / 30;
141 n = snprintf(timebuf, sizeof(timebuf), "%lu year%s",
142 years, (years > 1 ? "s" : ""));
144 snprintf(timebuf + n, sizeof(timebuf) - n,
146 months, (months > 1 ? "s" : ""));
148 snprintf(timebuf + n, sizeof(timebuf) - n,
152 /* Otherwise, just years. Centuries is probably overkill. */
153 snprintf(timebuf, sizeof(timebuf), "%lu years ago", (diff + 183) / 365);
157 if (mode == DATE_LOCAL)
158 tz = local_tzoffset(time);
160 tm = time_to_tm(time, tz);
163 if (mode == DATE_SHORT)
164 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
165 tm->tm_mon + 1, tm->tm_mday);
166 else if (mode == DATE_ISO8601)
167 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
171 tm->tm_hour, tm->tm_min, tm->tm_sec,
173 else if (mode == DATE_RFC2822)
174 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
175 weekday_names[tm->tm_wday], tm->tm_mday,
176 month_names[tm->tm_mon], tm->tm_year + 1900,
177 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
179 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
180 weekday_names[tm->tm_wday],
181 month_names[tm->tm_mon],
183 tm->tm_hour, tm->tm_min, tm->tm_sec,
185 (mode == DATE_LOCAL) ? 0 : ' ',
191 * Check these. And note how it doesn't do the summer-time conversion.
193 * In my world, it's always summer, and things are probably a bit off
196 static const struct {
200 } timezone_names[] = {
201 { "IDLW", -12, 0, }, /* International Date Line West */
202 { "NT", -11, 0, }, /* Nome */
203 { "CAT", -10, 0, }, /* Central Alaska */
204 { "HST", -10, 0, }, /* Hawaii Standard */
205 { "HDT", -10, 1, }, /* Hawaii Daylight */
206 { "YST", -9, 0, }, /* Yukon Standard */
207 { "YDT", -9, 1, }, /* Yukon Daylight */
208 { "PST", -8, 0, }, /* Pacific Standard */
209 { "PDT", -8, 1, }, /* Pacific Daylight */
210 { "MST", -7, 0, }, /* Mountain Standard */
211 { "MDT", -7, 1, }, /* Mountain Daylight */
212 { "CST", -6, 0, }, /* Central Standard */
213 { "CDT", -6, 1, }, /* Central Daylight */
214 { "EST", -5, 0, }, /* Eastern Standard */
215 { "EDT", -5, 1, }, /* Eastern Daylight */
216 { "AST", -3, 0, }, /* Atlantic Standard */
217 { "ADT", -3, 1, }, /* Atlantic Daylight */
218 { "WAT", -1, 0, }, /* West Africa */
220 { "GMT", 0, 0, }, /* Greenwich Mean */
221 { "UTC", 0, 0, }, /* Universal (Coordinated) */
223 { "WET", 0, 0, }, /* Western European */
224 { "BST", 0, 1, }, /* British Summer */
225 { "CET", +1, 0, }, /* Central European */
226 { "MET", +1, 0, }, /* Middle European */
227 { "MEWT", +1, 0, }, /* Middle European Winter */
228 { "MEST", +1, 1, }, /* Middle European Summer */
229 { "CEST", +1, 1, }, /* Central European Summer */
230 { "MESZ", +1, 1, }, /* Middle European Summer */
231 { "FWT", +1, 0, }, /* French Winter */
232 { "FST", +1, 1, }, /* French Summer */
233 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
234 { "EEST", +2, 1, }, /* Eastern European Daylight */
235 { "WAST", +7, 0, }, /* West Australian Standard */
236 { "WADT", +7, 1, }, /* West Australian Daylight */
237 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
238 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
239 { "EAST", +10, 0, }, /* Eastern Australian Standard */
240 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
241 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
242 { "NZT", +12, 0, }, /* New Zealand */
243 { "NZST", +12, 0, }, /* New Zealand Standard */
244 { "NZDT", +12, 1, }, /* New Zealand Daylight */
245 { "IDLE", +12, 0, }, /* International Date Line East */
248 static int match_string(const char *date, const char *str)
252 for (i = 0; *date; date++, str++, i++) {
255 if (toupper(*date) == toupper(*str))
264 static int skip_alpha(const char *date)
269 } while (isalpha(date[i]));
274 * Parse month, weekday, or timezone name
276 static int match_alpha(const char *date, struct tm *tm, int *offset)
280 for (i = 0; i < 12; i++) {
281 int match = match_string(date, month_names[i]);
288 for (i = 0; i < 7; i++) {
289 int match = match_string(date, weekday_names[i]);
296 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
297 int match = match_string(date, timezone_names[i].name);
299 int off = timezone_names[i].offset;
301 /* This is bogus, but we like summer */
302 off += timezone_names[i].dst;
304 /* Only use the tz name offset if we don't have anything better */
312 if (match_string(date, "PM") == 2) {
313 tm->tm_hour = (tm->tm_hour % 12) + 12;
317 if (match_string(date, "AM") == 2) {
318 tm->tm_hour = (tm->tm_hour % 12) + 0;
323 return skip_alpha(date);
326 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
328 if (month > 0 && month < 13 && day > 0 && day < 32) {
329 struct tm check = *tm;
330 struct tm *r = (now_tm ? &check : tm);
333 r->tm_mon = month - 1;
338 r->tm_year = now_tm->tm_year;
340 else if (year >= 1970 && year < 2100)
341 r->tm_year = year - 1900;
342 else if (year > 70 && year < 100)
345 r->tm_year = year + 100;
351 specified = tm_to_time_t(r);
353 /* Be it commit time or author time, it does not make
354 * sense to specify timestamp way into the future. Make
355 * sure it is not later than ten days from now...
357 if (now + 10*24*3600 < specified)
359 tm->tm_mon = r->tm_mon;
360 tm->tm_mday = r->tm_mday;
362 tm->tm_year = r->tm_year;
368 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
372 struct tm *refuse_future;
375 num2 = strtol(end+1, &end, 10);
377 if (*end == c && isdigit(end[1]))
378 num3 = strtol(end+1, &end, 10);
385 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
397 refuse_future = NULL;
398 if (gmtime_r(&now, &now_tm))
399 refuse_future = &now_tm;
403 if (is_date(num, num2, num3, refuse_future, now, tm))
406 if (is_date(num, num3, num2, refuse_future, now, tm))
409 /* Our eastern European friends say dd.mm.yy[yy]
410 * is the norm there, so giving precedence to
411 * mm/dd/yy[yy] form only when separator is not '.'
414 is_date(num3, num, num2, refuse_future, now, tm))
416 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
417 if (is_date(num3, num2, num, refuse_future, now, tm))
419 /* Funny European mm.dd.yy */
421 is_date(num3, num, num2, refuse_future, now, tm))
428 /* Have we filled in any part of the time/date yet? */
429 static inline int nodate(struct tm *tm)
431 return tm->tm_year < 0 &&
434 !(tm->tm_hour | tm->tm_min | tm->tm_sec);
438 * We've seen a digit. Time? Year? Date?
440 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
446 num = strtoul(date, &end, 10);
449 * Seconds since 1970? We trigger on that for any numbers with
450 * more than 8 digits. This is because we don't want to rule out
451 * numbers like 20070606 as a YYYYMMDD date.
453 if (num >= 100000000 && nodate(tm)) {
455 if (gmtime_r(&time, tm)) {
462 * Check for special formats: num[-.:/]num[same]num
469 if (isdigit(end[1])) {
470 int match = match_multi_number(num, *end, date, end, tm);
477 * None of the special formats? Try to guess what
478 * the number meant. We use the number of digits
479 * to make a more educated guess..
484 } while (isdigit(date[n]));
486 /* Four-digit year or a timezone? */
488 if (num <= 1400 && *offset == -1) {
489 unsigned int minutes = num % 100;
490 unsigned int hours = num / 100;
491 *offset = hours*60 + minutes;
492 } else if (num > 1900 && num < 2100)
493 tm->tm_year = num - 1900;
498 * Ignore lots of numerals. We took care of 4-digit years above.
499 * Days or months must be one or two digits.
505 * NOTE! We will give precedence to day-of-month over month or
506 * year numbers in the 1-12 range. So 05 is always "mday 5",
507 * unless we already have a mday..
509 * IOW, 01 Apr 05 parses as "April 1st, 2005".
511 if (num > 0 && num < 32 && tm->tm_mday < 0) {
516 /* Two-digit year? */
517 if (n == 2 && tm->tm_year < 0) {
518 if (num < 10 && tm->tm_mday >= 0) {
519 tm->tm_year = num + 100;
528 if (num > 0 && num < 32) {
530 } else if (num > 0 && num < 13) {
537 static int match_tz(const char *date, int *offp)
540 int offset = strtoul(date+1, &end, 10);
542 int n = end - date - 1;
548 * Don't accept any random crap.. At least 3 digits, and
549 * a valid minute. We might want to check that the minutes
550 * are divisible by 30 or something too.
552 if (min < 60 && n > 2) {
553 offset = hour*60+min;
562 static int date_string(unsigned long date, int offset, char *buf, int len)
570 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
573 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
574 (i.e. English) day/month names, and it doesn't work correctly with %z. */
575 int parse_date(const char *date, char *result, int maxlen)
581 memset(&tm, 0, sizeof(tm));
591 unsigned char c = *date;
593 /* Stop at end of string or newline */
598 match = match_alpha(date, &tm, &offset);
600 match = match_digit(date, &tm, &offset, &tm_gmt);
601 else if ((c == '-' || c == '+') && isdigit(date[1]))
602 match = match_tz(date, &offset);
612 /* mktime uses local timezone */
613 then = tm_to_time_t(&tm);
615 offset = (then - mktime(&tm)) / 60;
622 return date_string(then, offset, result, maxlen);
625 enum date_mode parse_date_format(const char *format)
627 if (!strcmp(format, "relative"))
628 return DATE_RELATIVE;
629 else if (!strcmp(format, "iso8601") ||
630 !strcmp(format, "iso"))
632 else if (!strcmp(format, "rfc2822") ||
633 !strcmp(format, "rfc"))
635 else if (!strcmp(format, "short"))
637 else if (!strcmp(format, "local"))
639 else if (!strcmp(format, "default"))
641 else if (!strcmp(format, "raw"))
644 die("unknown date format %s", format);
647 void datestamp(char *buf, int bufsize)
654 offset = tm_to_time_t(localtime(&now)) - now;
657 date_string(now, offset, buf, bufsize);
660 static void update_tm(struct tm *tm, unsigned long sec)
662 time_t n = mktime(tm) - sec;
666 static void date_yesterday(struct tm *tm, int *num)
668 update_tm(tm, 24*60*60);
671 static void date_time(struct tm *tm, int hour)
673 if (tm->tm_hour < hour)
674 date_yesterday(tm, NULL);
680 static void date_midnight(struct tm *tm, int *num)
685 static void date_noon(struct tm *tm, int *num)
690 static void date_tea(struct tm *tm, int *num)
695 static void date_pm(struct tm *tm, int *num)
706 tm->tm_hour = (hour % 12) + 12;
709 static void date_am(struct tm *tm, int *num)
720 tm->tm_hour = (hour % 12);
723 static void date_never(struct tm *tm, int *num)
729 static const struct special {
731 void (*fn)(struct tm *, int *);
733 { "yesterday", date_yesterday },
734 { "noon", date_noon },
735 { "midnight", date_midnight },
739 { "never", date_never },
743 static const char *number_name[] = {
744 "zero", "one", "two", "three", "four",
745 "five", "six", "seven", "eight", "nine", "ten",
748 static const struct typelen {
755 { "days", 24*60*60 },
756 { "weeks", 7*24*60*60 },
760 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
762 const struct typelen *tl;
763 const struct special *s;
764 const char *end = date;
767 while (isalpha(*++end));
770 for (i = 0; i < 12; i++) {
771 int match = match_string(date, month_names[i]);
778 for (s = special; s->name; s++) {
779 int len = strlen(s->name);
780 if (match_string(date, s->name) == len) {
787 for (i = 1; i < 11; i++) {
788 int len = strlen(number_name[i]);
789 if (match_string(date, number_name[i]) == len) {
794 if (match_string(date, "last") == 4)
801 int len = strlen(tl->type);
802 if (match_string(date, tl->type) >= len-1) {
803 update_tm(tm, tl->length * *num);
810 for (i = 0; i < 7; i++) {
811 int match = match_string(date, weekday_names[i]);
813 int diff, n = *num -1;
816 diff = tm->tm_wday - i;
821 update_tm(tm, diff * 24 * 60 * 60);
826 if (match_string(date, "months") >= 5) {
827 int n = tm->tm_mon - *num;
837 if (match_string(date, "years") >= 4) {
846 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
849 unsigned long number = strtoul(date, &end, 10);
856 if (isdigit(end[1])) {
857 int match = match_multi_number(number, *end, date, end, tm);
863 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
864 if (date[0] != '0' || end - date <= 2)
869 unsigned long approxidate(const char *date)
877 if (parse_date(date, buffer, sizeof(buffer)) > 0)
878 return strtoul(buffer, NULL, 10);
880 gettimeofday(&tv, NULL);
881 time_sec = tv.tv_sec;
882 localtime_r(&time_sec, &tm);
885 unsigned char c = *date;
890 date = approxidate_digit(date-1, &tm, &number);
894 date = approxidate_alpha(date-1, &tm, &number);
896 if (number > 0 && number < 32)
898 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)