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 void show_date_relative(unsigned long time, int tz,
90 const struct timeval *now,
91 struct strbuf *timebuf)
94 if (now->tv_sec < time) {
95 strbuf_addstr(timebuf, _("in the future"));
98 diff = now->tv_sec - time;
101 Q_("%lu second ago", "%lu seconds ago", diff), diff);
104 /* Turn it into minutes */
105 diff = (diff + 30) / 60;
108 Q_("%lu minute ago", "%lu minutes ago", diff), diff);
111 /* Turn it into hours */
112 diff = (diff + 30) / 60;
115 Q_("%lu hour ago", "%lu hours ago", diff), diff);
118 /* We deal with number of days from here on */
119 diff = (diff + 12) / 24;
122 Q_("%lu day ago", "%lu days ago", diff), diff);
125 /* Say weeks for the past 10 weeks or so */
128 Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
132 /* Say months for the past 12 months or so */
135 Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
139 /* Give years and months for 5 years or so */
141 unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
142 unsigned long years = totalmonths / 12;
143 unsigned long months = totalmonths % 12;
145 struct strbuf sb = STRBUF_INIT;
146 strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
147 /* TRANSLATORS: "%s" is "<n> years" */
149 Q_("%s, %lu month ago", "%s, %lu months ago", months),
154 Q_("%lu year ago", "%lu years ago", years), years);
157 /* Otherwise, just years. Centuries is probably overkill. */
159 Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
163 const char *show_date(unsigned long time, int tz, enum date_mode mode)
166 static struct strbuf timebuf = STRBUF_INIT;
168 if (mode == DATE_RAW) {
169 strbuf_reset(&timebuf);
170 strbuf_addf(&timebuf, "%lu %+05d", time, tz);
174 if (mode == DATE_RELATIVE) {
177 strbuf_reset(&timebuf);
178 gettimeofday(&now, NULL);
179 show_date_relative(time, tz, &now, &timebuf);
183 if (mode == DATE_LOCAL)
184 tz = local_tzoffset(time);
186 tm = time_to_tm(time, tz);
190 strbuf_reset(&timebuf);
191 if (mode == DATE_SHORT)
192 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
193 tm->tm_mon + 1, tm->tm_mday);
194 else if (mode == DATE_ISO8601)
195 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
199 tm->tm_hour, tm->tm_min, tm->tm_sec,
201 else if (mode == DATE_RFC2822)
202 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
203 weekday_names[tm->tm_wday], tm->tm_mday,
204 month_names[tm->tm_mon], tm->tm_year + 1900,
205 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
207 strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
208 weekday_names[tm->tm_wday],
209 month_names[tm->tm_mon],
211 tm->tm_hour, tm->tm_min, tm->tm_sec,
213 (mode == DATE_LOCAL) ? 0 : ' ',
219 * Check these. And note how it doesn't do the summer-time conversion.
221 * In my world, it's always summer, and things are probably a bit off
224 static const struct {
228 } timezone_names[] = {
229 { "IDLW", -12, 0, }, /* International Date Line West */
230 { "NT", -11, 0, }, /* Nome */
231 { "CAT", -10, 0, }, /* Central Alaska */
232 { "HST", -10, 0, }, /* Hawaii Standard */
233 { "HDT", -10, 1, }, /* Hawaii Daylight */
234 { "YST", -9, 0, }, /* Yukon Standard */
235 { "YDT", -9, 1, }, /* Yukon Daylight */
236 { "PST", -8, 0, }, /* Pacific Standard */
237 { "PDT", -8, 1, }, /* Pacific Daylight */
238 { "MST", -7, 0, }, /* Mountain Standard */
239 { "MDT", -7, 1, }, /* Mountain Daylight */
240 { "CST", -6, 0, }, /* Central Standard */
241 { "CDT", -6, 1, }, /* Central Daylight */
242 { "EST", -5, 0, }, /* Eastern Standard */
243 { "EDT", -5, 1, }, /* Eastern Daylight */
244 { "AST", -3, 0, }, /* Atlantic Standard */
245 { "ADT", -3, 1, }, /* Atlantic Daylight */
246 { "WAT", -1, 0, }, /* West Africa */
248 { "GMT", 0, 0, }, /* Greenwich Mean */
249 { "UTC", 0, 0, }, /* Universal (Coordinated) */
250 { "Z", 0, 0, }, /* Zulu, alias for UTC */
252 { "WET", 0, 0, }, /* Western European */
253 { "BST", 0, 1, }, /* British Summer */
254 { "CET", +1, 0, }, /* Central European */
255 { "MET", +1, 0, }, /* Middle European */
256 { "MEWT", +1, 0, }, /* Middle European Winter */
257 { "MEST", +1, 1, }, /* Middle European Summer */
258 { "CEST", +1, 1, }, /* Central European Summer */
259 { "MESZ", +1, 1, }, /* Middle European Summer */
260 { "FWT", +1, 0, }, /* French Winter */
261 { "FST", +1, 1, }, /* French Summer */
262 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
263 { "EEST", +2, 1, }, /* Eastern European Daylight */
264 { "WAST", +7, 0, }, /* West Australian Standard */
265 { "WADT", +7, 1, }, /* West Australian Daylight */
266 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
267 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
268 { "EAST", +10, 0, }, /* Eastern Australian Standard */
269 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
270 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
271 { "NZT", +12, 0, }, /* New Zealand */
272 { "NZST", +12, 0, }, /* New Zealand Standard */
273 { "NZDT", +12, 1, }, /* New Zealand Daylight */
274 { "IDLE", +12, 0, }, /* International Date Line East */
277 static int match_string(const char *date, const char *str)
281 for (i = 0; *date; date++, str++, i++) {
284 if (toupper(*date) == toupper(*str))
293 static int skip_alpha(const char *date)
298 } while (isalpha(date[i]));
303 * Parse month, weekday, or timezone name
305 static int match_alpha(const char *date, struct tm *tm, int *offset)
309 for (i = 0; i < 12; i++) {
310 int match = match_string(date, month_names[i]);
317 for (i = 0; i < 7; i++) {
318 int match = match_string(date, weekday_names[i]);
325 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
326 int match = match_string(date, timezone_names[i].name);
327 if (match >= 3 || match == strlen(timezone_names[i].name)) {
328 int off = timezone_names[i].offset;
330 /* This is bogus, but we like summer */
331 off += timezone_names[i].dst;
333 /* Only use the tz name offset if we don't have anything better */
341 if (match_string(date, "PM") == 2) {
342 tm->tm_hour = (tm->tm_hour % 12) + 12;
346 if (match_string(date, "AM") == 2) {
347 tm->tm_hour = (tm->tm_hour % 12) + 0;
352 return skip_alpha(date);
355 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
357 if (month > 0 && month < 13 && day > 0 && day < 32) {
358 struct tm check = *tm;
359 struct tm *r = (now_tm ? &check : tm);
362 r->tm_mon = month - 1;
367 r->tm_year = now_tm->tm_year;
369 else if (year >= 1970 && year < 2100)
370 r->tm_year = year - 1900;
371 else if (year > 70 && year < 100)
374 r->tm_year = year + 100;
380 specified = tm_to_time_t(r);
382 /* Be it commit time or author time, it does not make
383 * sense to specify timestamp way into the future. Make
384 * sure it is not later than ten days from now...
386 if (now + 10*24*3600 < specified)
388 tm->tm_mon = r->tm_mon;
389 tm->tm_mday = r->tm_mday;
391 tm->tm_year = r->tm_year;
397 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
401 struct tm *refuse_future;
404 num2 = strtol(end+1, &end, 10);
406 if (*end == c && isdigit(end[1]))
407 num3 = strtol(end+1, &end, 10);
414 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
426 refuse_future = NULL;
427 if (gmtime_r(&now, &now_tm))
428 refuse_future = &now_tm;
432 if (is_date(num, num2, num3, refuse_future, now, tm))
435 if (is_date(num, num3, num2, refuse_future, now, tm))
438 /* Our eastern European friends say dd.mm.yy[yy]
439 * is the norm there, so giving precedence to
440 * mm/dd/yy[yy] form only when separator is not '.'
443 is_date(num3, num, num2, refuse_future, now, tm))
445 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
446 if (is_date(num3, num2, num, refuse_future, now, tm))
448 /* Funny European mm.dd.yy */
450 is_date(num3, num, num2, refuse_future, now, tm))
458 * Have we filled in any part of the time/date yet?
459 * We just do a binary 'and' to see if the sign bit
460 * is set in all the values.
462 static inline int nodate(struct tm *tm)
464 return (tm->tm_year &
473 * We've seen a digit. Time? Year? Date?
475 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
481 num = strtoul(date, &end, 10);
484 * Seconds since 1970? We trigger on that for any numbers with
485 * more than 8 digits. This is because we don't want to rule out
486 * numbers like 20070606 as a YYYYMMDD date.
488 if (num >= 100000000 && nodate(tm)) {
490 if (gmtime_r(&time, tm)) {
497 * Check for special formats: num[-.:/]num[same]num
504 if (isdigit(end[1])) {
505 int match = match_multi_number(num, *end, date, end, tm);
512 * None of the special formats? Try to guess what
513 * the number meant. We use the number of digits
514 * to make a more educated guess..
519 } while (isdigit(date[n]));
521 /* Four-digit year or a timezone? */
523 if (num <= 1400 && *offset == -1) {
524 unsigned int minutes = num % 100;
525 unsigned int hours = num / 100;
526 *offset = hours*60 + minutes;
527 } else if (num > 1900 && num < 2100)
528 tm->tm_year = num - 1900;
533 * Ignore lots of numerals. We took care of 4-digit years above.
534 * Days or months must be one or two digits.
540 * NOTE! We will give precedence to day-of-month over month or
541 * year numbers in the 1-12 range. So 05 is always "mday 5",
542 * unless we already have a mday..
544 * IOW, 01 Apr 05 parses as "April 1st, 2005".
546 if (num > 0 && num < 32 && tm->tm_mday < 0) {
551 /* Two-digit year? */
552 if (n == 2 && tm->tm_year < 0) {
553 if (num < 10 && tm->tm_mday >= 0) {
554 tm->tm_year = num + 100;
563 if (num > 0 && num < 13 && tm->tm_mon < 0)
569 static int match_tz(const char *date, int *offp)
572 int hour = strtoul(date + 1, &end, 10);
573 int n = end - (date + 1);
581 min = 99; /* random crap */
582 } else if (*end == ':') {
584 min = strtoul(end + 1, &end, 10);
585 if (end - (date + 1) != 5)
586 min = 99; /* random crap */
587 } /* otherwise we parsed "hh" */
590 * Don't accept any random crap. Even though some places have
591 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
592 * UTC+14), there is something wrong if hour part is much
593 * larger than that. We might also want to check that the
594 * minutes are divisible by 15 or something too. (Offset of
595 * Kathmandu, Nepal is UTC+5:45)
597 if (min < 60 && hour < 24) {
598 int offset = hour * 60 + min;
606 static int date_string(unsigned long date, int offset, char *buf, int len)
614 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
618 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
619 * only when it appears not as part of any other string.
621 static int match_object_header_date(const char *date, unsigned long *timestamp, int *offset)
627 if (*date < '0' || '9' <= *date)
629 stamp = strtoul(date, &end, 10);
630 if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-'))
633 ofs = strtol(date, &end, 10);
634 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
636 ofs = (ofs / 100) * 60 + (ofs % 100);
644 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
645 (i.e. English) day/month names, and it doesn't work correctly with %z. */
646 int parse_date_basic(const char *date, unsigned long *timestamp, int *offset)
650 unsigned long dummy_timestamp;
654 timestamp = &dummy_timestamp;
656 offset = &dummy_offset;
658 memset(&tm, 0, sizeof(tm));
670 !match_object_header_date(date + 1, timestamp, offset))
671 return 0; /* success */
674 unsigned char c = *date;
676 /* Stop at end of string or newline */
681 match = match_alpha(date, &tm, offset);
683 match = match_digit(date, &tm, offset, &tm_gmt);
684 else if ((c == '-' || c == '+') && isdigit(date[1]))
685 match = match_tz(date, offset);
695 /* mktime uses local timezone */
696 *timestamp = tm_to_time_t(&tm);
698 *offset = ((time_t)*timestamp - mktime(&tm)) / 60;
700 if (*timestamp == -1)
704 *timestamp -= *offset * 60;
705 return 0; /* success */
708 int parse_date(const char *date, char *result, int maxlen)
710 unsigned long timestamp;
712 if (parse_date_basic(date, ×tamp, &offset))
714 return date_string(timestamp, offset, result, maxlen);
717 enum date_mode parse_date_format(const char *format)
719 if (!strcmp(format, "relative"))
720 return DATE_RELATIVE;
721 else if (!strcmp(format, "iso8601") ||
722 !strcmp(format, "iso"))
724 else if (!strcmp(format, "rfc2822") ||
725 !strcmp(format, "rfc"))
727 else if (!strcmp(format, "short"))
729 else if (!strcmp(format, "local"))
731 else if (!strcmp(format, "default"))
733 else if (!strcmp(format, "raw"))
736 die("unknown date format %s", format);
739 void datestamp(char *buf, int bufsize)
746 offset = tm_to_time_t(localtime(&now)) - now;
749 date_string(now, offset, buf, bufsize);
753 * Relative time update (eg "2 days ago"). If we haven't set the time
754 * yet, we need to set it from current time.
756 static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
761 tm->tm_mday = now->tm_mday;
763 tm->tm_mon = now->tm_mon;
764 if (tm->tm_year < 0) {
765 tm->tm_year = now->tm_year;
766 if (tm->tm_mon > now->tm_mon)
770 n = mktime(tm) - sec;
775 static void date_now(struct tm *tm, struct tm *now, int *num)
777 update_tm(tm, now, 0);
780 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
782 update_tm(tm, now, 24*60*60);
785 static void date_time(struct tm *tm, struct tm *now, int hour)
787 if (tm->tm_hour < hour)
788 date_yesterday(tm, now, NULL);
794 static void date_midnight(struct tm *tm, struct tm *now, int *num)
796 date_time(tm, now, 0);
799 static void date_noon(struct tm *tm, struct tm *now, int *num)
801 date_time(tm, now, 12);
804 static void date_tea(struct tm *tm, struct tm *now, int *num)
806 date_time(tm, now, 17);
809 static void date_pm(struct tm *tm, struct tm *now, int *num)
820 tm->tm_hour = (hour % 12) + 12;
823 static void date_am(struct tm *tm, struct tm *now, int *num)
834 tm->tm_hour = (hour % 12);
837 static void date_never(struct tm *tm, struct tm *now, int *num)
843 static const struct special {
845 void (*fn)(struct tm *, struct tm *, int *);
847 { "yesterday", date_yesterday },
848 { "noon", date_noon },
849 { "midnight", date_midnight },
853 { "never", date_never },
858 static const char *number_name[] = {
859 "zero", "one", "two", "three", "four",
860 "five", "six", "seven", "eight", "nine", "ten",
863 static const struct typelen {
870 { "days", 24*60*60 },
871 { "weeks", 7*24*60*60 },
875 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
877 const struct typelen *tl;
878 const struct special *s;
879 const char *end = date;
882 while (isalpha(*++end));
885 for (i = 0; i < 12; i++) {
886 int match = match_string(date, month_names[i]);
894 for (s = special; s->name; s++) {
895 int len = strlen(s->name);
896 if (match_string(date, s->name) == len) {
904 for (i = 1; i < 11; i++) {
905 int len = strlen(number_name[i]);
906 if (match_string(date, number_name[i]) == len) {
912 if (match_string(date, "last") == 4) {
921 int len = strlen(tl->type);
922 if (match_string(date, tl->type) >= len-1) {
923 update_tm(tm, now, tl->length * *num);
931 for (i = 0; i < 7; i++) {
932 int match = match_string(date, weekday_names[i]);
934 int diff, n = *num -1;
937 diff = tm->tm_wday - i;
942 update_tm(tm, now, diff * 24 * 60 * 60);
948 if (match_string(date, "months") >= 5) {
950 update_tm(tm, now, 0); /* fill in date fields if needed */
951 n = tm->tm_mon - *num;
962 if (match_string(date, "years") >= 4) {
963 update_tm(tm, now, 0); /* fill in date fields if needed */
973 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
976 unsigned long number = strtoul(date, &end, 10);
983 if (isdigit(end[1])) {
984 int match = match_multi_number(number, *end, date, end, tm);
990 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
991 if (date[0] != '0' || end - date <= 2)
997 * Do we have a pending number at the end, or when
998 * we see a new one? Let's assume it's a month day,
999 * as in "Dec 6, 1992"
1001 static void pending_number(struct tm *tm, int *num)
1007 if (tm->tm_mday < 0 && number < 32)
1008 tm->tm_mday = number;
1009 else if (tm->tm_mon < 0 && number < 13)
1010 tm->tm_mon = number-1;
1011 else if (tm->tm_year < 0) {
1012 if (number > 1969 && number < 2100)
1013 tm->tm_year = number - 1900;
1014 else if (number > 69 && number < 100)
1015 tm->tm_year = number;
1016 else if (number < 38)
1017 tm->tm_year = 100 + number;
1018 /* We screw up for number = 00 ? */
1023 static unsigned long approxidate_str(const char *date,
1024 const struct timeval *tv,
1032 time_sec = tv->tv_sec;
1033 localtime_r(&time_sec, &tm);
1041 unsigned char c = *date;
1046 pending_number(&tm, &number);
1047 date = approxidate_digit(date-1, &tm, &number);
1052 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
1054 pending_number(&tm, &number);
1057 return update_tm(&tm, &now, 0);
1060 unsigned long approxidate_relative(const char *date, const struct timeval *tv)
1062 unsigned long timestamp;
1066 if (!parse_date_basic(date, ×tamp, &offset))
1068 return approxidate_str(date, tv, &errors);
1071 unsigned long approxidate_careful(const char *date, int *error_ret)
1074 unsigned long timestamp;
1080 if (!parse_date_basic(date, ×tamp, &offset)) {
1085 gettimeofday(&tv, NULL);
1086 return approxidate_str(date, &tv, error_ret);