credential.c: store "wwwauth[]" values in `credential_read()`
[git/debian.git] / date.c
blobe867ebf6b78fb7e91b1ba0d411cdd015ec69aecf
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
8 #include "date.h"
9 #include "gettext.h"
10 #include "pager.h"
13 * This is like mktime, but without normalization of tm_wday and tm_yday.
15 time_t tm_to_time_t(const struct tm *tm)
17 static const int mdays[] = {
18 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
20 int year = tm->tm_year - 70;
21 int month = tm->tm_mon;
22 int day = tm->tm_mday;
24 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
25 return -1;
26 if (month < 0 || month > 11) /* array bounds */
27 return -1;
28 if (month < 2 || (year + 2) % 4)
29 day--;
30 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
31 return -1;
32 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
33 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
36 static const char *month_names[] = {
37 "January", "February", "March", "April", "May", "June",
38 "July", "August", "September", "October", "November", "December"
41 static const char *weekday_names[] = {
42 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
45 static time_t gm_time_t(timestamp_t time, int tz)
47 int minutes;
49 minutes = tz < 0 ? -tz : tz;
50 minutes = (minutes / 100)*60 + (minutes % 100);
51 minutes = tz < 0 ? -minutes : minutes;
53 if (minutes > 0) {
54 if (unsigned_add_overflows(time, minutes * 60))
55 die("Timestamp+tz too large: %"PRItime" +%04d",
56 time, tz);
57 } else if (time < -minutes * 60)
58 die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);
59 time += minutes * 60;
60 if (date_overflows(time))
61 die("Timestamp too large for this system: %"PRItime, time);
62 return (time_t)time;
66 * The "tz" thing is passed in as this strange "decimal parse of tz"
67 * thing, which means that tz -0100 is passed in as the integer -100,
68 * even though it means "sixty minutes off"
70 static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm)
72 time_t t = gm_time_t(time, tz);
73 return gmtime_r(&t, tm);
76 static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)
78 time_t t = time;
79 return localtime_r(&t, tm);
83 * Fill in the localtime 'struct tm' for the supplied time,
84 * and return the local tz.
86 static int local_time_tzoffset(time_t t, struct tm *tm)
88 time_t t_local;
89 int offset, eastwest;
91 localtime_r(&t, tm);
92 t_local = tm_to_time_t(tm);
93 if (t_local == -1)
94 return 0; /* error; just use +0000 */
95 if (t_local < t) {
96 eastwest = -1;
97 offset = t - t_local;
98 } else {
99 eastwest = 1;
100 offset = t_local - t;
102 offset /= 60; /* in minutes */
103 offset = (offset % 60) + ((offset / 60) * 100);
104 return offset * eastwest;
108 * What value of "tz" was in effect back then at "time" in the
109 * local timezone?
111 static int local_tzoffset(timestamp_t time)
113 struct tm tm;
115 if (date_overflows(time))
116 die("Timestamp too large for this system: %"PRItime, time);
118 return local_time_tzoffset((time_t)time, &tm);
121 static void get_time(struct timeval *now)
123 const char *x;
125 x = getenv("GIT_TEST_DATE_NOW");
126 if (x) {
127 now->tv_sec = atoi(x);
128 now->tv_usec = 0;
130 else
131 gettimeofday(now, NULL);
134 void show_date_relative(timestamp_t time, struct strbuf *timebuf)
136 struct timeval now;
137 timestamp_t diff;
139 get_time(&now);
140 if (now.tv_sec < time) {
141 strbuf_addstr(timebuf, _("in the future"));
142 return;
144 diff = now.tv_sec - time;
145 if (diff < 90) {
146 strbuf_addf(timebuf,
147 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
148 return;
150 /* Turn it into minutes */
151 diff = (diff + 30) / 60;
152 if (diff < 90) {
153 strbuf_addf(timebuf,
154 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
155 return;
157 /* Turn it into hours */
158 diff = (diff + 30) / 60;
159 if (diff < 36) {
160 strbuf_addf(timebuf,
161 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
162 return;
164 /* We deal with number of days from here on */
165 diff = (diff + 12) / 24;
166 if (diff < 14) {
167 strbuf_addf(timebuf,
168 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
169 return;
171 /* Say weeks for the past 10 weeks or so */
172 if (diff < 70) {
173 strbuf_addf(timebuf,
174 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
175 (diff + 3) / 7);
176 return;
178 /* Say months for the past 12 months or so */
179 if (diff < 365) {
180 strbuf_addf(timebuf,
181 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
182 (diff + 15) / 30);
183 return;
185 /* Give years and months for 5 years or so */
186 if (diff < 1825) {
187 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
188 timestamp_t years = totalmonths / 12;
189 timestamp_t months = totalmonths % 12;
190 if (months) {
191 struct strbuf sb = STRBUF_INIT;
192 strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
193 strbuf_addf(timebuf,
194 /* TRANSLATORS: "%s" is "<n> years" */
195 Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
196 sb.buf, months);
197 strbuf_release(&sb);
198 } else
199 strbuf_addf(timebuf,
200 Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
201 return;
203 /* Otherwise, just years. Centuries is probably overkill. */
204 strbuf_addf(timebuf,
205 Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
206 (diff + 183) / 365);
209 struct date_mode *date_mode_from_type(enum date_mode_type type)
211 static struct date_mode mode = DATE_MODE_INIT;
212 if (type == DATE_STRFTIME)
213 BUG("cannot create anonymous strftime date_mode struct");
214 mode.type = type;
215 return &mode;
218 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)
220 struct {
221 unsigned int year:1,
222 date:1,
223 wday:1,
224 time:1,
225 seconds:1,
226 tz:1;
227 } hide = { 0 };
229 hide.tz = local || tz == human_tz;
230 hide.year = tm->tm_year == human_tm->tm_year;
231 if (hide.year) {
232 if (tm->tm_mon == human_tm->tm_mon) {
233 if (tm->tm_mday > human_tm->tm_mday) {
234 /* Future date: think timezones */
235 } else if (tm->tm_mday == human_tm->tm_mday) {
236 hide.date = hide.wday = 1;
237 } else if (tm->tm_mday + 5 > human_tm->tm_mday) {
238 /* Leave just weekday if it was a few days ago */
239 hide.date = 1;
244 /* Show "today" times as just relative times */
245 if (hide.wday) {
246 show_date_relative(time, buf);
247 return;
251 * Always hide seconds for human-readable.
252 * Hide timezone if showing date.
253 * Hide weekday and time if showing year.
255 * The logic here is two-fold:
256 * (a) only show details when recent enough to matter
257 * (b) keep the maximum length "similar", and in check
259 if (human_tm->tm_year) {
260 hide.seconds = 1;
261 hide.tz |= !hide.date;
262 hide.wday = hide.time = !hide.year;
265 if (!hide.wday)
266 strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]);
267 if (!hide.date)
268 strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday);
270 /* Do we want AM/PM depending on locale? */
271 if (!hide.time) {
272 strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min);
273 if (!hide.seconds)
274 strbuf_addf(buf, ":%02d", tm->tm_sec);
275 } else
276 strbuf_rtrim(buf);
278 if (!hide.year)
279 strbuf_addf(buf, " %d", tm->tm_year + 1900);
281 if (!hide.tz)
282 strbuf_addf(buf, " %+05d", tz);
285 const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
287 struct tm *tm;
288 struct tm tmbuf = { 0 };
289 struct tm human_tm = { 0 };
290 int human_tz = -1;
291 static struct strbuf timebuf = STRBUF_INIT;
293 if (mode->type == DATE_UNIX) {
294 strbuf_reset(&timebuf);
295 strbuf_addf(&timebuf, "%"PRItime, time);
296 return timebuf.buf;
299 if (mode->type == DATE_HUMAN) {
300 struct timeval now;
302 get_time(&now);
304 /* Fill in the data for "current time" in human_tz and human_tm */
305 human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
308 if (mode->local)
309 tz = local_tzoffset(time);
311 if (mode->type == DATE_RAW) {
312 strbuf_reset(&timebuf);
313 strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
314 return timebuf.buf;
317 if (mode->type == DATE_RELATIVE) {
318 strbuf_reset(&timebuf);
319 show_date_relative(time, &timebuf);
320 return timebuf.buf;
323 if (mode->local)
324 tm = time_to_tm_local(time, &tmbuf);
325 else
326 tm = time_to_tm(time, tz, &tmbuf);
327 if (!tm) {
328 tm = time_to_tm(0, 0, &tmbuf);
329 tz = 0;
332 strbuf_reset(&timebuf);
333 if (mode->type == DATE_SHORT)
334 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
335 tm->tm_mon + 1, tm->tm_mday);
336 else if (mode->type == DATE_ISO8601)
337 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
338 tm->tm_year + 1900,
339 tm->tm_mon + 1,
340 tm->tm_mday,
341 tm->tm_hour, tm->tm_min, tm->tm_sec,
342 tz);
343 else if (mode->type == DATE_ISO8601_STRICT) {
344 char sign = (tz >= 0) ? '+' : '-';
345 tz = abs(tz);
346 strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
347 tm->tm_year + 1900,
348 tm->tm_mon + 1,
349 tm->tm_mday,
350 tm->tm_hour, tm->tm_min, tm->tm_sec,
351 sign, tz / 100, tz % 100);
352 } else if (mode->type == DATE_RFC2822)
353 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
354 weekday_names[tm->tm_wday], tm->tm_mday,
355 month_names[tm->tm_mon], tm->tm_year + 1900,
356 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
357 else if (mode->type == DATE_STRFTIME)
358 strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
359 !mode->local);
360 else
361 show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local);
362 return timebuf.buf;
366 * Check these. And note how it doesn't do the summer-time conversion.
368 * In my world, it's always summer, and things are probably a bit off
369 * in other ways too.
371 static const struct {
372 const char *name;
373 int offset;
374 int dst;
375 } timezone_names[] = {
376 { "IDLW", -12, 0, }, /* International Date Line West */
377 { "NT", -11, 0, }, /* Nome */
378 { "CAT", -10, 0, }, /* Central Alaska */
379 { "HST", -10, 0, }, /* Hawaii Standard */
380 { "HDT", -10, 1, }, /* Hawaii Daylight */
381 { "YST", -9, 0, }, /* Yukon Standard */
382 { "YDT", -9, 1, }, /* Yukon Daylight */
383 { "PST", -8, 0, }, /* Pacific Standard */
384 { "PDT", -8, 1, }, /* Pacific Daylight */
385 { "MST", -7, 0, }, /* Mountain Standard */
386 { "MDT", -7, 1, }, /* Mountain Daylight */
387 { "CST", -6, 0, }, /* Central Standard */
388 { "CDT", -6, 1, }, /* Central Daylight */
389 { "EST", -5, 0, }, /* Eastern Standard */
390 { "EDT", -5, 1, }, /* Eastern Daylight */
391 { "AST", -3, 0, }, /* Atlantic Standard */
392 { "ADT", -3, 1, }, /* Atlantic Daylight */
393 { "WAT", -1, 0, }, /* West Africa */
395 { "GMT", 0, 0, }, /* Greenwich Mean */
396 { "UTC", 0, 0, }, /* Universal (Coordinated) */
397 { "Z", 0, 0, }, /* Zulu, alias for UTC */
399 { "WET", 0, 0, }, /* Western European */
400 { "BST", 0, 1, }, /* British Summer */
401 { "CET", +1, 0, }, /* Central European */
402 { "MET", +1, 0, }, /* Middle European */
403 { "MEWT", +1, 0, }, /* Middle European Winter */
404 { "MEST", +1, 1, }, /* Middle European Summer */
405 { "CEST", +1, 1, }, /* Central European Summer */
406 { "MESZ", +1, 1, }, /* Middle European Summer */
407 { "FWT", +1, 0, }, /* French Winter */
408 { "FST", +1, 1, }, /* French Summer */
409 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
410 { "EEST", +2, 1, }, /* Eastern European Daylight */
411 { "WAST", +7, 0, }, /* West Australian Standard */
412 { "WADT", +7, 1, }, /* West Australian Daylight */
413 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
414 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
415 { "EAST", +10, 0, }, /* Eastern Australian Standard */
416 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
417 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
418 { "NZT", +12, 0, }, /* New Zealand */
419 { "NZST", +12, 0, }, /* New Zealand Standard */
420 { "NZDT", +12, 1, }, /* New Zealand Daylight */
421 { "IDLE", +12, 0, }, /* International Date Line East */
424 static int match_string(const char *date, const char *str)
426 int i = 0;
428 for (i = 0; *date; date++, str++, i++) {
429 if (*date == *str)
430 continue;
431 if (toupper(*date) == toupper(*str))
432 continue;
433 if (!isalnum(*date))
434 break;
435 return 0;
437 return i;
440 static int skip_alpha(const char *date)
442 int i = 0;
443 do {
444 i++;
445 } while (isalpha(date[i]));
446 return i;
450 * Parse month, weekday, or timezone name
452 static int match_alpha(const char *date, struct tm *tm, int *offset)
454 int i;
456 for (i = 0; i < 12; i++) {
457 int match = match_string(date, month_names[i]);
458 if (match >= 3) {
459 tm->tm_mon = i;
460 return match;
464 for (i = 0; i < 7; i++) {
465 int match = match_string(date, weekday_names[i]);
466 if (match >= 3) {
467 tm->tm_wday = i;
468 return match;
472 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
473 int match = match_string(date, timezone_names[i].name);
474 if (match >= 3 || match == strlen(timezone_names[i].name)) {
475 int off = timezone_names[i].offset;
477 /* This is bogus, but we like summer */
478 off += timezone_names[i].dst;
480 /* Only use the tz name offset if we don't have anything better */
481 if (*offset == -1)
482 *offset = 60*off;
484 return match;
488 if (match_string(date, "PM") == 2) {
489 tm->tm_hour = (tm->tm_hour % 12) + 12;
490 return 2;
493 if (match_string(date, "AM") == 2) {
494 tm->tm_hour = (tm->tm_hour % 12) + 0;
495 return 2;
498 /* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */
499 if (*date == 'T' && isdigit(date[1]) && tm->tm_hour == -1) {
500 tm->tm_min = tm->tm_sec = 0;
501 return 1;
504 /* BAD CRAP */
505 return skip_alpha(date);
508 static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
510 if (month > 0 && month < 13 && day > 0 && day < 32) {
511 struct tm check = *tm;
512 struct tm *r = (now_tm ? &check : tm);
513 time_t specified;
515 r->tm_mon = month - 1;
516 r->tm_mday = day;
517 if (year == -1) {
518 if (!now_tm)
519 return 1;
520 r->tm_year = now_tm->tm_year;
522 else if (year >= 1970 && year < 2100)
523 r->tm_year = year - 1900;
524 else if (year > 70 && year < 100)
525 r->tm_year = year;
526 else if (year < 38)
527 r->tm_year = year + 100;
528 else
529 return -1;
530 if (!now_tm)
531 return 0;
533 specified = tm_to_time_t(r);
535 /* Be it commit time or author time, it does not make
536 * sense to specify timestamp way into the future. Make
537 * sure it is not later than ten days from now...
539 if ((specified != -1) && (now + 10*24*3600 < specified))
540 return -1;
541 tm->tm_mon = r->tm_mon;
542 tm->tm_mday = r->tm_mday;
543 if (year != -1)
544 tm->tm_year = r->tm_year;
545 return 0;
547 return -1;
550 static int set_time(long hour, long minute, long second, struct tm *tm)
552 /* We accept 61st second because of leap second */
553 if (0 <= hour && hour <= 24 &&
554 0 <= minute && minute < 60 &&
555 0 <= second && second <= 60) {
556 tm->tm_hour = hour;
557 tm->tm_min = minute;
558 tm->tm_sec = second;
559 return 0;
561 return -1;
564 static int is_date_known(struct tm *tm)
566 return tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1;
569 static int match_multi_number(timestamp_t num, char c, const char *date,
570 char *end, struct tm *tm, time_t now)
572 struct tm now_tm;
573 struct tm *refuse_future;
574 long num2, num3;
576 num2 = strtol(end+1, &end, 10);
577 num3 = -1;
578 if (*end == c && isdigit(end[1]))
579 num3 = strtol(end+1, &end, 10);
581 /* Time? Date? */
582 switch (c) {
583 case ':':
584 if (num3 < 0)
585 num3 = 0;
586 if (set_time(num, num2, num3, tm) == 0) {
588 * If %H:%M:%S was just parsed followed by: .<num4>
589 * Consider (& discard) it as fractional second
590 * if %Y%m%d is parsed before.
592 if (*end == '.' && isdigit(end[1]) && is_date_known(tm))
593 strtol(end + 1, &end, 10);
594 break;
596 return 0;
598 case '-':
599 case '/':
600 case '.':
601 if (!now)
602 now = time(NULL);
603 refuse_future = NULL;
604 if (gmtime_r(&now, &now_tm))
605 refuse_future = &now_tm;
607 if (num > 70) {
608 /* yyyy-mm-dd? */
609 if (set_date(num, num2, num3, NULL, now, tm) == 0)
610 break;
611 /* yyyy-dd-mm? */
612 if (set_date(num, num3, num2, NULL, now, tm) == 0)
613 break;
615 /* Our eastern European friends say dd.mm.yy[yy]
616 * is the norm there, so giving precedence to
617 * mm/dd/yy[yy] form only when separator is not '.'
619 if (c != '.' &&
620 set_date(num3, num, num2, refuse_future, now, tm) == 0)
621 break;
622 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
623 if (set_date(num3, num2, num, refuse_future, now, tm) == 0)
624 break;
625 /* Funny European mm.dd.yy */
626 if (c == '.' &&
627 set_date(num3, num, num2, refuse_future, now, tm) == 0)
628 break;
629 return 0;
631 return end - date;
635 * Have we filled in any part of the time/date yet?
636 * We just do a binary 'and' to see if the sign bit
637 * is set in all the values.
639 static inline int nodate(struct tm *tm)
641 return (tm->tm_year &
642 tm->tm_mon &
643 tm->tm_mday &
644 tm->tm_hour &
645 tm->tm_min &
646 tm->tm_sec) < 0;
650 * Have we seen an ISO-8601-alike date, i.e. 20220101T0,
651 * In which, hour is still unset,
652 * and minutes and second has been set to 0.
654 static inline int maybeiso8601(struct tm *tm)
656 return tm->tm_hour == -1 &&
657 tm->tm_min == 0 &&
658 tm->tm_sec == 0;
662 * We've seen a digit. Time? Year? Date?
664 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
666 int n;
667 char *end;
668 timestamp_t num;
670 num = parse_timestamp(date, &end, 10);
673 * Seconds since 1970? We trigger on that for any numbers with
674 * more than 8 digits. This is because we don't want to rule out
675 * numbers like 20070606 as a YYYYMMDD date.
677 if (num >= 100000000 && nodate(tm)) {
678 time_t time = num;
679 if (gmtime_r(&time, tm)) {
680 *tm_gmt = 1;
681 return end - date;
686 * Check for special formats: num[-.:/]num[same]num
688 switch (*end) {
689 case ':':
690 case '.':
691 case '/':
692 case '-':
693 if (isdigit(end[1])) {
694 int match = match_multi_number(num, *end, date, end, tm, 0);
695 if (match)
696 return match;
701 * None of the special formats? Try to guess what
702 * the number meant. We use the number of digits
703 * to make a more educated guess..
705 n = 0;
706 do {
707 n++;
708 } while (isdigit(date[n]));
710 /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
711 /* 6 digits, compact style of ISO-8601's time: HHMMSS */
712 if (n == 8 || n == 6) {
713 unsigned int num1 = num / 10000;
714 unsigned int num2 = (num % 10000) / 100;
715 unsigned int num3 = num % 100;
716 if (n == 8)
717 set_date(num1, num2, num3, NULL, time(NULL), tm);
718 else if (n == 6 && set_time(num1, num2, num3, tm) == 0 &&
719 *end == '.' && isdigit(end[1]))
720 strtoul(end + 1, &end, 10);
721 return end - date;
724 /* reduced precision of ISO-8601's time: HHMM or HH */
725 if (maybeiso8601(tm)) {
726 unsigned int num1 = num;
727 unsigned int num2 = 0;
728 if (n == 4) {
729 num1 = num / 100;
730 num2 = num % 100;
732 if ((n == 4 || n == 2) && !nodate(tm) &&
733 set_time(num1, num2, 0, tm) == 0)
734 return n;
736 * We thought this is an ISO-8601 time string,
737 * we set minutes and seconds to 0,
738 * turn out it isn't, rollback the change.
740 tm->tm_min = tm->tm_sec = -1;
743 /* Four-digit year or a timezone? */
744 if (n == 4) {
745 if (num <= 1400 && *offset == -1) {
746 unsigned int minutes = num % 100;
747 unsigned int hours = num / 100;
748 *offset = hours*60 + minutes;
749 } else if (num > 1900 && num < 2100)
750 tm->tm_year = num - 1900;
751 return n;
755 * Ignore lots of numerals. We took care of 4-digit years above.
756 * Days or months must be one or two digits.
758 if (n > 2)
759 return n;
762 * NOTE! We will give precedence to day-of-month over month or
763 * year numbers in the 1-12 range. So 05 is always "mday 5",
764 * unless we already have a mday..
766 * IOW, 01 Apr 05 parses as "April 1st, 2005".
768 if (num > 0 && num < 32 && tm->tm_mday < 0) {
769 tm->tm_mday = num;
770 return n;
773 /* Two-digit year? */
774 if (n == 2 && tm->tm_year < 0) {
775 if (num < 10 && tm->tm_mday >= 0) {
776 tm->tm_year = num + 100;
777 return n;
779 if (num >= 70) {
780 tm->tm_year = num;
781 return n;
785 if (num > 0 && num < 13 && tm->tm_mon < 0)
786 tm->tm_mon = num-1;
788 return n;
791 static int match_tz(const char *date, int *offp)
793 char *end;
794 int hour = strtoul(date + 1, &end, 10);
795 int n = end - (date + 1);
796 int min = 0;
798 if (n == 4) {
799 /* hhmm */
800 min = hour % 100;
801 hour = hour / 100;
802 } else if (n != 2) {
803 min = 99; /* random crap */
804 } else if (*end == ':') {
805 /* hh:mm? */
806 min = strtoul(end + 1, &end, 10);
807 if (end - (date + 1) != 5)
808 min = 99; /* random crap */
809 } /* otherwise we parsed "hh" */
812 * Don't accept any random crap. Even though some places have
813 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
814 * UTC+14), there is something wrong if hour part is much
815 * larger than that. We might also want to check that the
816 * minutes are divisible by 15 or something too. (Offset of
817 * Kathmandu, Nepal is UTC+5:45)
819 if (min < 60 && hour < 24) {
820 int offset = hour * 60 + min;
821 if (*date == '-')
822 offset = -offset;
823 *offp = offset;
825 return end - date;
828 static void date_string(timestamp_t date, int offset, struct strbuf *buf)
830 int sign = '+';
832 if (offset < 0) {
833 offset = -offset;
834 sign = '-';
836 strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
840 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
841 * only when it appears not as part of any other string.
843 static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
845 char *end;
846 timestamp_t stamp;
847 int ofs;
849 if (*date < '0' || '9' < *date)
850 return -1;
851 stamp = parse_timestamp(date, &end, 10);
852 if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
853 return -1;
854 date = end + 2;
855 ofs = strtol(date, &end, 10);
856 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
857 return -1;
858 ofs = (ofs / 100) * 60 + (ofs % 100);
859 if (date[-1] == '-')
860 ofs = -ofs;
861 *timestamp = stamp;
862 *offset = ofs;
863 return 0;
866 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
867 (i.e. English) day/month names, and it doesn't work correctly with %z. */
868 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
870 struct tm tm;
871 int tm_gmt;
872 timestamp_t dummy_timestamp;
873 int dummy_offset;
875 if (!timestamp)
876 timestamp = &dummy_timestamp;
877 if (!offset)
878 offset = &dummy_offset;
880 memset(&tm, 0, sizeof(tm));
881 tm.tm_year = -1;
882 tm.tm_mon = -1;
883 tm.tm_mday = -1;
884 tm.tm_isdst = -1;
885 tm.tm_hour = -1;
886 tm.tm_min = -1;
887 tm.tm_sec = -1;
888 *offset = -1;
889 tm_gmt = 0;
891 if (*date == '@' &&
892 !match_object_header_date(date + 1, timestamp, offset))
893 return 0; /* success */
894 for (;;) {
895 int match = 0;
896 unsigned char c = *date;
898 /* Stop at end of string or newline */
899 if (!c || c == '\n')
900 break;
902 if (isalpha(c))
903 match = match_alpha(date, &tm, offset);
904 else if (isdigit(c))
905 match = match_digit(date, &tm, offset, &tm_gmt);
906 else if ((c == '-' || c == '+') && isdigit(date[1]))
907 match = match_tz(date, offset);
909 if (!match) {
910 /* BAD CRAP */
911 match = 1;
914 date += match;
917 /* do not use mktime(), which uses local timezone, here */
918 *timestamp = tm_to_time_t(&tm);
919 if (*timestamp == -1)
920 return -1;
922 if (*offset == -1) {
923 time_t temp_time;
925 /* gmtime_r() in match_digit() may have clobbered it */
926 tm.tm_isdst = -1;
927 temp_time = mktime(&tm);
928 if ((time_t)*timestamp > temp_time) {
929 *offset = ((time_t)*timestamp - temp_time) / 60;
930 } else {
931 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
935 if (!tm_gmt)
936 *timestamp -= *offset * 60;
937 return 0; /* success */
940 int parse_expiry_date(const char *date, timestamp_t *timestamp)
942 int errors = 0;
944 if (!strcmp(date, "never") || !strcmp(date, "false"))
945 *timestamp = 0;
946 else if (!strcmp(date, "all") || !strcmp(date, "now"))
948 * We take over "now" here, which usually translates
949 * to the current timestamp. This is because the user
950 * really means to expire everything that was done in
951 * the past, and by definition reflogs are the record
952 * of the past, and there is nothing from the future
953 * to be kept.
955 *timestamp = TIME_MAX;
956 else
957 *timestamp = approxidate_careful(date, &errors);
959 return errors;
962 int parse_date(const char *date, struct strbuf *result)
964 timestamp_t timestamp;
965 int offset;
966 if (parse_date_basic(date, &timestamp, &offset))
967 return -1;
968 date_string(timestamp, offset, result);
969 return 0;
972 static enum date_mode_type parse_date_type(const char *format, const char **end)
974 if (skip_prefix(format, "relative", end))
975 return DATE_RELATIVE;
976 if (skip_prefix(format, "iso8601-strict", end) ||
977 skip_prefix(format, "iso-strict", end))
978 return DATE_ISO8601_STRICT;
979 if (skip_prefix(format, "iso8601", end) ||
980 skip_prefix(format, "iso", end))
981 return DATE_ISO8601;
982 if (skip_prefix(format, "rfc2822", end) ||
983 skip_prefix(format, "rfc", end))
984 return DATE_RFC2822;
985 if (skip_prefix(format, "short", end))
986 return DATE_SHORT;
987 if (skip_prefix(format, "default", end))
988 return DATE_NORMAL;
989 if (skip_prefix(format, "human", end))
990 return DATE_HUMAN;
991 if (skip_prefix(format, "raw", end))
992 return DATE_RAW;
993 if (skip_prefix(format, "unix", end))
994 return DATE_UNIX;
995 if (skip_prefix(format, "format", end))
996 return DATE_STRFTIME;
998 * Please update $__git_log_date_formats in
999 * git-completion.bash when you add new formats.
1002 die("unknown date format %s", format);
1005 void parse_date_format(const char *format, struct date_mode *mode)
1007 const char *p;
1009 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
1010 if (skip_prefix(format, "auto:", &p)) {
1011 if (isatty(1) || pager_in_use())
1012 format = p;
1013 else
1014 format = "default";
1017 /* historical alias */
1018 if (!strcmp(format, "local"))
1019 format = "default-local";
1021 mode->type = parse_date_type(format, &p);
1022 mode->local = 0;
1024 if (skip_prefix(p, "-local", &p))
1025 mode->local = 1;
1027 if (mode->type == DATE_STRFTIME) {
1028 if (!skip_prefix(p, ":", &p))
1029 die("date format missing colon separator: %s", format);
1030 mode->strftime_fmt = xstrdup(p);
1031 } else if (*p)
1032 die("unknown date format %s", format);
1035 void date_mode_release(struct date_mode *mode)
1037 free((char *)mode->strftime_fmt);
1040 void datestamp(struct strbuf *out)
1042 time_t now;
1043 int offset;
1044 struct tm tm = { 0 };
1046 time(&now);
1048 offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
1049 offset /= 60;
1051 date_string(now, offset, out);
1055 * Relative time update (eg "2 days ago"). If we haven't set the time
1056 * yet, we need to set it from current time.
1058 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
1060 time_t n;
1062 if (tm->tm_mday < 0)
1063 tm->tm_mday = now->tm_mday;
1064 if (tm->tm_mon < 0)
1065 tm->tm_mon = now->tm_mon;
1066 if (tm->tm_year < 0) {
1067 tm->tm_year = now->tm_year;
1068 if (tm->tm_mon > now->tm_mon)
1069 tm->tm_year--;
1072 n = mktime(tm) - sec;
1073 localtime_r(&n, tm);
1074 return n;
1078 * Do we have a pending number at the end, or when
1079 * we see a new one? Let's assume it's a month day,
1080 * as in "Dec 6, 1992"
1082 static void pending_number(struct tm *tm, int *num)
1084 int number = *num;
1086 if (number) {
1087 *num = 0;
1088 if (tm->tm_mday < 0 && number < 32)
1089 tm->tm_mday = number;
1090 else if (tm->tm_mon < 0 && number < 13)
1091 tm->tm_mon = number-1;
1092 else if (tm->tm_year < 0) {
1093 if (number > 1969 && number < 2100)
1094 tm->tm_year = number - 1900;
1095 else if (number > 69 && number < 100)
1096 tm->tm_year = number;
1097 else if (number < 38)
1098 tm->tm_year = 100 + number;
1099 /* We screw up for number = 00 ? */
1104 static void date_now(struct tm *tm, struct tm *now, int *num)
1106 *num = 0;
1107 update_tm(tm, now, 0);
1110 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
1112 *num = 0;
1113 update_tm(tm, now, 24*60*60);
1116 static void date_time(struct tm *tm, struct tm *now, int hour)
1118 if (tm->tm_hour < hour)
1119 update_tm(tm, now, 24*60*60);
1120 tm->tm_hour = hour;
1121 tm->tm_min = 0;
1122 tm->tm_sec = 0;
1125 static void date_midnight(struct tm *tm, struct tm *now, int *num)
1127 pending_number(tm, num);
1128 date_time(tm, now, 0);
1131 static void date_noon(struct tm *tm, struct tm *now, int *num)
1133 pending_number(tm, num);
1134 date_time(tm, now, 12);
1137 static void date_tea(struct tm *tm, struct tm *now, int *num)
1139 pending_number(tm, num);
1140 date_time(tm, now, 17);
1143 static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
1145 int hour, n = *num;
1146 *num = 0;
1148 hour = tm->tm_hour;
1149 if (n) {
1150 hour = n;
1151 tm->tm_min = 0;
1152 tm->tm_sec = 0;
1154 tm->tm_hour = (hour % 12) + 12;
1157 static void date_am(struct tm *tm, struct tm *now UNUSED, int *num)
1159 int hour, n = *num;
1160 *num = 0;
1162 hour = tm->tm_hour;
1163 if (n) {
1164 hour = n;
1165 tm->tm_min = 0;
1166 tm->tm_sec = 0;
1168 tm->tm_hour = (hour % 12);
1171 static void date_never(struct tm *tm, struct tm *now UNUSED, int *num)
1173 time_t n = 0;
1174 localtime_r(&n, tm);
1175 *num = 0;
1178 static const struct special {
1179 const char *name;
1180 void (*fn)(struct tm *, struct tm *, int *);
1181 } special[] = {
1182 { "yesterday", date_yesterday },
1183 { "noon", date_noon },
1184 { "midnight", date_midnight },
1185 { "tea", date_tea },
1186 { "PM", date_pm },
1187 { "AM", date_am },
1188 { "never", date_never },
1189 { "now", date_now },
1190 { NULL }
1193 static const char *number_name[] = {
1194 "zero", "one", "two", "three", "four",
1195 "five", "six", "seven", "eight", "nine", "ten",
1198 static const struct typelen {
1199 const char *type;
1200 int length;
1201 } typelen[] = {
1202 { "seconds", 1 },
1203 { "minutes", 60 },
1204 { "hours", 60*60 },
1205 { "days", 24*60*60 },
1206 { "weeks", 7*24*60*60 },
1207 { NULL }
1210 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
1212 const struct typelen *tl;
1213 const struct special *s;
1214 const char *end = date;
1215 int i;
1217 while (isalpha(*++end))
1220 for (i = 0; i < 12; i++) {
1221 int match = match_string(date, month_names[i]);
1222 if (match >= 3) {
1223 tm->tm_mon = i;
1224 *touched = 1;
1225 return end;
1229 for (s = special; s->name; s++) {
1230 int len = strlen(s->name);
1231 if (match_string(date, s->name) == len) {
1232 s->fn(tm, now, num);
1233 *touched = 1;
1234 return end;
1238 if (!*num) {
1239 for (i = 1; i < 11; i++) {
1240 int len = strlen(number_name[i]);
1241 if (match_string(date, number_name[i]) == len) {
1242 *num = i;
1243 *touched = 1;
1244 return end;
1247 if (match_string(date, "last") == 4) {
1248 *num = 1;
1249 *touched = 1;
1251 return end;
1254 tl = typelen;
1255 while (tl->type) {
1256 int len = strlen(tl->type);
1257 if (match_string(date, tl->type) >= len-1) {
1258 update_tm(tm, now, tl->length * *num);
1259 *num = 0;
1260 *touched = 1;
1261 return end;
1263 tl++;
1266 for (i = 0; i < 7; i++) {
1267 int match = match_string(date, weekday_names[i]);
1268 if (match >= 3) {
1269 int diff, n = *num -1;
1270 *num = 0;
1272 diff = tm->tm_wday - i;
1273 if (diff <= 0)
1274 n++;
1275 diff += 7*n;
1277 update_tm(tm, now, diff * 24 * 60 * 60);
1278 *touched = 1;
1279 return end;
1283 if (match_string(date, "months") >= 5) {
1284 int n;
1285 update_tm(tm, now, 0); /* fill in date fields if needed */
1286 n = tm->tm_mon - *num;
1287 *num = 0;
1288 while (n < 0) {
1289 n += 12;
1290 tm->tm_year--;
1292 tm->tm_mon = n;
1293 *touched = 1;
1294 return end;
1297 if (match_string(date, "years") >= 4) {
1298 update_tm(tm, now, 0); /* fill in date fields if needed */
1299 tm->tm_year -= *num;
1300 *num = 0;
1301 *touched = 1;
1302 return end;
1305 return end;
1308 static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1309 time_t now)
1311 char *end;
1312 timestamp_t number = parse_timestamp(date, &end, 10);
1314 switch (*end) {
1315 case ':':
1316 case '.':
1317 case '/':
1318 case '-':
1319 if (isdigit(end[1])) {
1320 int match = match_multi_number(number, *end, date, end,
1321 tm, now);
1322 if (match)
1323 return date + match;
1327 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1328 if (date[0] != '0' || end - date <= 2)
1329 *num = number;
1330 return end;
1333 static timestamp_t approxidate_str(const char *date,
1334 const struct timeval *tv,
1335 int *error_ret)
1337 int number = 0;
1338 int touched = 0;
1339 struct tm tm, now;
1340 time_t time_sec;
1342 time_sec = tv->tv_sec;
1343 localtime_r(&time_sec, &tm);
1344 now = tm;
1346 tm.tm_year = -1;
1347 tm.tm_mon = -1;
1348 tm.tm_mday = -1;
1350 for (;;) {
1351 unsigned char c = *date;
1352 if (!c)
1353 break;
1354 date++;
1355 if (isdigit(c)) {
1356 pending_number(&tm, &number);
1357 date = approxidate_digit(date-1, &tm, &number, time_sec);
1358 touched = 1;
1359 continue;
1361 if (isalpha(c))
1362 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
1364 pending_number(&tm, &number);
1365 if (!touched)
1366 *error_ret = 1;
1367 return (timestamp_t)update_tm(&tm, &now, 0);
1370 timestamp_t approxidate_careful(const char *date, int *error_ret)
1372 struct timeval tv;
1373 timestamp_t timestamp;
1374 int offset;
1375 int dummy = 0;
1376 if (!error_ret)
1377 error_ret = &dummy;
1379 if (!parse_date_basic(date, &timestamp, &offset)) {
1380 *error_ret = 0;
1381 return timestamp;
1384 get_time(&tv);
1385 return approxidate_str(date, &tv, error_ret);
1388 int date_overflows(timestamp_t t)
1390 time_t sys;
1392 /* If we overflowed our timestamp data type, that's bad... */
1393 if ((uintmax_t)t >= TIME_MAX)
1394 return 1;
1397 * ...but we also are going to feed the result to system
1398 * functions that expect time_t, which is often "signed long".
1399 * Make sure that we fit into time_t, as well.
1401 sys = t;
1402 return t != sys || (t < 1) != (sys < 1);