interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / date.c

1455 lines · 34538 bytes Raw Blame History
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
7#define DISABLE_SIGN_COMPARE_WARNINGS
8
9#include "git-compat-util.h"
10#include "date.h"
11#include "gettext.h"
12#include "pager.h"
13#include "strbuf.h"
14
15/*
16 * This is like mktime, but without normalization of tm_wday and tm_yday.
17 */
18time_t tm_to_time_t(const struct tm *tm)
19{
20 static const int mdays[] = {
21 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
22 };
23 int year = tm->tm_year - 70;
24 int month = tm->tm_mon;
25 int day = tm->tm_mday;
26
27 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
28 return -1;
29 if (month < 0 || month > 11) /* array bounds */
30 return -1;
31 if (month < 2 || (year + 2) % 4)
32 day--;
33 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
34 return -1;
35 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
36 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
37}
38
39static const char *month_names[] = {
40 "January", "February", "March", "April", "May", "June",
41 "July", "August", "September", "October", "November", "December"
42};
43
44static const char *weekday_names[] = {
45 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
46};
47
48int tz_to_minutes(int tz)
49{
50 int minutes = tz < 0 ? -tz : tz;
51 minutes = (minutes / 100) * 60 + (minutes % 100);
52 return tz < 0 ? -minutes : minutes;
53}
54
55int minutes_to_tz(int minutes)
56{
57 int tz = minutes < 0 ? -minutes : minutes;
58 tz = (tz / 60) * 100 + (tz % 60);
59 return minutes < 0 ? -tz : tz;
60}
61
62static time_t gm_time_t(timestamp_t time, int tz)
63{
64 int minutes = tz_to_minutes(tz);
65
66 if (minutes > 0) {
67 if (unsigned_add_overflows(time, minutes * 60))
68 die("Timestamp+tz too large: %"PRItime" +%04d",
69 time, tz);
70 } else if (time < -minutes * 60)
71 die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);
72 time += minutes * 60;
73 if (date_overflows(time))
74 die("Timestamp too large for this system: %"PRItime, time);
75 return (time_t)time;
76}
77
78/*
79 * The "tz" thing is passed in as this strange "decimal parse of tz"
80 * thing, which means that tz -0100 is passed in as the integer -100,
81 * even though it means "sixty minutes off"
82 */
83static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm)
84{
85 time_t t = gm_time_t(time, tz);
86 return gmtime_r(&t, tm);
87}
88
89static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)
90{
91 time_t t = time;
92 return localtime_r(&t, tm);
93}
94
95/*
96 * Fill in the localtime 'struct tm' for the supplied time,
97 * and return the local tz.
98 */
99static int local_time_tzoffset(time_t t, struct tm *tm)
100{
101 time_t t_local;
102 int offset, eastwest;
103
104 localtime_r(&t, tm);
105 t_local = tm_to_time_t(tm);
106 if (t_local == -1)
107 return 0; /* error; just use +0000 */
108 if (t_local < t) {
109 eastwest = -1;
110 offset = t - t_local;
111 } else {
112 eastwest = 1;
113 offset = t_local - t;
114 }
115 offset /= 60; /* in minutes */
116 return minutes_to_tz(offset) * eastwest;
117}
118
119/*
120 * What value of "tz" was in effect back then at "time" in the
121 * local timezone?
122 */
123static int local_tzoffset(timestamp_t time)
124{
125 struct tm tm;
126
127 if (date_overflows(time))
128 die("Timestamp too large for this system: %"PRItime, time);
129
130 return local_time_tzoffset((time_t)time, &tm);
131}
132
133static void get_time(struct timeval *now)
134{
135 const char *x;
136
137 x = getenv("GIT_TEST_DATE_NOW");
138 if (x) {
139 now->tv_sec = atoi(x);
140 now->tv_usec = 0;
141 }
142 else
143 gettimeofday(now, NULL);
144}
145
146void show_date_relative(timestamp_t time, struct strbuf *timebuf)
147{
148 struct timeval now;
149 timestamp_t diff;
150
151 get_time(&now);
152 if (now.tv_sec < time) {
153 strbuf_addstr(timebuf, _("in the future"));
154 return;
155 }
156 diff = now.tv_sec - time;
157 if (diff < 90) {
158 strbuf_addf(timebuf,
159 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
160 return;
161 }
162 /* Turn it into minutes */
163 diff = (diff + 30) / 60;
164 if (diff < 90) {
165 strbuf_addf(timebuf,
166 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
167 return;
168 }
169 /* Turn it into hours */
170 diff = (diff + 30) / 60;
171 if (diff < 36) {
172 strbuf_addf(timebuf,
173 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
174 return;
175 }
176 /* We deal with number of days from here on */
177 diff = (diff + 12) / 24;
178 if (diff < 14) {
179 strbuf_addf(timebuf,
180 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
181 return;
182 }
183 /* Say weeks for the past 10 weeks or so */
184 if (diff < 70) {
185 strbuf_addf(timebuf,
186 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
187 (diff + 3) / 7);
188 return;
189 }
190 /* Say months for the past 12 months or so */
191 if (diff < 365) {
192 strbuf_addf(timebuf,
193 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
194 (diff + 15) / 30);
195 return;
196 }
197 /* Give years and months for 5 years or so */
198 if (diff < 1825) {
199 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
200 timestamp_t years = totalmonths / 12;
201 timestamp_t months = totalmonths % 12;
202 if (months) {
203 struct strbuf sb = STRBUF_INIT;
204 strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
205 strbuf_addf(timebuf,
206 /* TRANSLATORS: "%s" is "<n> years" */
207 Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
208 sb.buf, months);
209 strbuf_release(&sb);
210 } else
211 strbuf_addf(timebuf,
212 Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
213 return;
214 }
215 /* Otherwise, just years. Centuries is probably overkill. */
216 strbuf_addf(timebuf,
217 Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
218 (diff + 183) / 365);
219}
220
221struct date_mode date_mode_from_type(enum date_mode_type type)
222{
223 struct date_mode mode = DATE_MODE_INIT;
224 if (type == DATE_STRFTIME)
225 BUG("cannot create anonymous strftime date_mode struct");
226 mode.type = type;
227 return mode;
228}
229
230static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local)
231{
232 struct {
233 unsigned int year:1,
234 date:1,
235 wday:1,
236 time:1,
237 seconds:1,
238 tz:1;
239 } hide = { 0 };
240
241 hide.tz = local || tz == human_tz;
242 hide.year = tm->tm_year == human_tm->tm_year;
243 if (hide.year) {
244 if (tm->tm_mon == human_tm->tm_mon) {
245 if (tm->tm_mday > human_tm->tm_mday) {
246 /* Future date: think timezones */
247 } else if (tm->tm_mday == human_tm->tm_mday) {
248 hide.date = hide.wday = 1;
249 } else if (tm->tm_mday + 5 > human_tm->tm_mday) {
250 /* Leave just weekday if it was a few days ago */
251 hide.date = 1;
252 }
253 }
254 }
255
256 /* Show "today" times as just relative times */
257 if (hide.wday) {
258 show_date_relative(time, buf);
259 return;
260 }
261
262 /*
263 * Always hide seconds for human-readable.
264 * Hide timezone if showing date.
265 * Hide weekday and time if showing year.
266 *
267 * The logic here is two-fold:
268 * (a) only show details when recent enough to matter
269 * (b) keep the maximum length "similar", and in check
270 */
271 if (human_tm->tm_year) {
272 hide.seconds = 1;
273 hide.tz |= !hide.date;
274 hide.wday = hide.time = !hide.year;
275 }
276
277 if (!hide.wday)
278 strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]);
279 if (!hide.date)
280 strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday);
281
282 /* Do we want AM/PM depending on locale? */
283 if (!hide.time) {
284 strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min);
285 if (!hide.seconds)
286 strbuf_addf(buf, ":%02d", tm->tm_sec);
287 } else
288 strbuf_rtrim(buf);
289
290 if (!hide.year)
291 strbuf_addf(buf, " %d", tm->tm_year + 1900);
292
293 if (!hide.tz)
294 strbuf_addf(buf, " %+05d", tz);
295}
296
297const char *show_date(timestamp_t time, int tz, struct date_mode mode)
298{
299 struct tm *tm;
300 struct tm tmbuf = { 0 };
301 struct tm human_tm = { 0 };
302 int human_tz = -1;
303 static struct strbuf timebuf = STRBUF_INIT;
304
305 if (mode.type == DATE_UNIX) {
306 strbuf_reset(&timebuf);
307 strbuf_addf(&timebuf, "%"PRItime, time);
308 return timebuf.buf;
309 }
310
311 if (mode.type == DATE_HUMAN) {
312 struct timeval now;
313
314 get_time(&now);
315
316 /* Fill in the data for "current time" in human_tz and human_tm */
317 human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
318 }
319
320 if (mode.local)
321 tz = local_tzoffset(time);
322
323 if (mode.type == DATE_RAW) {
324 strbuf_reset(&timebuf);
325 strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
326 return timebuf.buf;
327 }
328
329 if (mode.type == DATE_RELATIVE) {
330 strbuf_reset(&timebuf);
331 show_date_relative(time, &timebuf);
332 return timebuf.buf;
333 }
334
335 if (mode.local)
336 tm = time_to_tm_local(time, &tmbuf);
337 else
338 tm = time_to_tm(time, tz, &tmbuf);
339 if (!tm) {
340 tm = time_to_tm(0, 0, &tmbuf);
341 tz = 0;
342 }
343
344 strbuf_reset(&timebuf);
345 if (mode.type == DATE_SHORT)
346 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
347 tm->tm_mon + 1, tm->tm_mday);
348 else if (mode.type == DATE_ISO8601)
349 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
350 tm->tm_year + 1900,
351 tm->tm_mon + 1,
352 tm->tm_mday,
353 tm->tm_hour, tm->tm_min, tm->tm_sec,
354 tz);
355 else if (mode.type == DATE_ISO8601_STRICT) {
356 strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d",
357 tm->tm_year + 1900,
358 tm->tm_mon + 1,
359 tm->tm_mday,
360 tm->tm_hour, tm->tm_min, tm->tm_sec);
361 if (tz == 0) {
362 strbuf_addch(&timebuf, 'Z');
363 } else {
364 strbuf_addch(&timebuf, tz >= 0 ? '+' : '-');
365 tz = abs(tz);
366 strbuf_addf(&timebuf, "%02d:%02d", tz / 100, tz % 100);
367 }
368 } else if (mode.type == DATE_RFC2822)
369 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
370 weekday_names[tm->tm_wday], tm->tm_mday,
371 month_names[tm->tm_mon], tm->tm_year + 1900,
372 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
373 else if (mode.type == DATE_STRFTIME)
374 strbuf_addftime(&timebuf, mode.strftime_fmt, tm, tz,
375 !mode.local);
376 else
377 show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode.local);
378 return timebuf.buf;
379}
380
381/*
382 * Check these. And note how it doesn't do the summer-time conversion.
383 *
384 * In my world, it's always summer, and things are probably a bit off
385 * in other ways too.
386 */
387static const struct {
388 const char *name;
389 int offset;
390 int dst;
391} timezone_names[] = {
392 { "IDLW", -12, 0, }, /* International Date Line West */
393 { "NT", -11, 0, }, /* Nome */
394 { "CAT", -10, 0, }, /* Central Alaska */
395 { "HST", -10, 0, }, /* Hawaii Standard */
396 { "HDT", -10, 1, }, /* Hawaii Daylight */
397 { "YST", -9, 0, }, /* Yukon Standard */
398 { "YDT", -9, 1, }, /* Yukon Daylight */
399 { "PST", -8, 0, }, /* Pacific Standard */
400 { "PDT", -8, 1, }, /* Pacific Daylight */
401 { "MST", -7, 0, }, /* Mountain Standard */
402 { "MDT", -7, 1, }, /* Mountain Daylight */
403 { "CST", -6, 0, }, /* Central Standard */
404 { "CDT", -6, 1, }, /* Central Daylight */
405 { "EST", -5, 0, }, /* Eastern Standard */
406 { "EDT", -5, 1, }, /* Eastern Daylight */
407 { "AST", -3, 0, }, /* Atlantic Standard */
408 { "ADT", -3, 1, }, /* Atlantic Daylight */
409 { "WAT", -1, 0, }, /* West Africa */
410
411 { "GMT", 0, 0, }, /* Greenwich Mean */
412 { "UTC", 0, 0, }, /* Universal (Coordinated) */
413 { "Z", 0, 0, }, /* Zulu, alias for UTC */
414
415 { "WET", 0, 0, }, /* Western European */
416 { "BST", 0, 1, }, /* British Summer */
417 { "CET", +1, 0, }, /* Central European */
418 { "MET", +1, 0, }, /* Middle European */
419 { "MEWT", +1, 0, }, /* Middle European Winter */
420 { "MEST", +1, 1, }, /* Middle European Summer */
421 { "CEST", +1, 1, }, /* Central European Summer */
422 { "MESZ", +1, 1, }, /* Middle European Summer */
423 { "FWT", +1, 0, }, /* French Winter */
424 { "FST", +1, 1, }, /* French Summer */
425 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
426 { "EEST", +2, 1, }, /* Eastern European Daylight */
427 { "WAST", +7, 0, }, /* West Australian Standard */
428 { "WADT", +7, 1, }, /* West Australian Daylight */
429 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
430 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
431 { "EAST", +10, 0, }, /* Eastern Australian Standard */
432 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
433 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
434 { "NZT", +12, 0, }, /* New Zealand */
435 { "NZST", +12, 0, }, /* New Zealand Standard */
436 { "NZDT", +12, 1, }, /* New Zealand Daylight */
437 { "IDLE", +12, 0, }, /* International Date Line East */
438};
439
440static int match_string(const char *date, const char *str)
441{
442 int i = 0;
443
444 for (i = 0; *date; date++, str++, i++) {
445 if (*date == *str)
446 continue;
447 if (toupper(*date) == toupper(*str))
448 continue;
449 if (!isalnum(*date))
450 break;
451 return 0;
452 }
453 return i;
454}
455
456static int skip_alpha(const char *date)
457{
458 int i = 0;
459 do {
460 i++;
461 } while (isalpha(date[i]));
462 return i;
463}
464
465/*
466* Parse month, weekday, or timezone name
467*/
468static int match_alpha(const char *date, struct tm *tm, int *offset)
469{
470 int i;
471
472 for (i = 0; i < 12; i++) {
473 int match = match_string(date, month_names[i]);
474 if (match >= 3) {
475 tm->tm_mon = i;
476 return match;
477 }
478 }
479
480 for (i = 0; i < 7; i++) {
481 int match = match_string(date, weekday_names[i]);
482 if (match >= 3) {
483 tm->tm_wday = i;
484 return match;
485 }
486 }
487
488 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
489 int match = match_string(date, timezone_names[i].name);
490 if (match >= 3 || match == strlen(timezone_names[i].name)) {
491 int off = timezone_names[i].offset;
492
493 /* This is bogus, but we like summer */
494 off += timezone_names[i].dst;
495
496 /* Only use the tz name offset if we don't have anything better */
497 if (*offset == -1)
498 *offset = 60*off;
499
500 return match;
501 }
502 }
503
504 if (match_string(date, "PM") == 2) {
505 tm->tm_hour = (tm->tm_hour % 12) + 12;
506 return 2;
507 }
508
509 if (match_string(date, "AM") == 2) {
510 tm->tm_hour = (tm->tm_hour % 12) + 0;
511 return 2;
512 }
513
514 /* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */
515 if (*date == 'T' && isdigit(date[1]) && tm->tm_hour == -1) {
516 tm->tm_min = tm->tm_sec = 0;
517 return 1;
518 }
519
520 /* BAD CRAP */
521 return skip_alpha(date);
522}
523
524static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
525{
526 if (month > 0 && month < 13 && day > 0 && day < 32) {
527 struct tm check = *tm;
528 struct tm *r = (now_tm ? &check : tm);
529 time_t specified;
530
531 r->tm_mon = month - 1;
532 r->tm_mday = day;
533 if (year == -1) {
534 if (!now_tm)
535 return 1;
536 r->tm_year = now_tm->tm_year;
537 }
538 else if (year >= 1970 && year < 2100)
539 r->tm_year = year - 1900;
540 else if (year > 70 && year < 100)
541 r->tm_year = year;
542 else if (year < 38)
543 r->tm_year = year + 100;
544 else
545 return -1;
546 if (!now_tm)
547 return 0;
548
549 specified = tm_to_time_t(r);
550
551 /* Be it commit time or author time, it does not make
552 * sense to specify timestamp way into the future. Make
553 * sure it is not later than ten days from now...
554 */
555 if ((specified != -1) && (now + 10*24*3600 < specified))
556 return -1;
557 tm->tm_mon = r->tm_mon;
558 tm->tm_mday = r->tm_mday;
559 if (year != -1)
560 tm->tm_year = r->tm_year;
561 return 0;
562 }
563 return -1;
564}
565
566static int set_time(long hour, long minute, long second, struct tm *tm)
567{
568 /* We accept 61st second because of leap second */
569 if (0 <= hour && hour <= 24 &&
570 0 <= minute && minute < 60 &&
571 0 <= second && second <= 60) {
572 tm->tm_hour = hour;
573 tm->tm_min = minute;
574 tm->tm_sec = second;
575 return 0;
576 }
577 return -1;
578}
579
580static int is_date_known(struct tm *tm)
581{
582 return tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1;
583}
584
585static int match_multi_number(timestamp_t num, char c, const char *date,
586 char *end, struct tm *tm, time_t now)
587{
588 struct tm now_tm;
589 struct tm *refuse_future;
590 long num2, num3;
591
592 num2 = strtol(end+1, &end, 10);
593 num3 = -1;
594 if (*end == c && isdigit(end[1]))
595 num3 = strtol(end+1, &end, 10);
596
597 /* Time? Date? */
598 switch (c) {
599 case ':':
600 if (num3 < 0)
601 num3 = 0;
602 if (set_time(num, num2, num3, tm) == 0) {
603 /*
604 * If %H:%M:%S was just parsed followed by: .<num4>
605 * Consider (& discard) it as fractional second
606 * if %Y%m%d is parsed before.
607 */
608 if (*end == '.' && isdigit(end[1]) && is_date_known(tm))
609 strtol(end + 1, &end, 10);
610 break;
611 }
612 return 0;
613
614 case '-':
615 case '/':
616 case '.':
617 if (!now)
618 now = time(NULL);
619 refuse_future = NULL;
620 if (gmtime_r(&now, &now_tm))
621 refuse_future = &now_tm;
622
623 if (num > 70) {
624 /* yyyy-mm-dd? */
625 if (set_date(num, num2, num3, NULL, now, tm) == 0)
626 break;
627 /* yyyy-dd-mm? */
628 if (set_date(num, num3, num2, NULL, now, tm) == 0)
629 break;
630 }
631 /* Our eastern European friends say dd.mm.yy[yy]
632 * is the norm there, so giving precedence to
633 * mm/dd/yy[yy] form only when separator is not '.'
634 */
635 if (c != '.' &&
636 set_date(num3, num, num2, refuse_future, now, tm) == 0)
637 break;
638 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
639 if (set_date(num3, num2, num, refuse_future, now, tm) == 0)
640 break;
641 /* Funny European mm.dd.yy */
642 if (c == '.' &&
643 set_date(num3, num, num2, refuse_future, now, tm) == 0)
644 break;
645 return 0;
646 }
647 return end - date;
648}
649
650/*
651 * Have we filled in any part of the time/date yet?
652 * We just do a binary 'and' to see if the sign bit
653 * is set in all the values.
654 */
655static inline int nodate(struct tm *tm)
656{
657 return (tm->tm_year &
658 tm->tm_mon &
659 tm->tm_mday &
660 tm->tm_hour &
661 tm->tm_min &
662 tm->tm_sec) < 0;
663}
664
665/*
666 * Have we seen an ISO-8601-alike date, i.e. 20220101T0,
667 * In which, hour is still unset,
668 * and minutes and second has been set to 0.
669 */
670static inline int maybeiso8601(struct tm *tm)
671{
672 return tm->tm_hour == -1 &&
673 tm->tm_min == 0 &&
674 tm->tm_sec == 0;
675}
676
677/*
678 * We've seen a digit. Time? Year? Date?
679 */
680static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
681{
682 int n;
683 char *end;
684 timestamp_t num;
685
686 num = parse_timestamp(date, &end, 10);
687
688 /*
689 * Seconds since 1970? We trigger on that for any numbers with
690 * more than 8 digits. This is because we don't want to rule out
691 * numbers like 20070606 as a YYYYMMDD date.
692 */
693 if (num >= 100000000 && nodate(tm)) {
694 time_t time = num;
695 if (gmtime_r(&time, tm)) {
696 *tm_gmt = 1;
697 return end - date;
698 }
699 }
700
701 /*
702 * Check for special formats: num[-.:/]num[same]num
703 */
704 switch (*end) {
705 case ':':
706 case '.':
707 case '/':
708 case '-':
709 if (isdigit(end[1])) {
710 int match = match_multi_number(num, *end, date, end, tm, 0);
711 if (match)
712 return match;
713 }
714 }
715
716 /*
717 * None of the special formats? Try to guess what
718 * the number meant. We use the number of digits
719 * to make a more educated guess..
720 */
721 n = 0;
722 do {
723 n++;
724 } while (isdigit(date[n]));
725
726 /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
727 /* 6 digits, compact style of ISO-8601's time: HHMMSS */
728 if (n == 8 || n == 6) {
729 unsigned int num1 = num / 10000;
730 unsigned int num2 = (num % 10000) / 100;
731 unsigned int num3 = num % 100;
732 if (n == 8)
733 set_date(num1, num2, num3, NULL, time(NULL), tm);
734 else if (n == 6 && set_time(num1, num2, num3, tm) == 0 &&
735 *end == '.' && isdigit(end[1]))
736 strtoul(end + 1, &end, 10);
737 return end - date;
738 }
739
740 /* reduced precision of ISO-8601's time: HHMM or HH */
741 if (maybeiso8601(tm)) {
742 unsigned int num1 = num;
743 unsigned int num2 = 0;
744 if (n == 4) {
745 num1 = num / 100;
746 num2 = num % 100;
747 }
748 if ((n == 4 || n == 2) && !nodate(tm) &&
749 set_time(num1, num2, 0, tm) == 0)
750 return n;
751 /*
752 * We thought this is an ISO-8601 time string,
753 * we set minutes and seconds to 0,
754 * turn out it isn't, rollback the change.
755 */
756 tm->tm_min = tm->tm_sec = -1;
757 }
758
759 /* Four-digit year or a timezone? */
760 if (n == 4) {
761 if (num <= 1400 && *offset == -1) {
762 unsigned int minutes = num % 100;
763 unsigned int hours = num / 100;
764 *offset = hours*60 + minutes;
765 } else if (num > 1900 && num < 2100)
766 tm->tm_year = num - 1900;
767 return n;
768 }
769
770 /*
771 * Ignore lots of numerals. We took care of 4-digit years above.
772 * Days or months must be one or two digits.
773 */
774 if (n > 2)
775 return n;
776
777 /*
778 * NOTE! We will give precedence to day-of-month over month or
779 * year numbers in the 1-12 range. So 05 is always "mday 5",
780 * unless we already have a mday..
781 *
782 * IOW, 01 Apr 05 parses as "April 1st, 2005".
783 */
784 if (num > 0 && num < 32 && tm->tm_mday < 0) {
785 tm->tm_mday = num;
786 return n;
787 }
788
789 /* Two-digit year? */
790 if (n == 2 && tm->tm_year < 0) {
791 if (num < 10 && tm->tm_mday >= 0) {
792 tm->tm_year = num + 100;
793 return n;
794 }
795 if (num >= 70) {
796 tm->tm_year = num;
797 return n;
798 }
799 }
800
801 if (num > 0 && num < 13 && tm->tm_mon < 0)
802 tm->tm_mon = num-1;
803
804 return n;
805}
806
807static int match_tz(const char *date, int *offp)
808{
809 char *end;
810 int hour = strtoul(date + 1, &end, 10);
811 int n = end - (date + 1);
812 int min = 0;
813
814 if (n == 4) {
815 /* hhmm */
816 min = hour % 100;
817 hour = hour / 100;
818 } else if (n != 2) {
819 min = 99; /* random crap */
820 } else if (*end == ':') {
821 /* hh:mm? */
822 min = strtoul(end + 1, &end, 10);
823 if (end - (date + 1) != 5)
824 min = 99; /* random crap */
825 } /* otherwise we parsed "hh" */
826
827 /*
828 * Don't accept any random crap. Even though some places have
829 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
830 * UTC+14), there is something wrong if hour part is much
831 * larger than that. We might also want to check that the
832 * minutes are divisible by 15 or something too. (Offset of
833 * Kathmandu, Nepal is UTC+5:45)
834 */
835 if (min < 60 && hour < 24) {
836 int offset = hour * 60 + min;
837 if (*date == '-')
838 offset = -offset;
839 *offp = offset;
840 }
841 return end - date;
842}
843
844static void date_string(timestamp_t date, int offset, struct strbuf *buf)
845{
846 int sign = '+';
847
848 if (offset < 0) {
849 offset = -offset;
850 sign = '-';
851 }
852 strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
853}
854
855/*
856 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
857 * only when it appears not as part of any other string.
858 */
859static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
860{
861 char *end;
862 timestamp_t stamp;
863 int ofs;
864
865 if (*date < '0' || '9' < *date)
866 return -1;
867 stamp = parse_timestamp(date, &end, 10);
868 if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
869 return -1;
870 date = end + 2;
871 ofs = strtol(date, &end, 10);
872 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
873 return -1;
874 ofs = tz_to_minutes(ofs);
875 if (date[-1] == '-')
876 ofs = -ofs;
877 *timestamp = stamp;
878 *offset = ofs;
879 return 0;
880}
881
882
883/* timestamp of 2099-12-31T23:59:59Z, including 32 leap days */
884static const timestamp_t timestamp_max = (((timestamp_t)2100 - 1970) * 365 + 32) * 24 * 60 * 60 - 1;
885
886/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
887 (i.e. English) day/month names, and it doesn't work correctly with %z. */
888int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
889{
890 struct tm tm;
891 int tm_gmt;
892 timestamp_t dummy_timestamp;
893 int dummy_offset;
894
895 if (!timestamp)
896 timestamp = &dummy_timestamp;
897 if (!offset)
898 offset = &dummy_offset;
899
900 memset(&tm, 0, sizeof(tm));
901 tm.tm_year = -1;
902 tm.tm_mon = -1;
903 tm.tm_mday = -1;
904 tm.tm_isdst = -1;
905 tm.tm_hour = -1;
906 tm.tm_min = -1;
907 tm.tm_sec = -1;
908 *offset = -1;
909 tm_gmt = 0;
910
911 if (*date == '@' &&
912 !match_object_header_date(date + 1, timestamp, offset))
913 return 0; /* success */
914 for (;;) {
915 int match = 0;
916 unsigned char c = *date;
917
918 /* Stop at end of string or newline */
919 if (!c || c == '\n')
920 break;
921
922 if (isalpha(c))
923 match = match_alpha(date, &tm, offset);
924 else if (isdigit(c))
925 match = match_digit(date, &tm, offset, &tm_gmt);
926 else if ((c == '-' || c == '+') && isdigit(date[1]))
927 match = match_tz(date, offset);
928
929 if (!match) {
930 /* BAD CRAP */
931 match = 1;
932 }
933
934 date += match;
935 }
936
937 /* do not use mktime(), which uses local timezone, here */
938 *timestamp = tm_to_time_t(&tm);
939 if (*timestamp == -1)
940 return -1;
941
942 if (*offset == -1) {
943 time_t temp_time;
944
945 /* gmtime_r() in match_digit() may have clobbered it */
946 tm.tm_isdst = -1;
947 temp_time = mktime(&tm);
948 if ((time_t)*timestamp > temp_time) {
949 *offset = ((time_t)*timestamp - temp_time) / 60;
950 } else {
951 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
952 }
953 }
954
955 if (!tm_gmt) {
956 if (*offset > 0 && *offset * 60 > *timestamp)
957 return -1;
958 if (*offset < 0 && -*offset * 60 > timestamp_max - *timestamp)
959 return -1;
960 *timestamp -= *offset * 60;
961 }
962
963 return 0; /* success */
964}
965
966int parse_expiry_date(const char *date, timestamp_t *timestamp)
967{
968 int errors = 0;
969
970 if (!strcmp(date, "never") || !strcmp(date, "false"))
971 *timestamp = 0;
972 else if (!strcmp(date, "all") || !strcmp(date, "now"))
973 /*
974 * We take over "now" here, which usually translates
975 * to the current timestamp. This is because the user
976 * really means to expire everything that was done in
977 * the past, and by definition reflogs are the record
978 * of the past, and there is nothing from the future
979 * to be kept.
980 */
981 *timestamp = TIME_MAX;
982 else
983 *timestamp = approxidate_careful(date, &errors);
984
985 return errors;
986}
987
988int parse_date(const char *date, struct strbuf *result)
989{
990 timestamp_t timestamp;
991 int offset;
992 if (parse_date_basic(date, &timestamp, &offset))
993 return -1;
994 date_string(timestamp, offset, result);
995 return 0;
996}
997
998static enum date_mode_type parse_date_type(const char *format, const char **end)
999{
1000 if (skip_prefix(format, "relative", end))
1001 return DATE_RELATIVE;
1002 if (skip_prefix(format, "iso8601-strict", end) ||
1003 skip_prefix(format, "iso-strict", end))
1004 return DATE_ISO8601_STRICT;
1005 if (skip_prefix(format, "iso8601", end) ||
1006 skip_prefix(format, "iso", end))
1007 return DATE_ISO8601;
1008 if (skip_prefix(format, "rfc2822", end) ||
1009 skip_prefix(format, "rfc", end))
1010 return DATE_RFC2822;
1011 if (skip_prefix(format, "short", end))
1012 return DATE_SHORT;
1013 if (skip_prefix(format, "default", end))
1014 return DATE_NORMAL;
1015 if (skip_prefix(format, "human", end))
1016 return DATE_HUMAN;
1017 if (skip_prefix(format, "raw", end))
1018 return DATE_RAW;
1019 if (skip_prefix(format, "unix", end))
1020 return DATE_UNIX;
1021 if (skip_prefix(format, "format", end))
1022 return DATE_STRFTIME;
1023 /*
1024 * Please update $__git_log_date_formats in
1025 * git-completion.bash when you add new formats.
1026 */
1027
1028 die("unknown date format %s", format);
1029}
1030
1031void parse_date_format(const char *format, struct date_mode *mode)
1032{
1033 const char *p;
1034
1035 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
1036 if (skip_prefix(format, "auto:", &p)) {
1037 if (isatty(1) || pager_in_use())
1038 format = p;
1039 else
1040 format = "default";
1041 }
1042
1043 /* historical alias */
1044 if (!strcmp(format, "local"))
1045 format = "default-local";
1046
1047 mode->type = parse_date_type(format, &p);
1048 mode->local = 0;
1049
1050 if (skip_prefix(p, "-local", &p))
1051 mode->local = 1;
1052
1053 if (mode->type == DATE_STRFTIME) {
1054 if (!skip_prefix(p, ":", &p))
1055 die("date format missing colon separator: %s", format);
1056 mode->strftime_fmt = xstrdup(p);
1057 } else if (*p)
1058 die("unknown date format %s", format);
1059}
1060
1061void date_mode_release(struct date_mode *mode)
1062{
1063 free((char *)mode->strftime_fmt);
1064}
1065
1066void datestamp(struct strbuf *out)
1067{
1068 time_t now;
1069 int offset;
1070 struct tm tm = { 0 };
1071
1072 time(&now);
1073
1074 offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
1075 offset /= 60;
1076
1077 date_string(now, offset, out);
1078}
1079
1080/*
1081 * Relative time update (eg "2 days ago"). If we haven't set the time
1082 * yet, we need to set it from current time.
1083 *
1084 * The tm->tm_mday field has an additional logic of using negative values
1085 * for date adjustments: -2 means yesterday and -3 the day before that,
1086 * and so on. The idea is to defer such adjustments until we are sure
1087 * there's no explicit mday specification in the approxidate string.
1088 */
1089static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
1090{
1091 time_t n;
1092
1093 if (tm->tm_mday < 0) {
1094 int offset = tm->tm_mday + 1;
1095 if (sec == 0 && offset < 0)
1096 sec = -offset * 24*60*60;
1097 tm->tm_mday = now->tm_mday;
1098 }
1099 if (tm->tm_mon < 0)
1100 tm->tm_mon = now->tm_mon;
1101 if (tm->tm_year < 0) {
1102 tm->tm_year = now->tm_year;
1103 if (tm->tm_mon > now->tm_mon)
1104 tm->tm_year--;
1105 }
1106
1107 n = mktime(tm) - sec;
1108 localtime_r(&n, tm);
1109 return n;
1110}
1111
1112/*
1113 * Do we have a pending number at the end, or when
1114 * we see a new one? Let's assume it's a month day,
1115 * as in "Dec 6, 1992"
1116 */
1117static void pending_number(struct tm *tm, int *num)
1118{
1119 int number = *num;
1120
1121 if (number) {
1122 *num = 0;
1123 if (tm->tm_mday < 0 && number < 32)
1124 tm->tm_mday = number;
1125 else if (tm->tm_mon < 0 && number < 13)
1126 tm->tm_mon = number-1;
1127 else if (tm->tm_year < 0) {
1128 if (number > 1969 && number < 2100)
1129 tm->tm_year = number - 1900;
1130 else if (number > 69 && number < 100)
1131 tm->tm_year = number;
1132 else if (number < 38)
1133 tm->tm_year = 100 + number;
1134 /* We screw up for number = 00 ? */
1135 }
1136 }
1137}
1138
1139static void date_now(struct tm *tm, struct tm *now, int *num)
1140{
1141 *num = 0;
1142 update_tm(tm, now, 0);
1143}
1144
1145static void date_yesterday(struct tm *tm, struct tm *now, int *num)
1146{
1147 *num = 0;
1148 tm->tm_mday = -1;
1149 update_tm(tm, now, 24*60*60);
1150}
1151
1152static void date_time(struct tm *tm, int hour)
1153{
1154 /*
1155 * If we do not yet have a specified day, we'll use the most recent
1156 * version of "hour" relative to now. But that may be yesterday.
1157 */
1158 if (tm->tm_mday < 0 && tm->tm_hour < hour)
1159 tm->tm_mday = -2; /* eventually handled by update_tm() */
1160 tm->tm_hour = hour;
1161 tm->tm_min = 0;
1162 tm->tm_sec = 0;
1163}
1164
1165static void date_midnight(struct tm *tm, struct tm *now UNUSED, int *num)
1166{
1167 pending_number(tm, num);
1168 date_time(tm, 0);
1169}
1170
1171static void date_noon(struct tm *tm, struct tm *now UNUSED, int *num)
1172{
1173 pending_number(tm, num);
1174 date_time(tm, 12);
1175}
1176
1177static void date_tea(struct tm *tm, struct tm *now UNUSED, int *num)
1178{
1179 pending_number(tm, num);
1180 date_time(tm, 17);
1181}
1182
1183static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
1184{
1185 int hour, n = *num;
1186 *num = 0;
1187
1188 hour = tm->tm_hour;
1189 if (n) {
1190 hour = n;
1191 tm->tm_min = 0;
1192 tm->tm_sec = 0;
1193 }
1194 tm->tm_hour = (hour % 12) + 12;
1195}
1196
1197static void date_am(struct tm *tm, struct tm *now UNUSED, int *num)
1198{
1199 int hour, n = *num;
1200 *num = 0;
1201
1202 hour = tm->tm_hour;
1203 if (n) {
1204 hour = n;
1205 tm->tm_min = 0;
1206 tm->tm_sec = 0;
1207 }
1208 tm->tm_hour = (hour % 12);
1209}
1210
1211static void date_never(struct tm *tm, struct tm *now UNUSED, int *num)
1212{
1213 time_t n = 0;
1214 localtime_r(&n, tm);
1215 *num = 0;
1216}
1217
1218static void date_today(struct tm *tm, struct tm *now, int *num)
1219{
1220 if (tm->tm_hour == now->tm_hour &&
1221 tm->tm_min == now->tm_min &&
1222 tm->tm_sec == now->tm_sec)
1223 date_time(tm, 0);
1224 *num = 0;
1225 tm->tm_mday = -1;
1226 update_tm(tm, now, 0);
1227}
1228
1229static const struct special {
1230 const char *name;
1231 void (*fn)(struct tm *, struct tm *, int *);
1232} special[] = {
1233 { "yesterday", date_yesterday },
1234 { "noon", date_noon },
1235 { "midnight", date_midnight },
1236 { "tea", date_tea },
1237 { "PM", date_pm },
1238 { "AM", date_am },
1239 { "never", date_never },
1240 { "now", date_now },
1241 { "today", date_today },
1242 { NULL }
1243};
1244
1245static const char *number_name[] = {
1246 "zero", "one", "two", "three", "four",
1247 "five", "six", "seven", "eight", "nine", "ten",
1248};
1249
1250static const struct typelen {
1251 const char *type;
1252 int length;
1253} typelen[] = {
1254 { "seconds", 1 },
1255 { "minutes", 60 },
1256 { "hours", 60*60 },
1257 { "days", 24*60*60 },
1258 { "weeks", 7*24*60*60 },
1259 { NULL }
1260};
1261
1262static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
1263{
1264 const struct typelen *tl;
1265 const struct special *s;
1266 const char *end = date;
1267 int i;
1268
1269 while (isalpha(*++end))
1270 ;
1271
1272 for (i = 0; i < 12; i++) {
1273 int match = match_string(date, month_names[i]);
1274 if (match >= 3) {
1275 tm->tm_mon = i;
1276 *touched = 1;
1277 return end;
1278 }
1279 }
1280
1281 for (s = special; s->name; s++) {
1282 size_t len = strlen(s->name);
1283 if (match_string(date, s->name) == len) {
1284 s->fn(tm, now, num);
1285 *touched = 1;
1286 return end;
1287 }
1288 }
1289
1290 if (!*num) {
1291 for (i = 1; i < 11; i++) {
1292 size_t len = strlen(number_name[i]);
1293 if (match_string(date, number_name[i]) == len) {
1294 *num = i;
1295 *touched = 1;
1296 return end;
1297 }
1298 }
1299 if (match_string(date, "last") == 4) {
1300 *num = 1;
1301 *touched = 1;
1302 }
1303 return end;
1304 }
1305
1306 tl = typelen;
1307 while (tl->type) {
1308 size_t len = strlen(tl->type);
1309 if (match_string(date, tl->type) >= len-1) {
1310 update_tm(tm, now, tl->length * *num);
1311 *num = 0;
1312 *touched = 1;
1313 return end;
1314 }
1315 tl++;
1316 }
1317
1318 for (i = 0; i < 7; i++) {
1319 int match = match_string(date, weekday_names[i]);
1320 if (match >= 3) {
1321 int diff, n = *num -1;
1322 *num = 0;
1323
1324 diff = tm->tm_wday - i;
1325 if (diff <= 0)
1326 n++;
1327 diff += 7*n;
1328
1329 update_tm(tm, now, diff * 24 * 60 * 60);
1330 *touched = 1;
1331 return end;
1332 }
1333 }
1334
1335 if (match_string(date, "months") >= 5) {
1336 int n;
1337 update_tm(tm, now, 0); /* fill in date fields if needed */
1338 n = tm->tm_mon - *num;
1339 *num = 0;
1340 while (n < 0) {
1341 n += 12;
1342 tm->tm_year--;
1343 }
1344 tm->tm_mon = n;
1345 *touched = 1;
1346 return end;
1347 }
1348
1349 if (match_string(date, "years") >= 4) {
1350 update_tm(tm, now, 0); /* fill in date fields if needed */
1351 tm->tm_year -= *num;
1352 *num = 0;
1353 *touched = 1;
1354 return end;
1355 }
1356
1357 return end;
1358}
1359
1360static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1361 time_t now)
1362{
1363 char *end;
1364 timestamp_t number = parse_timestamp(date, &end, 10);
1365
1366 switch (*end) {
1367 case ':':
1368 case '.':
1369 case '/':
1370 case '-':
1371 if (isdigit(end[1])) {
1372 int match = match_multi_number(number, *end, date, end,
1373 tm, now);
1374 if (match)
1375 return date + match;
1376 }
1377 }
1378
1379 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1380 if (date[0] != '0' || end - date <= 2)
1381 *num = number;
1382 return end;
1383}
1384
1385static timestamp_t approxidate_str(const char *date,
1386 const struct timeval *tv,
1387 int *error_ret)
1388{
1389 int number = 0;
1390 int touched = 0;
1391 struct tm tm, now;
1392 time_t time_sec;
1393
1394 time_sec = tv->tv_sec;
1395 localtime_r(&time_sec, &tm);
1396 now = tm;
1397
1398 tm.tm_year = -1;
1399 tm.tm_mon = -1;
1400 tm.tm_mday = -1;
1401
1402 for (;;) {
1403 unsigned char c = *date;
1404 if (!c)
1405 break;
1406 date++;
1407 if (isdigit(c)) {
1408 pending_number(&tm, &number);
1409 date = approxidate_digit(date-1, &tm, &number, time_sec);
1410 touched = 1;
1411 continue;
1412 }
1413 if (isalpha(c))
1414 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
1415 }
1416 pending_number(&tm, &number);
1417 if (!touched)
1418 *error_ret = 1;
1419 return (timestamp_t)update_tm(&tm, &now, 0);
1420}
1421
1422timestamp_t approxidate_careful(const char *date, int *error_ret)
1423{
1424 struct timeval tv;
1425 timestamp_t timestamp;
1426 int offset;
1427 int dummy = 0;
1428 if (!error_ret)
1429 error_ret = &dummy;
1430
1431 if (!parse_date_basic(date, &timestamp, &offset)) {
1432 *error_ret = 0;
1433 return timestamp;
1434 }
1435
1436 get_time(&tv);
1437 return approxidate_str(date, &tv, error_ret);
1438}
1439
1440int date_overflows(timestamp_t t)
1441{
1442 time_t sys;
1443
1444 /* If we overflowed our timestamp data type, that's bad... */
1445 if ((uintmax_t)t >= TIME_MAX)
1446 return 1;
1447
1448 /*
1449 * ...but we also are going to feed the result to system
1450 * functions that expect time_t, which is often "signed long".
1451 * Make sure that we fit into time_t, as well.
1452 */
1453 sys = t;
1454 return t != sys || (t < 1) != (sys < 1);
1455}