rebase: add coverage of other incompatible options
[alt-git.git] / date.c
blob6f45eeb3568671e5b4bb37c2830315a490a12cfe
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
8 #include "date.h"
11 * This is like mktime, but without normalization of tm_wday and tm_yday.
13 time_t tm_to_time_t(const struct tm *tm)
15 static const int mdays[] = {
16 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
18 int year = tm->tm_year - 70;
19 int month = tm->tm_mon;
20 int day = tm->tm_mday;
22 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
23 return -1;
24 if (month < 0 || month > 11) /* array bounds */
25 return -1;
26 if (month < 2 || (year + 2) % 4)
27 day--;
28 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
29 return -1;
30 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
31 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
34 static const char *month_names[] = {
35 "January", "February", "March", "April", "May", "June",
36 "July", "August", "September", "October", "November", "December"
39 static const char *weekday_names[] = {
40 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
43 static time_t gm_time_t(timestamp_t time, int tz)
45 int minutes;
47 minutes = tz < 0 ? -tz : tz;
48 minutes = (minutes / 100)*60 + (minutes % 100);
49 minutes = tz < 0 ? -minutes : minutes;
51 if (minutes > 0) {
52 if (unsigned_add_overflows(time, minutes * 60))
53 die("Timestamp+tz too large: %"PRItime" +%04d",
54 time, tz);
55 } else if (time < -minutes * 60)
56 die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);
57 time += minutes * 60;
58 if (date_overflows(time))
59 die("Timestamp too large for this system: %"PRItime, time);
60 return (time_t)time;
64 * The "tz" thing is passed in as this strange "decimal parse of tz"
65 * thing, which means that tz -0100 is passed in as the integer -100,
66 * even though it means "sixty minutes off"
68 static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm)
70 time_t t = gm_time_t(time, tz);
71 return gmtime_r(&t, tm);
74 static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)
76 time_t t = time;
77 return localtime_r(&t, tm);
81 * Fill in the localtime 'struct tm' for the supplied time,
82 * and return the local tz.
84 static int local_time_tzoffset(time_t t, struct tm *tm)
86 time_t t_local;
87 int offset, eastwest;
89 localtime_r(&t, tm);
90 t_local = tm_to_time_t(tm);
91 if (t_local == -1)
92 return 0; /* error; just use +0000 */
93 if (t_local < t) {
94 eastwest = -1;
95 offset = t - t_local;
96 } else {
97 eastwest = 1;
98 offset = t_local - t;
100 offset /= 60; /* in minutes */
101 offset = (offset % 60) + ((offset / 60) * 100);
102 return offset * eastwest;
106 * What value of "tz" was in effect back then at "time" in the
107 * local timezone?
109 static int local_tzoffset(timestamp_t time)
111 struct tm tm;
113 if (date_overflows(time))
114 die("Timestamp too large for this system: %"PRItime, time);
116 return local_time_tzoffset((time_t)time, &tm);
119 static void get_time(struct timeval *now)
121 const char *x;
123 x = getenv("GIT_TEST_DATE_NOW");
124 if (x) {
125 now->tv_sec = atoi(x);
126 now->tv_usec = 0;
128 else
129 gettimeofday(now, NULL);
132 void show_date_relative(timestamp_t time, struct strbuf *timebuf)
134 struct timeval now;
135 timestamp_t diff;
137 get_time(&now);
138 if (now.tv_sec < time) {
139 strbuf_addstr(timebuf, _("in the future"));
140 return;
142 diff = now.tv_sec - time;
143 if (diff < 90) {
144 strbuf_addf(timebuf,
145 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
146 return;
148 /* Turn it into minutes */
149 diff = (diff + 30) / 60;
150 if (diff < 90) {
151 strbuf_addf(timebuf,
152 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
153 return;
155 /* Turn it into hours */
156 diff = (diff + 30) / 60;
157 if (diff < 36) {
158 strbuf_addf(timebuf,
159 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
160 return;
162 /* We deal with number of days from here on */
163 diff = (diff + 12) / 24;
164 if (diff < 14) {
165 strbuf_addf(timebuf,
166 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
167 return;
169 /* Say weeks for the past 10 weeks or so */
170 if (diff < 70) {
171 strbuf_addf(timebuf,
172 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
173 (diff + 3) / 7);
174 return;
176 /* Say months for the past 12 months or so */
177 if (diff < 365) {
178 strbuf_addf(timebuf,
179 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
180 (diff + 15) / 30);
181 return;
183 /* Give years and months for 5 years or so */
184 if (diff < 1825) {
185 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
186 timestamp_t years = totalmonths / 12;
187 timestamp_t months = totalmonths % 12;
188 if (months) {
189 struct strbuf sb = STRBUF_INIT;
190 strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
191 strbuf_addf(timebuf,
192 /* TRANSLATORS: "%s" is "<n> years" */
193 Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
194 sb.buf, months);
195 strbuf_release(&sb);
196 } else
197 strbuf_addf(timebuf,
198 Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
199 return;
201 /* Otherwise, just years. Centuries is probably overkill. */
202 strbuf_addf(timebuf,
203 Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
204 (diff + 183) / 365);
207 struct date_mode *date_mode_from_type(enum date_mode_type type)
209 static struct date_mode mode = DATE_MODE_INIT;
210 if (type == DATE_STRFTIME)
211 BUG("cannot create anonymous strftime date_mode struct");
212 mode.type = type;
213 return &mode;
216 static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local)
218 struct {
219 unsigned int year:1,
220 date:1,
221 wday:1,
222 time:1,
223 seconds:1,
224 tz:1;
225 } hide = { 0 };
227 hide.tz = local || tz == human_tz;
228 hide.year = tm->tm_year == human_tm->tm_year;
229 if (hide.year) {
230 if (tm->tm_mon == human_tm->tm_mon) {
231 if (tm->tm_mday > human_tm->tm_mday) {
232 /* Future date: think timezones */
233 } else if (tm->tm_mday == human_tm->tm_mday) {
234 hide.date = hide.wday = 1;
235 } else if (tm->tm_mday + 5 > human_tm->tm_mday) {
236 /* Leave just weekday if it was a few days ago */
237 hide.date = 1;
242 /* Show "today" times as just relative times */
243 if (hide.wday) {
244 show_date_relative(time, buf);
245 return;
249 * Always hide seconds for human-readable.
250 * Hide timezone if showing date.
251 * Hide weekday and time if showing year.
253 * The logic here is two-fold:
254 * (a) only show details when recent enough to matter
255 * (b) keep the maximum length "similar", and in check
257 if (human_tm->tm_year) {
258 hide.seconds = 1;
259 hide.tz |= !hide.date;
260 hide.wday = hide.time = !hide.year;
263 if (!hide.wday)
264 strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]);
265 if (!hide.date)
266 strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday);
268 /* Do we want AM/PM depending on locale? */
269 if (!hide.time) {
270 strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min);
271 if (!hide.seconds)
272 strbuf_addf(buf, ":%02d", tm->tm_sec);
273 } else
274 strbuf_rtrim(buf);
276 if (!hide.year)
277 strbuf_addf(buf, " %d", tm->tm_year + 1900);
279 if (!hide.tz)
280 strbuf_addf(buf, " %+05d", tz);
283 const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
285 struct tm *tm;
286 struct tm tmbuf = { 0 };
287 struct tm human_tm = { 0 };
288 int human_tz = -1;
289 static struct strbuf timebuf = STRBUF_INIT;
291 if (mode->type == DATE_UNIX) {
292 strbuf_reset(&timebuf);
293 strbuf_addf(&timebuf, "%"PRItime, time);
294 return timebuf.buf;
297 if (mode->type == DATE_HUMAN) {
298 struct timeval now;
300 get_time(&now);
302 /* Fill in the data for "current time" in human_tz and human_tm */
303 human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
306 if (mode->local)
307 tz = local_tzoffset(time);
309 if (mode->type == DATE_RAW) {
310 strbuf_reset(&timebuf);
311 strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
312 return timebuf.buf;
315 if (mode->type == DATE_RELATIVE) {
316 strbuf_reset(&timebuf);
317 show_date_relative(time, &timebuf);
318 return timebuf.buf;
321 if (mode->local)
322 tm = time_to_tm_local(time, &tmbuf);
323 else
324 tm = time_to_tm(time, tz, &tmbuf);
325 if (!tm) {
326 tm = time_to_tm(0, 0, &tmbuf);
327 tz = 0;
330 strbuf_reset(&timebuf);
331 if (mode->type == DATE_SHORT)
332 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
333 tm->tm_mon + 1, tm->tm_mday);
334 else if (mode->type == DATE_ISO8601)
335 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
336 tm->tm_year + 1900,
337 tm->tm_mon + 1,
338 tm->tm_mday,
339 tm->tm_hour, tm->tm_min, tm->tm_sec,
340 tz);
341 else if (mode->type == DATE_ISO8601_STRICT) {
342 char sign = (tz >= 0) ? '+' : '-';
343 tz = abs(tz);
344 strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
345 tm->tm_year + 1900,
346 tm->tm_mon + 1,
347 tm->tm_mday,
348 tm->tm_hour, tm->tm_min, tm->tm_sec,
349 sign, tz / 100, tz % 100);
350 } else if (mode->type == DATE_RFC2822)
351 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
352 weekday_names[tm->tm_wday], tm->tm_mday,
353 month_names[tm->tm_mon], tm->tm_year + 1900,
354 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
355 else if (mode->type == DATE_STRFTIME)
356 strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
357 !mode->local);
358 else
359 show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local);
360 return timebuf.buf;
364 * Check these. And note how it doesn't do the summer-time conversion.
366 * In my world, it's always summer, and things are probably a bit off
367 * in other ways too.
369 static const struct {
370 const char *name;
371 int offset;
372 int dst;
373 } timezone_names[] = {
374 { "IDLW", -12, 0, }, /* International Date Line West */
375 { "NT", -11, 0, }, /* Nome */
376 { "CAT", -10, 0, }, /* Central Alaska */
377 { "HST", -10, 0, }, /* Hawaii Standard */
378 { "HDT", -10, 1, }, /* Hawaii Daylight */
379 { "YST", -9, 0, }, /* Yukon Standard */
380 { "YDT", -9, 1, }, /* Yukon Daylight */
381 { "PST", -8, 0, }, /* Pacific Standard */
382 { "PDT", -8, 1, }, /* Pacific Daylight */
383 { "MST", -7, 0, }, /* Mountain Standard */
384 { "MDT", -7, 1, }, /* Mountain Daylight */
385 { "CST", -6, 0, }, /* Central Standard */
386 { "CDT", -6, 1, }, /* Central Daylight */
387 { "EST", -5, 0, }, /* Eastern Standard */
388 { "EDT", -5, 1, }, /* Eastern Daylight */
389 { "AST", -3, 0, }, /* Atlantic Standard */
390 { "ADT", -3, 1, }, /* Atlantic Daylight */
391 { "WAT", -1, 0, }, /* West Africa */
393 { "GMT", 0, 0, }, /* Greenwich Mean */
394 { "UTC", 0, 0, }, /* Universal (Coordinated) */
395 { "Z", 0, 0, }, /* Zulu, alias for UTC */
397 { "WET", 0, 0, }, /* Western European */
398 { "BST", 0, 1, }, /* British Summer */
399 { "CET", +1, 0, }, /* Central European */
400 { "MET", +1, 0, }, /* Middle European */
401 { "MEWT", +1, 0, }, /* Middle European Winter */
402 { "MEST", +1, 1, }, /* Middle European Summer */
403 { "CEST", +1, 1, }, /* Central European Summer */
404 { "MESZ", +1, 1, }, /* Middle European Summer */
405 { "FWT", +1, 0, }, /* French Winter */
406 { "FST", +1, 1, }, /* French Summer */
407 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
408 { "EEST", +2, 1, }, /* Eastern European Daylight */
409 { "WAST", +7, 0, }, /* West Australian Standard */
410 { "WADT", +7, 1, }, /* West Australian Daylight */
411 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
412 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
413 { "EAST", +10, 0, }, /* Eastern Australian Standard */
414 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
415 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
416 { "NZT", +12, 0, }, /* New Zealand */
417 { "NZST", +12, 0, }, /* New Zealand Standard */
418 { "NZDT", +12, 1, }, /* New Zealand Daylight */
419 { "IDLE", +12, 0, }, /* International Date Line East */
422 static int match_string(const char *date, const char *str)
424 int i = 0;
426 for (i = 0; *date; date++, str++, i++) {
427 if (*date == *str)
428 continue;
429 if (toupper(*date) == toupper(*str))
430 continue;
431 if (!isalnum(*date))
432 break;
433 return 0;
435 return i;
438 static int skip_alpha(const char *date)
440 int i = 0;
441 do {
442 i++;
443 } while (isalpha(date[i]));
444 return i;
448 * Parse month, weekday, or timezone name
450 static int match_alpha(const char *date, struct tm *tm, int *offset)
452 int i;
454 for (i = 0; i < 12; i++) {
455 int match = match_string(date, month_names[i]);
456 if (match >= 3) {
457 tm->tm_mon = i;
458 return match;
462 for (i = 0; i < 7; i++) {
463 int match = match_string(date, weekday_names[i]);
464 if (match >= 3) {
465 tm->tm_wday = i;
466 return match;
470 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
471 int match = match_string(date, timezone_names[i].name);
472 if (match >= 3 || match == strlen(timezone_names[i].name)) {
473 int off = timezone_names[i].offset;
475 /* This is bogus, but we like summer */
476 off += timezone_names[i].dst;
478 /* Only use the tz name offset if we don't have anything better */
479 if (*offset == -1)
480 *offset = 60*off;
482 return match;
486 if (match_string(date, "PM") == 2) {
487 tm->tm_hour = (tm->tm_hour % 12) + 12;
488 return 2;
491 if (match_string(date, "AM") == 2) {
492 tm->tm_hour = (tm->tm_hour % 12) + 0;
493 return 2;
496 /* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */
497 if (*date == 'T' && isdigit(date[1]) && tm->tm_hour == -1) {
498 tm->tm_min = tm->tm_sec = 0;
499 return 1;
502 /* BAD CRAP */
503 return skip_alpha(date);
506 static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
508 if (month > 0 && month < 13 && day > 0 && day < 32) {
509 struct tm check = *tm;
510 struct tm *r = (now_tm ? &check : tm);
511 time_t specified;
513 r->tm_mon = month - 1;
514 r->tm_mday = day;
515 if (year == -1) {
516 if (!now_tm)
517 return 1;
518 r->tm_year = now_tm->tm_year;
520 else if (year >= 1970 && year < 2100)
521 r->tm_year = year - 1900;
522 else if (year > 70 && year < 100)
523 r->tm_year = year;
524 else if (year < 38)
525 r->tm_year = year + 100;
526 else
527 return -1;
528 if (!now_tm)
529 return 0;
531 specified = tm_to_time_t(r);
533 /* Be it commit time or author time, it does not make
534 * sense to specify timestamp way into the future. Make
535 * sure it is not later than ten days from now...
537 if ((specified != -1) && (now + 10*24*3600 < specified))
538 return -1;
539 tm->tm_mon = r->tm_mon;
540 tm->tm_mday = r->tm_mday;
541 if (year != -1)
542 tm->tm_year = r->tm_year;
543 return 0;
545 return -1;
548 static int set_time(long hour, long minute, long second, struct tm *tm)
550 /* We accept 61st second because of leap second */
551 if (0 <= hour && hour <= 24 &&
552 0 <= minute && minute < 60 &&
553 0 <= second && second <= 60) {
554 tm->tm_hour = hour;
555 tm->tm_min = minute;
556 tm->tm_sec = second;
557 return 0;
559 return -1;
562 static int is_date_known(struct tm *tm)
564 return tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1;
567 static int match_multi_number(timestamp_t num, char c, const char *date,
568 char *end, struct tm *tm, time_t now)
570 struct tm now_tm;
571 struct tm *refuse_future;
572 long num2, num3;
574 num2 = strtol(end+1, &end, 10);
575 num3 = -1;
576 if (*end == c && isdigit(end[1]))
577 num3 = strtol(end+1, &end, 10);
579 /* Time? Date? */
580 switch (c) {
581 case ':':
582 if (num3 < 0)
583 num3 = 0;
584 if (set_time(num, num2, num3, tm) == 0) {
586 * If %H:%M:%S was just parsed followed by: .<num4>
587 * Consider (& discard) it as fractional second
588 * if %Y%m%d is parsed before.
590 if (*end == '.' && isdigit(end[1]) && is_date_known(tm))
591 strtol(end + 1, &end, 10);
592 break;
594 return 0;
596 case '-':
597 case '/':
598 case '.':
599 if (!now)
600 now = time(NULL);
601 refuse_future = NULL;
602 if (gmtime_r(&now, &now_tm))
603 refuse_future = &now_tm;
605 if (num > 70) {
606 /* yyyy-mm-dd? */
607 if (set_date(num, num2, num3, NULL, now, tm) == 0)
608 break;
609 /* yyyy-dd-mm? */
610 if (set_date(num, num3, num2, NULL, now, tm) == 0)
611 break;
613 /* Our eastern European friends say dd.mm.yy[yy]
614 * is the norm there, so giving precedence to
615 * mm/dd/yy[yy] form only when separator is not '.'
617 if (c != '.' &&
618 set_date(num3, num, num2, refuse_future, now, tm) == 0)
619 break;
620 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
621 if (set_date(num3, num2, num, refuse_future, now, tm) == 0)
622 break;
623 /* Funny European mm.dd.yy */
624 if (c == '.' &&
625 set_date(num3, num, num2, refuse_future, now, tm) == 0)
626 break;
627 return 0;
629 return end - date;
633 * Have we filled in any part of the time/date yet?
634 * We just do a binary 'and' to see if the sign bit
635 * is set in all the values.
637 static inline int nodate(struct tm *tm)
639 return (tm->tm_year &
640 tm->tm_mon &
641 tm->tm_mday &
642 tm->tm_hour &
643 tm->tm_min &
644 tm->tm_sec) < 0;
648 * Have we seen an ISO-8601-alike date, i.e. 20220101T0,
649 * In which, hour is still unset,
650 * and minutes and second has been set to 0.
652 static inline int maybeiso8601(struct tm *tm)
654 return tm->tm_hour == -1 &&
655 tm->tm_min == 0 &&
656 tm->tm_sec == 0;
660 * We've seen a digit. Time? Year? Date?
662 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
664 int n;
665 char *end;
666 timestamp_t num;
668 num = parse_timestamp(date, &end, 10);
671 * Seconds since 1970? We trigger on that for any numbers with
672 * more than 8 digits. This is because we don't want to rule out
673 * numbers like 20070606 as a YYYYMMDD date.
675 if (num >= 100000000 && nodate(tm)) {
676 time_t time = num;
677 if (gmtime_r(&time, tm)) {
678 *tm_gmt = 1;
679 return end - date;
684 * Check for special formats: num[-.:/]num[same]num
686 switch (*end) {
687 case ':':
688 case '.':
689 case '/':
690 case '-':
691 if (isdigit(end[1])) {
692 int match = match_multi_number(num, *end, date, end, tm, 0);
693 if (match)
694 return match;
699 * None of the special formats? Try to guess what
700 * the number meant. We use the number of digits
701 * to make a more educated guess..
703 n = 0;
704 do {
705 n++;
706 } while (isdigit(date[n]));
708 /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
709 /* 6 digits, compact style of ISO-8601's time: HHMMSS */
710 if (n == 8 || n == 6) {
711 unsigned int num1 = num / 10000;
712 unsigned int num2 = (num % 10000) / 100;
713 unsigned int num3 = num % 100;
714 if (n == 8)
715 set_date(num1, num2, num3, NULL, time(NULL), tm);
716 else if (n == 6 && set_time(num1, num2, num3, tm) == 0 &&
717 *end == '.' && isdigit(end[1]))
718 strtoul(end + 1, &end, 10);
719 return end - date;
722 /* reduced precision of ISO-8601's time: HHMM or HH */
723 if (maybeiso8601(tm)) {
724 unsigned int num1 = num;
725 unsigned int num2 = 0;
726 if (n == 4) {
727 num1 = num / 100;
728 num2 = num % 100;
730 if ((n == 4 || n == 2) && !nodate(tm) &&
731 set_time(num1, num2, 0, tm) == 0)
732 return n;
734 * We thought this is an ISO-8601 time string,
735 * we set minutes and seconds to 0,
736 * turn out it isn't, rollback the change.
738 tm->tm_min = tm->tm_sec = -1;
741 /* Four-digit year or a timezone? */
742 if (n == 4) {
743 if (num <= 1400 && *offset == -1) {
744 unsigned int minutes = num % 100;
745 unsigned int hours = num / 100;
746 *offset = hours*60 + minutes;
747 } else if (num > 1900 && num < 2100)
748 tm->tm_year = num - 1900;
749 return n;
753 * Ignore lots of numerals. We took care of 4-digit years above.
754 * Days or months must be one or two digits.
756 if (n > 2)
757 return n;
760 * NOTE! We will give precedence to day-of-month over month or
761 * year numbers in the 1-12 range. So 05 is always "mday 5",
762 * unless we already have a mday..
764 * IOW, 01 Apr 05 parses as "April 1st, 2005".
766 if (num > 0 && num < 32 && tm->tm_mday < 0) {
767 tm->tm_mday = num;
768 return n;
771 /* Two-digit year? */
772 if (n == 2 && tm->tm_year < 0) {
773 if (num < 10 && tm->tm_mday >= 0) {
774 tm->tm_year = num + 100;
775 return n;
777 if (num >= 70) {
778 tm->tm_year = num;
779 return n;
783 if (num > 0 && num < 13 && tm->tm_mon < 0)
784 tm->tm_mon = num-1;
786 return n;
789 static int match_tz(const char *date, int *offp)
791 char *end;
792 int hour = strtoul(date + 1, &end, 10);
793 int n = end - (date + 1);
794 int min = 0;
796 if (n == 4) {
797 /* hhmm */
798 min = hour % 100;
799 hour = hour / 100;
800 } else if (n != 2) {
801 min = 99; /* random crap */
802 } else if (*end == ':') {
803 /* hh:mm? */
804 min = strtoul(end + 1, &end, 10);
805 if (end - (date + 1) != 5)
806 min = 99; /* random crap */
807 } /* otherwise we parsed "hh" */
810 * Don't accept any random crap. Even though some places have
811 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
812 * UTC+14), there is something wrong if hour part is much
813 * larger than that. We might also want to check that the
814 * minutes are divisible by 15 or something too. (Offset of
815 * Kathmandu, Nepal is UTC+5:45)
817 if (min < 60 && hour < 24) {
818 int offset = hour * 60 + min;
819 if (*date == '-')
820 offset = -offset;
821 *offp = offset;
823 return end - date;
826 static void date_string(timestamp_t date, int offset, struct strbuf *buf)
828 int sign = '+';
830 if (offset < 0) {
831 offset = -offset;
832 sign = '-';
834 strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
838 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
839 * only when it appears not as part of any other string.
841 static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
843 char *end;
844 timestamp_t stamp;
845 int ofs;
847 if (*date < '0' || '9' < *date)
848 return -1;
849 stamp = parse_timestamp(date, &end, 10);
850 if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
851 return -1;
852 date = end + 2;
853 ofs = strtol(date, &end, 10);
854 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
855 return -1;
856 ofs = (ofs / 100) * 60 + (ofs % 100);
857 if (date[-1] == '-')
858 ofs = -ofs;
859 *timestamp = stamp;
860 *offset = ofs;
861 return 0;
864 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
865 (i.e. English) day/month names, and it doesn't work correctly with %z. */
866 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
868 struct tm tm;
869 int tm_gmt;
870 timestamp_t dummy_timestamp;
871 int dummy_offset;
873 if (!timestamp)
874 timestamp = &dummy_timestamp;
875 if (!offset)
876 offset = &dummy_offset;
878 memset(&tm, 0, sizeof(tm));
879 tm.tm_year = -1;
880 tm.tm_mon = -1;
881 tm.tm_mday = -1;
882 tm.tm_isdst = -1;
883 tm.tm_hour = -1;
884 tm.tm_min = -1;
885 tm.tm_sec = -1;
886 *offset = -1;
887 tm_gmt = 0;
889 if (*date == '@' &&
890 !match_object_header_date(date + 1, timestamp, offset))
891 return 0; /* success */
892 for (;;) {
893 int match = 0;
894 unsigned char c = *date;
896 /* Stop at end of string or newline */
897 if (!c || c == '\n')
898 break;
900 if (isalpha(c))
901 match = match_alpha(date, &tm, offset);
902 else if (isdigit(c))
903 match = match_digit(date, &tm, offset, &tm_gmt);
904 else if ((c == '-' || c == '+') && isdigit(date[1]))
905 match = match_tz(date, offset);
907 if (!match) {
908 /* BAD CRAP */
909 match = 1;
912 date += match;
915 /* do not use mktime(), which uses local timezone, here */
916 *timestamp = tm_to_time_t(&tm);
917 if (*timestamp == -1)
918 return -1;
920 if (*offset == -1) {
921 time_t temp_time;
923 /* gmtime_r() in match_digit() may have clobbered it */
924 tm.tm_isdst = -1;
925 temp_time = mktime(&tm);
926 if ((time_t)*timestamp > temp_time) {
927 *offset = ((time_t)*timestamp - temp_time) / 60;
928 } else {
929 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
933 if (!tm_gmt)
934 *timestamp -= *offset * 60;
935 return 0; /* success */
938 int parse_expiry_date(const char *date, timestamp_t *timestamp)
940 int errors = 0;
942 if (!strcmp(date, "never") || !strcmp(date, "false"))
943 *timestamp = 0;
944 else if (!strcmp(date, "all") || !strcmp(date, "now"))
946 * We take over "now" here, which usually translates
947 * to the current timestamp. This is because the user
948 * really means to expire everything that was done in
949 * the past, and by definition reflogs are the record
950 * of the past, and there is nothing from the future
951 * to be kept.
953 *timestamp = TIME_MAX;
954 else
955 *timestamp = approxidate_careful(date, &errors);
957 return errors;
960 int parse_date(const char *date, struct strbuf *result)
962 timestamp_t timestamp;
963 int offset;
964 if (parse_date_basic(date, &timestamp, &offset))
965 return -1;
966 date_string(timestamp, offset, result);
967 return 0;
970 static enum date_mode_type parse_date_type(const char *format, const char **end)
972 if (skip_prefix(format, "relative", end))
973 return DATE_RELATIVE;
974 if (skip_prefix(format, "iso8601-strict", end) ||
975 skip_prefix(format, "iso-strict", end))
976 return DATE_ISO8601_STRICT;
977 if (skip_prefix(format, "iso8601", end) ||
978 skip_prefix(format, "iso", end))
979 return DATE_ISO8601;
980 if (skip_prefix(format, "rfc2822", end) ||
981 skip_prefix(format, "rfc", end))
982 return DATE_RFC2822;
983 if (skip_prefix(format, "short", end))
984 return DATE_SHORT;
985 if (skip_prefix(format, "default", end))
986 return DATE_NORMAL;
987 if (skip_prefix(format, "human", end))
988 return DATE_HUMAN;
989 if (skip_prefix(format, "raw", end))
990 return DATE_RAW;
991 if (skip_prefix(format, "unix", end))
992 return DATE_UNIX;
993 if (skip_prefix(format, "format", end))
994 return DATE_STRFTIME;
996 * Please update $__git_log_date_formats in
997 * git-completion.bash when you add new formats.
1000 die("unknown date format %s", format);
1003 void parse_date_format(const char *format, struct date_mode *mode)
1005 const char *p;
1007 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
1008 if (skip_prefix(format, "auto:", &p)) {
1009 if (isatty(1) || pager_in_use())
1010 format = p;
1011 else
1012 format = "default";
1015 /* historical alias */
1016 if (!strcmp(format, "local"))
1017 format = "default-local";
1019 mode->type = parse_date_type(format, &p);
1020 mode->local = 0;
1022 if (skip_prefix(p, "-local", &p))
1023 mode->local = 1;
1025 if (mode->type == DATE_STRFTIME) {
1026 if (!skip_prefix(p, ":", &p))
1027 die("date format missing colon separator: %s", format);
1028 mode->strftime_fmt = xstrdup(p);
1029 } else if (*p)
1030 die("unknown date format %s", format);
1033 void date_mode_release(struct date_mode *mode)
1035 free((char *)mode->strftime_fmt);
1038 void datestamp(struct strbuf *out)
1040 time_t now;
1041 int offset;
1042 struct tm tm = { 0 };
1044 time(&now);
1046 offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
1047 offset /= 60;
1049 date_string(now, offset, out);
1053 * Relative time update (eg "2 days ago"). If we haven't set the time
1054 * yet, we need to set it from current time.
1056 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
1058 time_t n;
1060 if (tm->tm_mday < 0)
1061 tm->tm_mday = now->tm_mday;
1062 if (tm->tm_mon < 0)
1063 tm->tm_mon = now->tm_mon;
1064 if (tm->tm_year < 0) {
1065 tm->tm_year = now->tm_year;
1066 if (tm->tm_mon > now->tm_mon)
1067 tm->tm_year--;
1070 n = mktime(tm) - sec;
1071 localtime_r(&n, tm);
1072 return n;
1076 * Do we have a pending number at the end, or when
1077 * we see a new one? Let's assume it's a month day,
1078 * as in "Dec 6, 1992"
1080 static void pending_number(struct tm *tm, int *num)
1082 int number = *num;
1084 if (number) {
1085 *num = 0;
1086 if (tm->tm_mday < 0 && number < 32)
1087 tm->tm_mday = number;
1088 else if (tm->tm_mon < 0 && number < 13)
1089 tm->tm_mon = number-1;
1090 else if (tm->tm_year < 0) {
1091 if (number > 1969 && number < 2100)
1092 tm->tm_year = number - 1900;
1093 else if (number > 69 && number < 100)
1094 tm->tm_year = number;
1095 else if (number < 38)
1096 tm->tm_year = 100 + number;
1097 /* We screw up for number = 00 ? */
1102 static void date_now(struct tm *tm, struct tm *now, int *num)
1104 *num = 0;
1105 update_tm(tm, now, 0);
1108 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
1110 *num = 0;
1111 update_tm(tm, now, 24*60*60);
1114 static void date_time(struct tm *tm, struct tm *now, int hour)
1116 if (tm->tm_hour < hour)
1117 update_tm(tm, now, 24*60*60);
1118 tm->tm_hour = hour;
1119 tm->tm_min = 0;
1120 tm->tm_sec = 0;
1123 static void date_midnight(struct tm *tm, struct tm *now, int *num)
1125 pending_number(tm, num);
1126 date_time(tm, now, 0);
1129 static void date_noon(struct tm *tm, struct tm *now, int *num)
1131 pending_number(tm, num);
1132 date_time(tm, now, 12);
1135 static void date_tea(struct tm *tm, struct tm *now, int *num)
1137 pending_number(tm, num);
1138 date_time(tm, now, 17);
1141 static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
1143 int hour, n = *num;
1144 *num = 0;
1146 hour = tm->tm_hour;
1147 if (n) {
1148 hour = n;
1149 tm->tm_min = 0;
1150 tm->tm_sec = 0;
1152 tm->tm_hour = (hour % 12) + 12;
1155 static void date_am(struct tm *tm, struct tm *now UNUSED, int *num)
1157 int hour, n = *num;
1158 *num = 0;
1160 hour = tm->tm_hour;
1161 if (n) {
1162 hour = n;
1163 tm->tm_min = 0;
1164 tm->tm_sec = 0;
1166 tm->tm_hour = (hour % 12);
1169 static void date_never(struct tm *tm, struct tm *now UNUSED, int *num)
1171 time_t n = 0;
1172 localtime_r(&n, tm);
1173 *num = 0;
1176 static const struct special {
1177 const char *name;
1178 void (*fn)(struct tm *, struct tm *, int *);
1179 } special[] = {
1180 { "yesterday", date_yesterday },
1181 { "noon", date_noon },
1182 { "midnight", date_midnight },
1183 { "tea", date_tea },
1184 { "PM", date_pm },
1185 { "AM", date_am },
1186 { "never", date_never },
1187 { "now", date_now },
1188 { NULL }
1191 static const char *number_name[] = {
1192 "zero", "one", "two", "three", "four",
1193 "five", "six", "seven", "eight", "nine", "ten",
1196 static const struct typelen {
1197 const char *type;
1198 int length;
1199 } typelen[] = {
1200 { "seconds", 1 },
1201 { "minutes", 60 },
1202 { "hours", 60*60 },
1203 { "days", 24*60*60 },
1204 { "weeks", 7*24*60*60 },
1205 { NULL }
1208 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
1210 const struct typelen *tl;
1211 const struct special *s;
1212 const char *end = date;
1213 int i;
1215 while (isalpha(*++end))
1218 for (i = 0; i < 12; i++) {
1219 int match = match_string(date, month_names[i]);
1220 if (match >= 3) {
1221 tm->tm_mon = i;
1222 *touched = 1;
1223 return end;
1227 for (s = special; s->name; s++) {
1228 int len = strlen(s->name);
1229 if (match_string(date, s->name) == len) {
1230 s->fn(tm, now, num);
1231 *touched = 1;
1232 return end;
1236 if (!*num) {
1237 for (i = 1; i < 11; i++) {
1238 int len = strlen(number_name[i]);
1239 if (match_string(date, number_name[i]) == len) {
1240 *num = i;
1241 *touched = 1;
1242 return end;
1245 if (match_string(date, "last") == 4) {
1246 *num = 1;
1247 *touched = 1;
1249 return end;
1252 tl = typelen;
1253 while (tl->type) {
1254 int len = strlen(tl->type);
1255 if (match_string(date, tl->type) >= len-1) {
1256 update_tm(tm, now, tl->length * *num);
1257 *num = 0;
1258 *touched = 1;
1259 return end;
1261 tl++;
1264 for (i = 0; i < 7; i++) {
1265 int match = match_string(date, weekday_names[i]);
1266 if (match >= 3) {
1267 int diff, n = *num -1;
1268 *num = 0;
1270 diff = tm->tm_wday - i;
1271 if (diff <= 0)
1272 n++;
1273 diff += 7*n;
1275 update_tm(tm, now, diff * 24 * 60 * 60);
1276 *touched = 1;
1277 return end;
1281 if (match_string(date, "months") >= 5) {
1282 int n;
1283 update_tm(tm, now, 0); /* fill in date fields if needed */
1284 n = tm->tm_mon - *num;
1285 *num = 0;
1286 while (n < 0) {
1287 n += 12;
1288 tm->tm_year--;
1290 tm->tm_mon = n;
1291 *touched = 1;
1292 return end;
1295 if (match_string(date, "years") >= 4) {
1296 update_tm(tm, now, 0); /* fill in date fields if needed */
1297 tm->tm_year -= *num;
1298 *num = 0;
1299 *touched = 1;
1300 return end;
1303 return end;
1306 static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1307 time_t now)
1309 char *end;
1310 timestamp_t number = parse_timestamp(date, &end, 10);
1312 switch (*end) {
1313 case ':':
1314 case '.':
1315 case '/':
1316 case '-':
1317 if (isdigit(end[1])) {
1318 int match = match_multi_number(number, *end, date, end,
1319 tm, now);
1320 if (match)
1321 return date + match;
1325 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1326 if (date[0] != '0' || end - date <= 2)
1327 *num = number;
1328 return end;
1331 static timestamp_t approxidate_str(const char *date,
1332 const struct timeval *tv,
1333 int *error_ret)
1335 int number = 0;
1336 int touched = 0;
1337 struct tm tm, now;
1338 time_t time_sec;
1340 time_sec = tv->tv_sec;
1341 localtime_r(&time_sec, &tm);
1342 now = tm;
1344 tm.tm_year = -1;
1345 tm.tm_mon = -1;
1346 tm.tm_mday = -1;
1348 for (;;) {
1349 unsigned char c = *date;
1350 if (!c)
1351 break;
1352 date++;
1353 if (isdigit(c)) {
1354 pending_number(&tm, &number);
1355 date = approxidate_digit(date-1, &tm, &number, time_sec);
1356 touched = 1;
1357 continue;
1359 if (isalpha(c))
1360 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
1362 pending_number(&tm, &number);
1363 if (!touched)
1364 *error_ret = 1;
1365 return (timestamp_t)update_tm(&tm, &now, 0);
1368 timestamp_t approxidate_relative(const char *date)
1370 struct timeval tv;
1371 timestamp_t timestamp;
1372 int offset;
1373 int errors = 0;
1375 if (!parse_date_basic(date, &timestamp, &offset))
1376 return timestamp;
1378 get_time(&tv);
1379 return approxidate_str(date, (const struct timeval *) &tv, &errors);
1382 timestamp_t approxidate_careful(const char *date, int *error_ret)
1384 struct timeval tv;
1385 timestamp_t timestamp;
1386 int offset;
1387 int dummy = 0;
1388 if (!error_ret)
1389 error_ret = &dummy;
1391 if (!parse_date_basic(date, &timestamp, &offset)) {
1392 *error_ret = 0;
1393 return timestamp;
1396 get_time(&tv);
1397 return approxidate_str(date, &tv, error_ret);
1400 int date_overflows(timestamp_t t)
1402 time_t sys;
1404 /* If we overflowed our timestamp data type, that's bad... */
1405 if ((uintmax_t)t >= TIME_MAX)
1406 return 1;
1409 * ...but we also are going to feed the result to system
1410 * functions that expect time_t, which is often "signed long".
1411 * Make sure that we fit into time_t, as well.
1413 sys = t;
1414 return t != sys || (t < 1) != (sys < 1);