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 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(unsigned long time, int tz, enum date_mode mode)
92 static char timebuf[200];
94 if (mode == DATE_RAW) {
95 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
99 if (mode == DATE_RELATIVE) {
102 gettimeofday(&now, NULL);
103 if (now.tv_sec < time)
104 return "in the future";
105 diff = now.tv_sec - time;
107 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
110 /* Turn it into minutes */
111 diff = (diff + 30) / 60;
113 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
116 /* Turn it into hours */
117 diff = (diff + 30) / 60;
119 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
122 /* We deal with number of days from here on */
123 diff = (diff + 12) / 24;
125 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
128 /* Say weeks for the past 10 weeks or so */
130 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
133 /* Say months for the past 12 months or so */
135 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
138 /* Give years and months for 5 years or so */
140 unsigned long years = diff / 365;
141 unsigned long months = (diff % 365 + 15) / 30;
143 n = snprintf(timebuf, sizeof(timebuf), "%lu year%s",
144 years, (years > 1 ? "s" : ""));
146 snprintf(timebuf + n, sizeof(timebuf) - n,
148 months, (months > 1 ? "s" : ""));
150 snprintf(timebuf + n, sizeof(timebuf) - n,
154 /* Otherwise, just years. Centuries is probably overkill. */
155 snprintf(timebuf, sizeof(timebuf), "%lu years ago", (diff + 183) / 365);
159 if (mode == DATE_LOCAL)
160 tz = local_tzoffset(time);
162 tm = time_to_tm(time, tz);
165 if (mode == DATE_SHORT)
166 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
167 tm->tm_mon + 1, tm->tm_mday);
168 else if (mode == DATE_ISO8601)
169 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
173 tm->tm_hour, tm->tm_min, tm->tm_sec,
175 else if (mode == DATE_RFC2822)
176 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
177 weekday_names[tm->tm_wday], tm->tm_mday,
178 month_names[tm->tm_mon], tm->tm_year + 1900,
179 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
181 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
182 weekday_names[tm->tm_wday],
183 month_names[tm->tm_mon],
185 tm->tm_hour, tm->tm_min, tm->tm_sec,
187 (mode == DATE_LOCAL) ? 0 : ' ',
193 * Check these. And note how it doesn't do the summer-time conversion.
195 * In my world, it's always summer, and things are probably a bit off
198 static const struct {
202 } timezone_names[] = {
203 { "IDLW", -12, 0, }, /* International Date Line West */
204 { "NT", -11, 0, }, /* Nome */
205 { "CAT", -10, 0, }, /* Central Alaska */
206 { "HST", -10, 0, }, /* Hawaii Standard */
207 { "HDT", -10, 1, }, /* Hawaii Daylight */
208 { "YST", -9, 0, }, /* Yukon Standard */
209 { "YDT", -9, 1, }, /* Yukon Daylight */
210 { "PST", -8, 0, }, /* Pacific Standard */
211 { "PDT", -8, 1, }, /* Pacific Daylight */
212 { "MST", -7, 0, }, /* Mountain Standard */
213 { "MDT", -7, 1, }, /* Mountain Daylight */
214 { "CST", -6, 0, }, /* Central Standard */
215 { "CDT", -6, 1, }, /* Central Daylight */
216 { "EST", -5, 0, }, /* Eastern Standard */
217 { "EDT", -5, 1, }, /* Eastern Daylight */
218 { "AST", -3, 0, }, /* Atlantic Standard */
219 { "ADT", -3, 1, }, /* Atlantic Daylight */
220 { "WAT", -1, 0, }, /* West Africa */
222 { "GMT", 0, 0, }, /* Greenwich Mean */
223 { "UTC", 0, 0, }, /* Universal (Coordinated) */
225 { "WET", 0, 0, }, /* Western European */
226 { "BST", 0, 1, }, /* British Summer */
227 { "CET", +1, 0, }, /* Central European */
228 { "MET", +1, 0, }, /* Middle European */
229 { "MEWT", +1, 0, }, /* Middle European Winter */
230 { "MEST", +1, 1, }, /* Middle European Summer */
231 { "CEST", +1, 1, }, /* Central European Summer */
232 { "MESZ", +1, 1, }, /* Middle European Summer */
233 { "FWT", +1, 0, }, /* French Winter */
234 { "FST", +1, 1, }, /* French Summer */
235 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
236 { "EEST", +2, 1, }, /* Eastern European Daylight */
237 { "WAST", +7, 0, }, /* West Australian Standard */
238 { "WADT", +7, 1, }, /* West Australian Daylight */
239 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
240 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
241 { "EAST", +10, 0, }, /* Eastern Australian Standard */
242 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
243 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
244 { "NZT", +12, 0, }, /* New Zealand */
245 { "NZST", +12, 0, }, /* New Zealand Standard */
246 { "NZDT", +12, 1, }, /* New Zealand Daylight */
247 { "IDLE", +12, 0, }, /* International Date Line East */
250 static int match_string(const char *date, const char *str)
254 for (i = 0; *date; date++, str++, i++) {
257 if (toupper(*date) == toupper(*str))
266 static int skip_alpha(const char *date)
271 } while (isalpha(date[i]));
276 * Parse month, weekday, or timezone name
278 static int match_alpha(const char *date, struct tm *tm, int *offset)
282 for (i = 0; i < 12; i++) {
283 int match = match_string(date, month_names[i]);
290 for (i = 0; i < 7; i++) {
291 int match = match_string(date, weekday_names[i]);
298 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
299 int match = match_string(date, timezone_names[i].name);
301 int off = timezone_names[i].offset;
303 /* This is bogus, but we like summer */
304 off += timezone_names[i].dst;
306 /* Only use the tz name offset if we don't have anything better */
314 if (match_string(date, "PM") == 2) {
315 tm->tm_hour = (tm->tm_hour % 12) + 12;
319 if (match_string(date, "AM") == 2) {
320 tm->tm_hour = (tm->tm_hour % 12) + 0;
325 return skip_alpha(date);
328 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
330 if (month > 0 && month < 13 && day > 0 && day < 32) {
331 struct tm check = *tm;
332 struct tm *r = (now_tm ? &check : tm);
335 r->tm_mon = month - 1;
340 r->tm_year = now_tm->tm_year;
342 else if (year >= 1970 && year < 2100)
343 r->tm_year = year - 1900;
344 else if (year > 70 && year < 100)
347 r->tm_year = year + 100;
353 specified = tm_to_time_t(r);
355 /* Be it commit time or author time, it does not make
356 * sense to specify timestamp way into the future. Make
357 * sure it is not later than ten days from now...
359 if (now + 10*24*3600 < specified)
361 tm->tm_mon = r->tm_mon;
362 tm->tm_mday = r->tm_mday;
364 tm->tm_year = r->tm_year;
370 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
374 struct tm *refuse_future;
377 num2 = strtol(end+1, &end, 10);
379 if (*end == c && isdigit(end[1]))
380 num3 = strtol(end+1, &end, 10);
387 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
399 refuse_future = NULL;
400 if (gmtime_r(&now, &now_tm))
401 refuse_future = &now_tm;
405 if (is_date(num, num2, num3, refuse_future, now, tm))
408 if (is_date(num, num3, num2, refuse_future, now, tm))
411 /* Our eastern European friends say dd.mm.yy[yy]
412 * is the norm there, so giving precedence to
413 * mm/dd/yy[yy] form only when separator is not '.'
416 is_date(num3, num, num2, refuse_future, now, tm))
418 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
419 if (is_date(num3, num2, num, refuse_future, now, tm))
421 /* Funny European mm.dd.yy */
423 is_date(num3, num, num2, refuse_future, now, tm))
431 * Have we filled in any part of the time/date yet?
432 * We just do a binary 'and' to see if the sign bit
433 * is set in all the values.
435 static inline int nodate(struct tm *tm)
437 return (tm->tm_year &
446 * We've seen a digit. Time? Year? Date?
448 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
454 num = strtoul(date, &end, 10);
457 * Seconds since 1970? We trigger on that for any numbers with
458 * more than 8 digits. This is because we don't want to rule out
459 * numbers like 20070606 as a YYYYMMDD date.
461 if (num >= 100000000 && nodate(tm)) {
463 if (gmtime_r(&time, tm)) {
470 * Check for special formats: num[-.:/]num[same]num
477 if (isdigit(end[1])) {
478 int match = match_multi_number(num, *end, date, end, tm);
485 * None of the special formats? Try to guess what
486 * the number meant. We use the number of digits
487 * to make a more educated guess..
492 } while (isdigit(date[n]));
494 /* Four-digit year or a timezone? */
496 if (num <= 1400 && *offset == -1) {
497 unsigned int minutes = num % 100;
498 unsigned int hours = num / 100;
499 *offset = hours*60 + minutes;
500 } else if (num > 1900 && num < 2100)
501 tm->tm_year = num - 1900;
506 * Ignore lots of numerals. We took care of 4-digit years above.
507 * Days or months must be one or two digits.
513 * NOTE! We will give precedence to day-of-month over month or
514 * year numbers in the 1-12 range. So 05 is always "mday 5",
515 * unless we already have a mday..
517 * IOW, 01 Apr 05 parses as "April 1st, 2005".
519 if (num > 0 && num < 32 && tm->tm_mday < 0) {
524 /* Two-digit year? */
525 if (n == 2 && tm->tm_year < 0) {
526 if (num < 10 && tm->tm_mday >= 0) {
527 tm->tm_year = num + 100;
536 if (num > 0 && num < 13 && tm->tm_mon < 0)
542 static int match_tz(const char *date, int *offp)
545 int offset = strtoul(date+1, &end, 10);
547 int n = end - date - 1;
553 * Don't accept any random crap.. At least 3 digits, and
554 * a valid minute. We might want to check that the minutes
555 * are divisible by 30 or something too.
557 if (min < 60 && n > 2) {
558 offset = hour*60+min;
567 static int date_string(unsigned long date, int offset, char *buf, int len)
575 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
578 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
579 (i.e. English) day/month names, and it doesn't work correctly with %z. */
580 int parse_date(const char *date, char *result, int maxlen)
586 memset(&tm, 0, sizeof(tm));
599 unsigned char c = *date;
601 /* Stop at end of string or newline */
606 match = match_alpha(date, &tm, &offset);
608 match = match_digit(date, &tm, &offset, &tm_gmt);
609 else if ((c == '-' || c == '+') && isdigit(date[1]))
610 match = match_tz(date, &offset);
620 /* mktime uses local timezone */
621 then = tm_to_time_t(&tm);
623 offset = (then - mktime(&tm)) / 60;
630 return date_string(then, offset, result, maxlen);
633 enum date_mode parse_date_format(const char *format)
635 if (!strcmp(format, "relative"))
636 return DATE_RELATIVE;
637 else if (!strcmp(format, "iso8601") ||
638 !strcmp(format, "iso"))
640 else if (!strcmp(format, "rfc2822") ||
641 !strcmp(format, "rfc"))
643 else if (!strcmp(format, "short"))
645 else if (!strcmp(format, "local"))
647 else if (!strcmp(format, "default"))
649 else if (!strcmp(format, "raw"))
652 die("unknown date format %s", format);
655 void datestamp(char *buf, int bufsize)
662 offset = tm_to_time_t(localtime(&now)) - now;
665 date_string(now, offset, buf, bufsize);
669 * Relative time update (eg "2 days ago"). If we haven't set the time
670 * yet, we need to set it from current time.
672 static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
677 tm->tm_mday = now->tm_mday;
679 tm->tm_mon = now->tm_mon;
680 if (tm->tm_year < 0) {
681 tm->tm_year = now->tm_year;
682 if (tm->tm_mon > now->tm_mon)
686 n = mktime(tm) - sec;
691 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
693 update_tm(tm, now, 24*60*60);
696 static void date_time(struct tm *tm, struct tm *now, int hour)
698 if (tm->tm_hour < hour)
699 date_yesterday(tm, now, NULL);
705 static void date_midnight(struct tm *tm, struct tm *now, int *num)
707 date_time(tm, now, 0);
710 static void date_noon(struct tm *tm, struct tm *now, int *num)
712 date_time(tm, now, 12);
715 static void date_tea(struct tm *tm, struct tm *now, int *num)
717 date_time(tm, now, 17);
720 static void date_pm(struct tm *tm, struct tm *now, int *num)
731 tm->tm_hour = (hour % 12) + 12;
734 static void date_am(struct tm *tm, struct tm *now, int *num)
745 tm->tm_hour = (hour % 12);
748 static void date_never(struct tm *tm, struct tm *now, int *num)
754 static const struct special {
756 void (*fn)(struct tm *, struct tm *, int *);
758 { "yesterday", date_yesterday },
759 { "noon", date_noon },
760 { "midnight", date_midnight },
764 { "never", date_never },
768 static const char *number_name[] = {
769 "zero", "one", "two", "three", "four",
770 "five", "six", "seven", "eight", "nine", "ten",
773 static const struct typelen {
780 { "days", 24*60*60 },
781 { "weeks", 7*24*60*60 },
785 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num)
787 const struct typelen *tl;
788 const struct special *s;
789 const char *end = date;
792 while (isalpha(*++end));
795 for (i = 0; i < 12; i++) {
796 int match = match_string(date, month_names[i]);
803 for (s = special; s->name; s++) {
804 int len = strlen(s->name);
805 if (match_string(date, s->name) == len) {
812 for (i = 1; i < 11; i++) {
813 int len = strlen(number_name[i]);
814 if (match_string(date, number_name[i]) == len) {
819 if (match_string(date, "last") == 4)
826 int len = strlen(tl->type);
827 if (match_string(date, tl->type) >= len-1) {
828 update_tm(tm, now, tl->length * *num);
835 for (i = 0; i < 7; i++) {
836 int match = match_string(date, weekday_names[i]);
838 int diff, n = *num -1;
841 diff = tm->tm_wday - i;
846 update_tm(tm, now, diff * 24 * 60 * 60);
851 if (match_string(date, "months") >= 5) {
852 int n = tm->tm_mon - *num;
862 if (match_string(date, "years") >= 4) {
871 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
874 unsigned long number = strtoul(date, &end, 10);
881 if (isdigit(end[1])) {
882 int match = match_multi_number(number, *end, date, end, tm);
888 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
889 if (date[0] != '0' || end - date <= 2)
895 * Do we have a pending number at the end, or when
896 * we see a new one? Let's assume it's a month day,
897 * as in "Dec 6, 1992"
899 static void pending_number(struct tm *tm, int *num)
905 if (tm->tm_mday < 0 && number < 32)
906 tm->tm_mday = number;
907 else if (tm->tm_mon < 0 && number < 13)
908 tm->tm_mon = number-1;
909 else if (tm->tm_year < 0) {
910 if (number > 1969 && number < 2100)
911 tm->tm_year = number - 1900;
912 else if (number > 69 && number < 100)
913 tm->tm_year = number;
914 else if (number < 38)
915 tm->tm_year = 100 + number;
916 /* We screw up for number = 00 ? */
921 unsigned long approxidate(const char *date)
929 if (parse_date(date, buffer, sizeof(buffer)) > 0)
930 return strtoul(buffer, NULL, 10);
932 gettimeofday(&tv, NULL);
933 time_sec = tv.tv_sec;
934 localtime_r(&time_sec, &tm);
942 unsigned char c = *date;
947 pending_number(&tm, &number);
948 date = approxidate_digit(date-1, &tm, &number);
952 date = approxidate_alpha(date-1, &tm, &now, &number);
954 pending_number(&tm, &number);
955 return update_tm(&tm, &now, 0);