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 static 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 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
29 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
30 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
33 static const char *month_names[] = {
34 "January", "February", "March", "April", "May", "June",
35 "July", "August", "September", "October", "November", "December"
38 static const char *weekday_names[] = {
39 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
42 static time_t gm_time_t(unsigned long time, int tz)
46 minutes = tz < 0 ? -tz : tz;
47 minutes = (minutes / 100)*60 + (minutes % 100);
48 minutes = tz < 0 ? -minutes : minutes;
49 return time + minutes * 60;
53 * The "tz" thing is passed in as this strange "decimal parse of tz"
54 * thing, which means that tz -0100 is passed in as the integer -100,
55 * even though it means "sixty minutes off"
57 static struct tm *time_to_tm(unsigned long time, int tz)
59 time_t t = gm_time_t(time, tz);
64 * What value of "tz" was in effect back then at "time" in the
67 static int local_tzoffset(unsigned long time)
75 t_local = tm_to_time_t(&tm);
84 offset /= 60; /* in minutes */
85 offset = (offset % 60) + ((offset / 60) * 100);
86 return offset * eastwest;
89 const char *show_date_relative(unsigned long time, int tz,
90 const struct timeval *now,
95 if (now->tv_sec < time)
96 return "in the future";
97 diff = now->tv_sec - time;
99 snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
102 /* Turn it into minutes */
103 diff = (diff + 30) / 60;
105 snprintf(timebuf, timebuf_size, "%lu minutes ago", diff);
108 /* Turn it into hours */
109 diff = (diff + 30) / 60;
111 snprintf(timebuf, timebuf_size, "%lu hours ago", diff);
114 /* We deal with number of days from here on */
115 diff = (diff + 12) / 24;
117 snprintf(timebuf, timebuf_size, "%lu days ago", diff);
120 /* Say weeks for the past 10 weeks or so */
122 snprintf(timebuf, timebuf_size, "%lu weeks ago", (diff + 3) / 7);
125 /* Say months for the past 12 months or so */
127 snprintf(timebuf, timebuf_size, "%lu months ago", (diff + 15) / 30);
130 /* Give years and months for 5 years or so */
132 unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
133 unsigned long years = totalmonths / 12;
134 unsigned long months = totalmonths % 12;
136 n = snprintf(timebuf, timebuf_size, "%lu year%s",
137 years, (years > 1 ? "s" : ""));
139 snprintf(timebuf + n, timebuf_size - n,
141 months, (months > 1 ? "s" : ""));
143 snprintf(timebuf + n, timebuf_size - n, " ago");
146 /* Otherwise, just years. Centuries is probably overkill. */
147 snprintf(timebuf, timebuf_size, "%lu years ago", (diff + 183) / 365);
151 const char *show_date(unsigned long time, int tz, enum date_mode mode)
154 static char timebuf[200];
156 if (mode == DATE_RAW) {
157 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
161 if (mode == DATE_RELATIVE) {
163 gettimeofday(&now, NULL);
164 return show_date_relative(time, tz, &now,
165 timebuf, sizeof(timebuf));
168 if (mode == DATE_LOCAL)
169 tz = local_tzoffset(time);
171 tm = time_to_tm(time, tz);
174 if (mode == DATE_SHORT)
175 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
176 tm->tm_mon + 1, tm->tm_mday);
177 else if (mode == DATE_ISO8601)
178 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
182 tm->tm_hour, tm->tm_min, tm->tm_sec,
184 else if (mode == DATE_RFC2822)
185 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
186 weekday_names[tm->tm_wday], tm->tm_mday,
187 month_names[tm->tm_mon], tm->tm_year + 1900,
188 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
190 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
191 weekday_names[tm->tm_wday],
192 month_names[tm->tm_mon],
194 tm->tm_hour, tm->tm_min, tm->tm_sec,
196 (mode == DATE_LOCAL) ? 0 : ' ',
202 * Check these. And note how it doesn't do the summer-time conversion.
204 * In my world, it's always summer, and things are probably a bit off
207 static const struct {
211 } timezone_names[] = {
212 { "IDLW", -12, 0, }, /* International Date Line West */
213 { "NT", -11, 0, }, /* Nome */
214 { "CAT", -10, 0, }, /* Central Alaska */
215 { "HST", -10, 0, }, /* Hawaii Standard */
216 { "HDT", -10, 1, }, /* Hawaii Daylight */
217 { "YST", -9, 0, }, /* Yukon Standard */
218 { "YDT", -9, 1, }, /* Yukon Daylight */
219 { "PST", -8, 0, }, /* Pacific Standard */
220 { "PDT", -8, 1, }, /* Pacific Daylight */
221 { "MST", -7, 0, }, /* Mountain Standard */
222 { "MDT", -7, 1, }, /* Mountain Daylight */
223 { "CST", -6, 0, }, /* Central Standard */
224 { "CDT", -6, 1, }, /* Central Daylight */
225 { "EST", -5, 0, }, /* Eastern Standard */
226 { "EDT", -5, 1, }, /* Eastern Daylight */
227 { "AST", -3, 0, }, /* Atlantic Standard */
228 { "ADT", -3, 1, }, /* Atlantic Daylight */
229 { "WAT", -1, 0, }, /* West Africa */
231 { "GMT", 0, 0, }, /* Greenwich Mean */
232 { "UTC", 0, 0, }, /* Universal (Coordinated) */
233 { "Z", 0, 0, }, /* Zulu, alias for UTC */
235 { "WET", 0, 0, }, /* Western European */
236 { "BST", 0, 1, }, /* British Summer */
237 { "CET", +1, 0, }, /* Central European */
238 { "MET", +1, 0, }, /* Middle European */
239 { "MEWT", +1, 0, }, /* Middle European Winter */
240 { "MEST", +1, 1, }, /* Middle European Summer */
241 { "CEST", +1, 1, }, /* Central European Summer */
242 { "MESZ", +1, 1, }, /* Middle European Summer */
243 { "FWT", +1, 0, }, /* French Winter */
244 { "FST", +1, 1, }, /* French Summer */
245 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
246 { "EEST", +2, 1, }, /* Eastern European Daylight */
247 { "WAST", +7, 0, }, /* West Australian Standard */
248 { "WADT", +7, 1, }, /* West Australian Daylight */
249 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
250 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
251 { "EAST", +10, 0, }, /* Eastern Australian Standard */
252 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
253 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
254 { "NZT", +12, 0, }, /* New Zealand */
255 { "NZST", +12, 0, }, /* New Zealand Standard */
256 { "NZDT", +12, 1, }, /* New Zealand Daylight */
257 { "IDLE", +12, 0, }, /* International Date Line East */
260 static int match_string(const char *date, const char *str)
264 for (i = 0; *date; date++, str++, i++) {
267 if (toupper(*date) == toupper(*str))
276 static int skip_alpha(const char *date)
281 } while (isalpha(date[i]));
286 * Parse month, weekday, or timezone name
288 static int match_alpha(const char *date, struct tm *tm, int *offset)
292 for (i = 0; i < 12; i++) {
293 int match = match_string(date, month_names[i]);
300 for (i = 0; i < 7; i++) {
301 int match = match_string(date, weekday_names[i]);
308 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
309 int match = match_string(date, timezone_names[i].name);
310 if (match >= 3 || match == strlen(timezone_names[i].name)) {
311 int off = timezone_names[i].offset;
313 /* This is bogus, but we like summer */
314 off += timezone_names[i].dst;
316 /* Only use the tz name offset if we don't have anything better */
324 if (match_string(date, "PM") == 2) {
325 tm->tm_hour = (tm->tm_hour % 12) + 12;
329 if (match_string(date, "AM") == 2) {
330 tm->tm_hour = (tm->tm_hour % 12) + 0;
335 return skip_alpha(date);
338 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
340 if (month > 0 && month < 13 && day > 0 && day < 32) {
341 struct tm check = *tm;
342 struct tm *r = (now_tm ? &check : tm);
345 r->tm_mon = month - 1;
350 r->tm_year = now_tm->tm_year;
352 else if (year >= 1970 && year < 2100)
353 r->tm_year = year - 1900;
354 else if (year > 70 && year < 100)
357 r->tm_year = year + 100;
363 specified = tm_to_time_t(r);
365 /* Be it commit time or author time, it does not make
366 * sense to specify timestamp way into the future. Make
367 * sure it is not later than ten days from now...
369 if (now + 10*24*3600 < specified)
371 tm->tm_mon = r->tm_mon;
372 tm->tm_mday = r->tm_mday;
374 tm->tm_year = r->tm_year;
380 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
384 struct tm *refuse_future;
387 num2 = strtol(end+1, &end, 10);
389 if (*end == c && isdigit(end[1]))
390 num3 = strtol(end+1, &end, 10);
397 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
409 refuse_future = NULL;
410 if (gmtime_r(&now, &now_tm))
411 refuse_future = &now_tm;
415 if (is_date(num, num2, num3, refuse_future, now, tm))
418 if (is_date(num, num3, num2, refuse_future, now, tm))
421 /* Our eastern European friends say dd.mm.yy[yy]
422 * is the norm there, so giving precedence to
423 * mm/dd/yy[yy] form only when separator is not '.'
426 is_date(num3, num, num2, refuse_future, now, tm))
428 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
429 if (is_date(num3, num2, num, refuse_future, now, tm))
431 /* Funny European mm.dd.yy */
433 is_date(num3, num, num2, refuse_future, now, tm))
441 * Have we filled in any part of the time/date yet?
442 * We just do a binary 'and' to see if the sign bit
443 * is set in all the values.
445 static inline int nodate(struct tm *tm)
447 return (tm->tm_year &
456 * We've seen a digit. Time? Year? Date?
458 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
464 num = strtoul(date, &end, 10);
467 * Seconds since 1970? We trigger on that for any numbers with
468 * more than 8 digits. This is because we don't want to rule out
469 * numbers like 20070606 as a YYYYMMDD date.
471 if (num >= 100000000 && nodate(tm)) {
473 if (gmtime_r(&time, tm)) {
480 * Check for special formats: num[-.:/]num[same]num
487 if (isdigit(end[1])) {
488 int match = match_multi_number(num, *end, date, end, tm);
495 * None of the special formats? Try to guess what
496 * the number meant. We use the number of digits
497 * to make a more educated guess..
502 } while (isdigit(date[n]));
504 /* Four-digit year or a timezone? */
506 if (num <= 1400 && *offset == -1) {
507 unsigned int minutes = num % 100;
508 unsigned int hours = num / 100;
509 *offset = hours*60 + minutes;
510 } else if (num > 1900 && num < 2100)
511 tm->tm_year = num - 1900;
516 * Ignore lots of numerals. We took care of 4-digit years above.
517 * Days or months must be one or two digits.
523 * NOTE! We will give precedence to day-of-month over month or
524 * year numbers in the 1-12 range. So 05 is always "mday 5",
525 * unless we already have a mday..
527 * IOW, 01 Apr 05 parses as "April 1st, 2005".
529 if (num > 0 && num < 32 && tm->tm_mday < 0) {
534 /* Two-digit year? */
535 if (n == 2 && tm->tm_year < 0) {
536 if (num < 10 && tm->tm_mday >= 0) {
537 tm->tm_year = num + 100;
546 if (num > 0 && num < 13 && tm->tm_mon < 0)
552 static int match_tz(const char *date, int *offp)
555 int offset = strtoul(date+1, &end, 10);
557 int n = end - date - 1;
563 * Don't accept any random crap.. At least 3 digits, and
564 * a valid minute. We might want to check that the minutes
565 * are divisible by 30 or something too.
567 if (min < 60 && n > 2) {
568 offset = hour*60+min;
577 static int date_string(unsigned long date, int offset, char *buf, int len)
585 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
588 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
589 (i.e. English) day/month names, and it doesn't work correctly with %z. */
590 int parse_date_basic(const char *date, unsigned long *timestamp, int *offset)
594 unsigned long dummy_timestamp;
598 timestamp = &dummy_timestamp;
600 offset = &dummy_offset;
602 memset(&tm, 0, sizeof(tm));
615 unsigned char c = *date;
617 /* Stop at end of string or newline */
622 match = match_alpha(date, &tm, offset);
624 match = match_digit(date, &tm, offset, &tm_gmt);
625 else if ((c == '-' || c == '+') && isdigit(date[1]))
626 match = match_tz(date, offset);
636 /* mktime uses local timezone */
637 *timestamp = tm_to_time_t(&tm);
639 *offset = ((time_t)*timestamp - mktime(&tm)) / 60;
641 if (*timestamp == -1)
645 *timestamp -= *offset * 60;
646 return 0; /* success */
649 int parse_date(const char *date, char *result, int maxlen)
651 unsigned long timestamp;
653 if (parse_date_basic(date, ×tamp, &offset))
655 return date_string(timestamp, offset, result, maxlen);
658 enum date_mode parse_date_format(const char *format)
660 if (!strcmp(format, "relative"))
661 return DATE_RELATIVE;
662 else if (!strcmp(format, "iso8601") ||
663 !strcmp(format, "iso"))
665 else if (!strcmp(format, "rfc2822") ||
666 !strcmp(format, "rfc"))
668 else if (!strcmp(format, "short"))
670 else if (!strcmp(format, "local"))
672 else if (!strcmp(format, "default"))
674 else if (!strcmp(format, "raw"))
677 die("unknown date format %s", format);
680 void datestamp(char *buf, int bufsize)
687 offset = tm_to_time_t(localtime(&now)) - now;
690 date_string(now, offset, buf, bufsize);
694 * Relative time update (eg "2 days ago"). If we haven't set the time
695 * yet, we need to set it from current time.
697 static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
702 tm->tm_mday = now->tm_mday;
704 tm->tm_mon = now->tm_mon;
705 if (tm->tm_year < 0) {
706 tm->tm_year = now->tm_year;
707 if (tm->tm_mon > now->tm_mon)
711 n = mktime(tm) - sec;
716 static void date_now(struct tm *tm, struct tm *now, int *num)
718 update_tm(tm, now, 0);
721 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
723 update_tm(tm, now, 24*60*60);
726 static void date_time(struct tm *tm, struct tm *now, int hour)
728 if (tm->tm_hour < hour)
729 date_yesterday(tm, now, NULL);
735 static void date_midnight(struct tm *tm, struct tm *now, int *num)
737 date_time(tm, now, 0);
740 static void date_noon(struct tm *tm, struct tm *now, int *num)
742 date_time(tm, now, 12);
745 static void date_tea(struct tm *tm, struct tm *now, int *num)
747 date_time(tm, now, 17);
750 static void date_pm(struct tm *tm, struct tm *now, int *num)
761 tm->tm_hour = (hour % 12) + 12;
764 static void date_am(struct tm *tm, struct tm *now, int *num)
775 tm->tm_hour = (hour % 12);
778 static void date_never(struct tm *tm, struct tm *now, int *num)
784 static const struct special {
786 void (*fn)(struct tm *, struct tm *, int *);
788 { "yesterday", date_yesterday },
789 { "noon", date_noon },
790 { "midnight", date_midnight },
794 { "never", date_never },
799 static const char *number_name[] = {
800 "zero", "one", "two", "three", "four",
801 "five", "six", "seven", "eight", "nine", "ten",
804 static const struct typelen {
811 { "days", 24*60*60 },
812 { "weeks", 7*24*60*60 },
816 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
818 const struct typelen *tl;
819 const struct special *s;
820 const char *end = date;
823 while (isalpha(*++end));
826 for (i = 0; i < 12; i++) {
827 int match = match_string(date, month_names[i]);
835 for (s = special; s->name; s++) {
836 int len = strlen(s->name);
837 if (match_string(date, s->name) == len) {
845 for (i = 1; i < 11; i++) {
846 int len = strlen(number_name[i]);
847 if (match_string(date, number_name[i]) == len) {
853 if (match_string(date, "last") == 4) {
862 int len = strlen(tl->type);
863 if (match_string(date, tl->type) >= len-1) {
864 update_tm(tm, now, tl->length * *num);
872 for (i = 0; i < 7; i++) {
873 int match = match_string(date, weekday_names[i]);
875 int diff, n = *num -1;
878 diff = tm->tm_wday - i;
883 update_tm(tm, now, diff * 24 * 60 * 60);
889 if (match_string(date, "months") >= 5) {
891 update_tm(tm, now, 0); /* fill in date fields if needed */
892 n = tm->tm_mon - *num;
903 if (match_string(date, "years") >= 4) {
904 update_tm(tm, now, 0); /* fill in date fields if needed */
914 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
917 unsigned long number = strtoul(date, &end, 10);
924 if (isdigit(end[1])) {
925 int match = match_multi_number(number, *end, date, end, tm);
931 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
932 if (date[0] != '0' || end - date <= 2)
938 * Do we have a pending number at the end, or when
939 * we see a new one? Let's assume it's a month day,
940 * as in "Dec 6, 1992"
942 static void pending_number(struct tm *tm, int *num)
948 if (tm->tm_mday < 0 && number < 32)
949 tm->tm_mday = number;
950 else if (tm->tm_mon < 0 && number < 13)
951 tm->tm_mon = number-1;
952 else if (tm->tm_year < 0) {
953 if (number > 1969 && number < 2100)
954 tm->tm_year = number - 1900;
955 else if (number > 69 && number < 100)
956 tm->tm_year = number;
957 else if (number < 38)
958 tm->tm_year = 100 + number;
959 /* We screw up for number = 00 ? */
964 static unsigned long approxidate_str(const char *date,
965 const struct timeval *tv,
973 time_sec = tv->tv_sec;
974 localtime_r(&time_sec, &tm);
982 unsigned char c = *date;
987 pending_number(&tm, &number);
988 date = approxidate_digit(date-1, &tm, &number);
993 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
995 pending_number(&tm, &number);
998 return update_tm(&tm, &now, 0);
1001 unsigned long approxidate_relative(const char *date, const struct timeval *tv)
1003 unsigned long timestamp;
1007 if (!parse_date_basic(date, ×tamp, &offset))
1009 return approxidate_str(date, tv, &errors);
1012 unsigned long approxidate_careful(const char *date, int *error_ret)
1015 unsigned long timestamp;
1021 if (!parse_date_basic(date, ×tamp, &offset)) {
1026 gettimeofday(&tv, NULL);
1027 return approxidate_str(date, &tv, error_ret);