add_packed_ref(): teach function to overwrite existing refs
[git.git] / date.c
blob63fa99685e288bd79c75fd6af983f8b628a08fed
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
9 /*
10 * This is like mktime, but without normalization of tm_wday and tm_yday.
12 static time_t tm_to_time_t(const struct tm *tm)
14 static const int mdays[] = {
15 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17 int year = tm->tm_year - 70;
18 int month = tm->tm_mon;
19 int day = tm->tm_mday;
21 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
22 return -1;
23 if (month < 0 || month > 11) /* array bounds */
24 return -1;
25 if (month < 2 || (year + 2) % 4)
26 day--;
27 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
28 return -1;
29 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
30 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
33 static const char *month_names[] = {
34 "January", "February", "March", "April", "May", "June",
35 "July", "August", "September", "October", "November", "December"
38 static const char *weekday_names[] = {
39 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
42 static time_t gm_time_t(timestamp_t time, int tz)
44 int minutes;
46 minutes = tz < 0 ? -tz : tz;
47 minutes = (minutes / 100)*60 + (minutes % 100);
48 minutes = tz < 0 ? -minutes : minutes;
50 if (minutes > 0) {
51 if (unsigned_add_overflows(time, minutes * 60))
52 die("Timestamp+tz too large: %"PRItime" +%04d",
53 time, tz);
54 } else if (time < -minutes * 60)
55 die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);
56 time += minutes * 60;
57 if (date_overflows(time))
58 die("Timestamp too large for this system: %"PRItime, time);
59 return (time_t)time;
63 * The "tz" thing is passed in as this strange "decimal parse of tz"
64 * thing, which means that tz -0100 is passed in as the integer -100,
65 * even though it means "sixty minutes off"
67 static struct tm *time_to_tm(timestamp_t time, int tz)
69 time_t t = gm_time_t(time, tz);
70 return gmtime(&t);
74 * What value of "tz" was in effect back then at "time" in the
75 * local timezone?
77 static int local_tzoffset(timestamp_t time)
79 time_t t, t_local;
80 struct tm tm;
81 int offset, eastwest;
83 if (date_overflows(time))
84 die("Timestamp too large for this system: %"PRItime, time);
86 t = (time_t)time;
87 localtime_r(&t, &tm);
88 t_local = tm_to_time_t(&tm);
90 if (t_local == -1)
91 return 0; /* error; just use +0000 */
92 if (t_local < t) {
93 eastwest = -1;
94 offset = t - t_local;
95 } else {
96 eastwest = 1;
97 offset = t_local - t;
99 offset /= 60; /* in minutes */
100 offset = (offset % 60) + ((offset / 60) * 100);
101 return offset * eastwest;
104 void show_date_relative(timestamp_t time, int tz,
105 const struct timeval *now,
106 struct strbuf *timebuf)
108 timestamp_t diff;
109 if (now->tv_sec < time) {
110 strbuf_addstr(timebuf, _("in the future"));
111 return;
113 diff = now->tv_sec - time;
114 if (diff < 90) {
115 strbuf_addf(timebuf,
116 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
117 return;
119 /* Turn it into minutes */
120 diff = (diff + 30) / 60;
121 if (diff < 90) {
122 strbuf_addf(timebuf,
123 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
124 return;
126 /* Turn it into hours */
127 diff = (diff + 30) / 60;
128 if (diff < 36) {
129 strbuf_addf(timebuf,
130 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
131 return;
133 /* We deal with number of days from here on */
134 diff = (diff + 12) / 24;
135 if (diff < 14) {
136 strbuf_addf(timebuf,
137 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
138 return;
140 /* Say weeks for the past 10 weeks or so */
141 if (diff < 70) {
142 strbuf_addf(timebuf,
143 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
144 (diff + 3) / 7);
145 return;
147 /* Say months for the past 12 months or so */
148 if (diff < 365) {
149 strbuf_addf(timebuf,
150 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
151 (diff + 15) / 30);
152 return;
154 /* Give years and months for 5 years or so */
155 if (diff < 1825) {
156 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
157 timestamp_t years = totalmonths / 12;
158 timestamp_t months = totalmonths % 12;
159 if (months) {
160 struct strbuf sb = STRBUF_INIT;
161 strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
162 strbuf_addf(timebuf,
163 /* TRANSLATORS: "%s" is "<n> years" */
164 Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
165 sb.buf, months);
166 strbuf_release(&sb);
167 } else
168 strbuf_addf(timebuf,
169 Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
170 return;
172 /* Otherwise, just years. Centuries is probably overkill. */
173 strbuf_addf(timebuf,
174 Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
175 (diff + 183) / 365);
178 struct date_mode *date_mode_from_type(enum date_mode_type type)
180 static struct date_mode mode;
181 if (type == DATE_STRFTIME)
182 die("BUG: cannot create anonymous strftime date_mode struct");
183 mode.type = type;
184 mode.local = 0;
185 return &mode;
188 const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
190 struct tm *tm;
191 static struct strbuf timebuf = STRBUF_INIT;
193 if (mode->type == DATE_UNIX) {
194 strbuf_reset(&timebuf);
195 strbuf_addf(&timebuf, "%"PRItime, time);
196 return timebuf.buf;
199 if (mode->local)
200 tz = local_tzoffset(time);
202 if (mode->type == DATE_RAW) {
203 strbuf_reset(&timebuf);
204 strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
205 return timebuf.buf;
208 if (mode->type == DATE_RELATIVE) {
209 struct timeval now;
211 strbuf_reset(&timebuf);
212 gettimeofday(&now, NULL);
213 show_date_relative(time, tz, &now, &timebuf);
214 return timebuf.buf;
217 tm = time_to_tm(time, tz);
218 if (!tm) {
219 tm = time_to_tm(0, 0);
220 tz = 0;
223 strbuf_reset(&timebuf);
224 if (mode->type == DATE_SHORT)
225 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
226 tm->tm_mon + 1, tm->tm_mday);
227 else if (mode->type == DATE_ISO8601)
228 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
229 tm->tm_year + 1900,
230 tm->tm_mon + 1,
231 tm->tm_mday,
232 tm->tm_hour, tm->tm_min, tm->tm_sec,
233 tz);
234 else if (mode->type == DATE_ISO8601_STRICT) {
235 char sign = (tz >= 0) ? '+' : '-';
236 tz = abs(tz);
237 strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
238 tm->tm_year + 1900,
239 tm->tm_mon + 1,
240 tm->tm_mday,
241 tm->tm_hour, tm->tm_min, tm->tm_sec,
242 sign, tz / 100, tz % 100);
243 } else if (mode->type == DATE_RFC2822)
244 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
245 weekday_names[tm->tm_wday], tm->tm_mday,
246 month_names[tm->tm_mon], tm->tm_year + 1900,
247 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
248 else if (mode->type == DATE_STRFTIME)
249 strbuf_addftime(&timebuf, mode->strftime_fmt, tm);
250 else
251 strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
252 weekday_names[tm->tm_wday],
253 month_names[tm->tm_mon],
254 tm->tm_mday,
255 tm->tm_hour, tm->tm_min, tm->tm_sec,
256 tm->tm_year + 1900,
257 mode->local ? 0 : ' ',
258 tz);
259 return timebuf.buf;
263 * Check these. And note how it doesn't do the summer-time conversion.
265 * In my world, it's always summer, and things are probably a bit off
266 * in other ways too.
268 static const struct {
269 const char *name;
270 int offset;
271 int dst;
272 } timezone_names[] = {
273 { "IDLW", -12, 0, }, /* International Date Line West */
274 { "NT", -11, 0, }, /* Nome */
275 { "CAT", -10, 0, }, /* Central Alaska */
276 { "HST", -10, 0, }, /* Hawaii Standard */
277 { "HDT", -10, 1, }, /* Hawaii Daylight */
278 { "YST", -9, 0, }, /* Yukon Standard */
279 { "YDT", -9, 1, }, /* Yukon Daylight */
280 { "PST", -8, 0, }, /* Pacific Standard */
281 { "PDT", -8, 1, }, /* Pacific Daylight */
282 { "MST", -7, 0, }, /* Mountain Standard */
283 { "MDT", -7, 1, }, /* Mountain Daylight */
284 { "CST", -6, 0, }, /* Central Standard */
285 { "CDT", -6, 1, }, /* Central Daylight */
286 { "EST", -5, 0, }, /* Eastern Standard */
287 { "EDT", -5, 1, }, /* Eastern Daylight */
288 { "AST", -3, 0, }, /* Atlantic Standard */
289 { "ADT", -3, 1, }, /* Atlantic Daylight */
290 { "WAT", -1, 0, }, /* West Africa */
292 { "GMT", 0, 0, }, /* Greenwich Mean */
293 { "UTC", 0, 0, }, /* Universal (Coordinated) */
294 { "Z", 0, 0, }, /* Zulu, alias for UTC */
296 { "WET", 0, 0, }, /* Western European */
297 { "BST", 0, 1, }, /* British Summer */
298 { "CET", +1, 0, }, /* Central European */
299 { "MET", +1, 0, }, /* Middle European */
300 { "MEWT", +1, 0, }, /* Middle European Winter */
301 { "MEST", +1, 1, }, /* Middle European Summer */
302 { "CEST", +1, 1, }, /* Central European Summer */
303 { "MESZ", +1, 1, }, /* Middle European Summer */
304 { "FWT", +1, 0, }, /* French Winter */
305 { "FST", +1, 1, }, /* French Summer */
306 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
307 { "EEST", +2, 1, }, /* Eastern European Daylight */
308 { "WAST", +7, 0, }, /* West Australian Standard */
309 { "WADT", +7, 1, }, /* West Australian Daylight */
310 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
311 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
312 { "EAST", +10, 0, }, /* Eastern Australian Standard */
313 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
314 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
315 { "NZT", +12, 0, }, /* New Zealand */
316 { "NZST", +12, 0, }, /* New Zealand Standard */
317 { "NZDT", +12, 1, }, /* New Zealand Daylight */
318 { "IDLE", +12, 0, }, /* International Date Line East */
321 static int match_string(const char *date, const char *str)
323 int i = 0;
325 for (i = 0; *date; date++, str++, i++) {
326 if (*date == *str)
327 continue;
328 if (toupper(*date) == toupper(*str))
329 continue;
330 if (!isalnum(*date))
331 break;
332 return 0;
334 return i;
337 static int skip_alpha(const char *date)
339 int i = 0;
340 do {
341 i++;
342 } while (isalpha(date[i]));
343 return i;
347 * Parse month, weekday, or timezone name
349 static int match_alpha(const char *date, struct tm *tm, int *offset)
351 int i;
353 for (i = 0; i < 12; i++) {
354 int match = match_string(date, month_names[i]);
355 if (match >= 3) {
356 tm->tm_mon = i;
357 return match;
361 for (i = 0; i < 7; i++) {
362 int match = match_string(date, weekday_names[i]);
363 if (match >= 3) {
364 tm->tm_wday = i;
365 return match;
369 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
370 int match = match_string(date, timezone_names[i].name);
371 if (match >= 3 || match == strlen(timezone_names[i].name)) {
372 int off = timezone_names[i].offset;
374 /* This is bogus, but we like summer */
375 off += timezone_names[i].dst;
377 /* Only use the tz name offset if we don't have anything better */
378 if (*offset == -1)
379 *offset = 60*off;
381 return match;
385 if (match_string(date, "PM") == 2) {
386 tm->tm_hour = (tm->tm_hour % 12) + 12;
387 return 2;
390 if (match_string(date, "AM") == 2) {
391 tm->tm_hour = (tm->tm_hour % 12) + 0;
392 return 2;
395 /* BAD CRAP */
396 return skip_alpha(date);
399 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
401 if (month > 0 && month < 13 && day > 0 && day < 32) {
402 struct tm check = *tm;
403 struct tm *r = (now_tm ? &check : tm);
404 time_t specified;
406 r->tm_mon = month - 1;
407 r->tm_mday = day;
408 if (year == -1) {
409 if (!now_tm)
410 return 1;
411 r->tm_year = now_tm->tm_year;
413 else if (year >= 1970 && year < 2100)
414 r->tm_year = year - 1900;
415 else if (year > 70 && year < 100)
416 r->tm_year = year;
417 else if (year < 38)
418 r->tm_year = year + 100;
419 else
420 return 0;
421 if (!now_tm)
422 return 1;
424 specified = tm_to_time_t(r);
426 /* Be it commit time or author time, it does not make
427 * sense to specify timestamp way into the future. Make
428 * sure it is not later than ten days from now...
430 if ((specified != -1) && (now + 10*24*3600 < specified))
431 return 0;
432 tm->tm_mon = r->tm_mon;
433 tm->tm_mday = r->tm_mday;
434 if (year != -1)
435 tm->tm_year = r->tm_year;
436 return 1;
438 return 0;
441 static int match_multi_number(timestamp_t num, char c, const char *date,
442 char *end, struct tm *tm, time_t now)
444 struct tm now_tm;
445 struct tm *refuse_future;
446 long num2, num3;
448 num2 = strtol(end+1, &end, 10);
449 num3 = -1;
450 if (*end == c && isdigit(end[1]))
451 num3 = strtol(end+1, &end, 10);
453 /* Time? Date? */
454 switch (c) {
455 case ':':
456 if (num3 < 0)
457 num3 = 0;
458 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
459 tm->tm_hour = num;
460 tm->tm_min = num2;
461 tm->tm_sec = num3;
462 break;
464 return 0;
466 case '-':
467 case '/':
468 case '.':
469 if (!now)
470 now = time(NULL);
471 refuse_future = NULL;
472 if (gmtime_r(&now, &now_tm))
473 refuse_future = &now_tm;
475 if (num > 70) {
476 /* yyyy-mm-dd? */
477 if (is_date(num, num2, num3, NULL, now, tm))
478 break;
479 /* yyyy-dd-mm? */
480 if (is_date(num, num3, num2, NULL, now, tm))
481 break;
483 /* Our eastern European friends say dd.mm.yy[yy]
484 * is the norm there, so giving precedence to
485 * mm/dd/yy[yy] form only when separator is not '.'
487 if (c != '.' &&
488 is_date(num3, num, num2, refuse_future, now, tm))
489 break;
490 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
491 if (is_date(num3, num2, num, refuse_future, now, tm))
492 break;
493 /* Funny European mm.dd.yy */
494 if (c == '.' &&
495 is_date(num3, num, num2, refuse_future, now, tm))
496 break;
497 return 0;
499 return end - date;
503 * Have we filled in any part of the time/date yet?
504 * We just do a binary 'and' to see if the sign bit
505 * is set in all the values.
507 static inline int nodate(struct tm *tm)
509 return (tm->tm_year &
510 tm->tm_mon &
511 tm->tm_mday &
512 tm->tm_hour &
513 tm->tm_min &
514 tm->tm_sec) < 0;
518 * We've seen a digit. Time? Year? Date?
520 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
522 int n;
523 char *end;
524 timestamp_t num;
526 num = parse_timestamp(date, &end, 10);
529 * Seconds since 1970? We trigger on that for any numbers with
530 * more than 8 digits. This is because we don't want to rule out
531 * numbers like 20070606 as a YYYYMMDD date.
533 if (num >= 100000000 && nodate(tm)) {
534 time_t time = num;
535 if (gmtime_r(&time, tm)) {
536 *tm_gmt = 1;
537 return end - date;
542 * Check for special formats: num[-.:/]num[same]num
544 switch (*end) {
545 case ':':
546 case '.':
547 case '/':
548 case '-':
549 if (isdigit(end[1])) {
550 int match = match_multi_number(num, *end, date, end, tm, 0);
551 if (match)
552 return match;
557 * None of the special formats? Try to guess what
558 * the number meant. We use the number of digits
559 * to make a more educated guess..
561 n = 0;
562 do {
563 n++;
564 } while (isdigit(date[n]));
566 /* Four-digit year or a timezone? */
567 if (n == 4) {
568 if (num <= 1400 && *offset == -1) {
569 unsigned int minutes = num % 100;
570 unsigned int hours = num / 100;
571 *offset = hours*60 + minutes;
572 } else if (num > 1900 && num < 2100)
573 tm->tm_year = num - 1900;
574 return n;
578 * Ignore lots of numerals. We took care of 4-digit years above.
579 * Days or months must be one or two digits.
581 if (n > 2)
582 return n;
585 * NOTE! We will give precedence to day-of-month over month or
586 * year numbers in the 1-12 range. So 05 is always "mday 5",
587 * unless we already have a mday..
589 * IOW, 01 Apr 05 parses as "April 1st, 2005".
591 if (num > 0 && num < 32 && tm->tm_mday < 0) {
592 tm->tm_mday = num;
593 return n;
596 /* Two-digit year? */
597 if (n == 2 && tm->tm_year < 0) {
598 if (num < 10 && tm->tm_mday >= 0) {
599 tm->tm_year = num + 100;
600 return n;
602 if (num >= 70) {
603 tm->tm_year = num;
604 return n;
608 if (num > 0 && num < 13 && tm->tm_mon < 0)
609 tm->tm_mon = num-1;
611 return n;
614 static int match_tz(const char *date, int *offp)
616 char *end;
617 int hour = strtoul(date + 1, &end, 10);
618 int n = end - (date + 1);
619 int min = 0;
621 if (n == 4) {
622 /* hhmm */
623 min = hour % 100;
624 hour = hour / 100;
625 } else if (n != 2) {
626 min = 99; /* random crap */
627 } else if (*end == ':') {
628 /* hh:mm? */
629 min = strtoul(end + 1, &end, 10);
630 if (end - (date + 1) != 5)
631 min = 99; /* random crap */
632 } /* otherwise we parsed "hh" */
635 * Don't accept any random crap. Even though some places have
636 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
637 * UTC+14), there is something wrong if hour part is much
638 * larger than that. We might also want to check that the
639 * minutes are divisible by 15 or something too. (Offset of
640 * Kathmandu, Nepal is UTC+5:45)
642 if (min < 60 && hour < 24) {
643 int offset = hour * 60 + min;
644 if (*date == '-')
645 offset = -offset;
646 *offp = offset;
648 return end - date;
651 static void date_string(timestamp_t date, int offset, struct strbuf *buf)
653 int sign = '+';
655 if (offset < 0) {
656 offset = -offset;
657 sign = '-';
659 strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
663 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
664 * only when it appears not as part of any other string.
666 static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
668 char *end;
669 timestamp_t stamp;
670 int ofs;
672 if (*date < '0' || '9' < *date)
673 return -1;
674 stamp = parse_timestamp(date, &end, 10);
675 if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
676 return -1;
677 date = end + 2;
678 ofs = strtol(date, &end, 10);
679 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
680 return -1;
681 ofs = (ofs / 100) * 60 + (ofs % 100);
682 if (date[-1] == '-')
683 ofs = -ofs;
684 *timestamp = stamp;
685 *offset = ofs;
686 return 0;
689 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
690 (i.e. English) day/month names, and it doesn't work correctly with %z. */
691 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
693 struct tm tm;
694 int tm_gmt;
695 timestamp_t dummy_timestamp;
696 int dummy_offset;
698 if (!timestamp)
699 timestamp = &dummy_timestamp;
700 if (!offset)
701 offset = &dummy_offset;
703 memset(&tm, 0, sizeof(tm));
704 tm.tm_year = -1;
705 tm.tm_mon = -1;
706 tm.tm_mday = -1;
707 tm.tm_isdst = -1;
708 tm.tm_hour = -1;
709 tm.tm_min = -1;
710 tm.tm_sec = -1;
711 *offset = -1;
712 tm_gmt = 0;
714 if (*date == '@' &&
715 !match_object_header_date(date + 1, timestamp, offset))
716 return 0; /* success */
717 for (;;) {
718 int match = 0;
719 unsigned char c = *date;
721 /* Stop at end of string or newline */
722 if (!c || c == '\n')
723 break;
725 if (isalpha(c))
726 match = match_alpha(date, &tm, offset);
727 else if (isdigit(c))
728 match = match_digit(date, &tm, offset, &tm_gmt);
729 else if ((c == '-' || c == '+') && isdigit(date[1]))
730 match = match_tz(date, offset);
732 if (!match) {
733 /* BAD CRAP */
734 match = 1;
737 date += match;
740 /* do not use mktime(), which uses local timezone, here */
741 *timestamp = tm_to_time_t(&tm);
742 if (*timestamp == -1)
743 return -1;
745 if (*offset == -1) {
746 time_t temp_time;
748 /* gmtime_r() in match_digit() may have clobbered it */
749 tm.tm_isdst = -1;
750 temp_time = mktime(&tm);
751 if ((time_t)*timestamp > temp_time) {
752 *offset = ((time_t)*timestamp - temp_time) / 60;
753 } else {
754 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
758 if (!tm_gmt)
759 *timestamp -= *offset * 60;
760 return 0; /* success */
763 int parse_expiry_date(const char *date, timestamp_t *timestamp)
765 int errors = 0;
767 if (!strcmp(date, "never") || !strcmp(date, "false"))
768 *timestamp = 0;
769 else if (!strcmp(date, "all") || !strcmp(date, "now"))
771 * We take over "now" here, which usually translates
772 * to the current timestamp. This is because the user
773 * really means to expire everything she has done in
774 * the past, and by definition reflogs are the record
775 * of the past, and there is nothing from the future
776 * to be kept.
778 *timestamp = TIME_MAX;
779 else
780 *timestamp = approxidate_careful(date, &errors);
782 return errors;
785 int parse_date(const char *date, struct strbuf *result)
787 timestamp_t timestamp;
788 int offset;
789 if (parse_date_basic(date, &timestamp, &offset))
790 return -1;
791 date_string(timestamp, offset, result);
792 return 0;
795 static enum date_mode_type parse_date_type(const char *format, const char **end)
797 if (skip_prefix(format, "relative", end))
798 return DATE_RELATIVE;
799 if (skip_prefix(format, "iso8601-strict", end) ||
800 skip_prefix(format, "iso-strict", end))
801 return DATE_ISO8601_STRICT;
802 if (skip_prefix(format, "iso8601", end) ||
803 skip_prefix(format, "iso", end))
804 return DATE_ISO8601;
805 if (skip_prefix(format, "rfc2822", end) ||
806 skip_prefix(format, "rfc", end))
807 return DATE_RFC2822;
808 if (skip_prefix(format, "short", end))
809 return DATE_SHORT;
810 if (skip_prefix(format, "default", end))
811 return DATE_NORMAL;
812 if (skip_prefix(format, "raw", end))
813 return DATE_RAW;
814 if (skip_prefix(format, "unix", end))
815 return DATE_UNIX;
816 if (skip_prefix(format, "format", end))
817 return DATE_STRFTIME;
819 die("unknown date format %s", format);
822 void parse_date_format(const char *format, struct date_mode *mode)
824 const char *p;
826 /* historical alias */
827 if (!strcmp(format, "local"))
828 format = "default-local";
830 mode->type = parse_date_type(format, &p);
831 mode->local = 0;
833 if (skip_prefix(p, "-local", &p))
834 mode->local = 1;
836 if (mode->type == DATE_STRFTIME) {
837 if (!skip_prefix(p, ":", &p))
838 die("date format missing colon separator: %s", format);
839 mode->strftime_fmt = xstrdup(p);
840 } else if (*p)
841 die("unknown date format %s", format);
844 void datestamp(struct strbuf *out)
846 time_t now;
847 int offset;
849 time(&now);
851 offset = tm_to_time_t(localtime(&now)) - now;
852 offset /= 60;
854 date_string(now, offset, out);
858 * Relative time update (eg "2 days ago"). If we haven't set the time
859 * yet, we need to set it from current time.
861 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
863 time_t n;
865 if (tm->tm_mday < 0)
866 tm->tm_mday = now->tm_mday;
867 if (tm->tm_mon < 0)
868 tm->tm_mon = now->tm_mon;
869 if (tm->tm_year < 0) {
870 tm->tm_year = now->tm_year;
871 if (tm->tm_mon > now->tm_mon)
872 tm->tm_year--;
875 n = mktime(tm) - sec;
876 localtime_r(&n, tm);
877 return n;
880 static void date_now(struct tm *tm, struct tm *now, int *num)
882 update_tm(tm, now, 0);
885 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
887 update_tm(tm, now, 24*60*60);
890 static void date_time(struct tm *tm, struct tm *now, int hour)
892 if (tm->tm_hour < hour)
893 date_yesterday(tm, now, NULL);
894 tm->tm_hour = hour;
895 tm->tm_min = 0;
896 tm->tm_sec = 0;
899 static void date_midnight(struct tm *tm, struct tm *now, int *num)
901 date_time(tm, now, 0);
904 static void date_noon(struct tm *tm, struct tm *now, int *num)
906 date_time(tm, now, 12);
909 static void date_tea(struct tm *tm, struct tm *now, int *num)
911 date_time(tm, now, 17);
914 static void date_pm(struct tm *tm, struct tm *now, int *num)
916 int hour, n = *num;
917 *num = 0;
919 hour = tm->tm_hour;
920 if (n) {
921 hour = n;
922 tm->tm_min = 0;
923 tm->tm_sec = 0;
925 tm->tm_hour = (hour % 12) + 12;
928 static void date_am(struct tm *tm, struct tm *now, int *num)
930 int hour, n = *num;
931 *num = 0;
933 hour = tm->tm_hour;
934 if (n) {
935 hour = n;
936 tm->tm_min = 0;
937 tm->tm_sec = 0;
939 tm->tm_hour = (hour % 12);
942 static void date_never(struct tm *tm, struct tm *now, int *num)
944 time_t n = 0;
945 localtime_r(&n, tm);
948 static const struct special {
949 const char *name;
950 void (*fn)(struct tm *, struct tm *, int *);
951 } special[] = {
952 { "yesterday", date_yesterday },
953 { "noon", date_noon },
954 { "midnight", date_midnight },
955 { "tea", date_tea },
956 { "PM", date_pm },
957 { "AM", date_am },
958 { "never", date_never },
959 { "now", date_now },
960 { NULL }
963 static const char *number_name[] = {
964 "zero", "one", "two", "three", "four",
965 "five", "six", "seven", "eight", "nine", "ten",
968 static const struct typelen {
969 const char *type;
970 int length;
971 } typelen[] = {
972 { "seconds", 1 },
973 { "minutes", 60 },
974 { "hours", 60*60 },
975 { "days", 24*60*60 },
976 { "weeks", 7*24*60*60 },
977 { NULL }
980 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
982 const struct typelen *tl;
983 const struct special *s;
984 const char *end = date;
985 int i;
987 while (isalpha(*++end))
990 for (i = 0; i < 12; i++) {
991 int match = match_string(date, month_names[i]);
992 if (match >= 3) {
993 tm->tm_mon = i;
994 *touched = 1;
995 return end;
999 for (s = special; s->name; s++) {
1000 int len = strlen(s->name);
1001 if (match_string(date, s->name) == len) {
1002 s->fn(tm, now, num);
1003 *touched = 1;
1004 return end;
1008 if (!*num) {
1009 for (i = 1; i < 11; i++) {
1010 int len = strlen(number_name[i]);
1011 if (match_string(date, number_name[i]) == len) {
1012 *num = i;
1013 *touched = 1;
1014 return end;
1017 if (match_string(date, "last") == 4) {
1018 *num = 1;
1019 *touched = 1;
1021 return end;
1024 tl = typelen;
1025 while (tl->type) {
1026 int len = strlen(tl->type);
1027 if (match_string(date, tl->type) >= len-1) {
1028 update_tm(tm, now, tl->length * *num);
1029 *num = 0;
1030 *touched = 1;
1031 return end;
1033 tl++;
1036 for (i = 0; i < 7; i++) {
1037 int match = match_string(date, weekday_names[i]);
1038 if (match >= 3) {
1039 int diff, n = *num -1;
1040 *num = 0;
1042 diff = tm->tm_wday - i;
1043 if (diff <= 0)
1044 n++;
1045 diff += 7*n;
1047 update_tm(tm, now, diff * 24 * 60 * 60);
1048 *touched = 1;
1049 return end;
1053 if (match_string(date, "months") >= 5) {
1054 int n;
1055 update_tm(tm, now, 0); /* fill in date fields if needed */
1056 n = tm->tm_mon - *num;
1057 *num = 0;
1058 while (n < 0) {
1059 n += 12;
1060 tm->tm_year--;
1062 tm->tm_mon = n;
1063 *touched = 1;
1064 return end;
1067 if (match_string(date, "years") >= 4) {
1068 update_tm(tm, now, 0); /* fill in date fields if needed */
1069 tm->tm_year -= *num;
1070 *num = 0;
1071 *touched = 1;
1072 return end;
1075 return end;
1078 static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1079 time_t now)
1081 char *end;
1082 timestamp_t number = parse_timestamp(date, &end, 10);
1084 switch (*end) {
1085 case ':':
1086 case '.':
1087 case '/':
1088 case '-':
1089 if (isdigit(end[1])) {
1090 int match = match_multi_number(number, *end, date, end,
1091 tm, now);
1092 if (match)
1093 return date + match;
1097 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1098 if (date[0] != '0' || end - date <= 2)
1099 *num = number;
1100 return end;
1104 * Do we have a pending number at the end, or when
1105 * we see a new one? Let's assume it's a month day,
1106 * as in "Dec 6, 1992"
1108 static void pending_number(struct tm *tm, int *num)
1110 int number = *num;
1112 if (number) {
1113 *num = 0;
1114 if (tm->tm_mday < 0 && number < 32)
1115 tm->tm_mday = number;
1116 else if (tm->tm_mon < 0 && number < 13)
1117 tm->tm_mon = number-1;
1118 else if (tm->tm_year < 0) {
1119 if (number > 1969 && number < 2100)
1120 tm->tm_year = number - 1900;
1121 else if (number > 69 && number < 100)
1122 tm->tm_year = number;
1123 else if (number < 38)
1124 tm->tm_year = 100 + number;
1125 /* We screw up for number = 00 ? */
1130 static timestamp_t approxidate_str(const char *date,
1131 const struct timeval *tv,
1132 int *error_ret)
1134 int number = 0;
1135 int touched = 0;
1136 struct tm tm, now;
1137 time_t time_sec;
1139 time_sec = tv->tv_sec;
1140 localtime_r(&time_sec, &tm);
1141 now = tm;
1143 tm.tm_year = -1;
1144 tm.tm_mon = -1;
1145 tm.tm_mday = -1;
1147 for (;;) {
1148 unsigned char c = *date;
1149 if (!c)
1150 break;
1151 date++;
1152 if (isdigit(c)) {
1153 pending_number(&tm, &number);
1154 date = approxidate_digit(date-1, &tm, &number, time_sec);
1155 touched = 1;
1156 continue;
1158 if (isalpha(c))
1159 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
1161 pending_number(&tm, &number);
1162 if (!touched)
1163 *error_ret = 1;
1164 return (timestamp_t)update_tm(&tm, &now, 0);
1167 timestamp_t approxidate_relative(const char *date, const struct timeval *tv)
1169 timestamp_t timestamp;
1170 int offset;
1171 int errors = 0;
1173 if (!parse_date_basic(date, &timestamp, &offset))
1174 return timestamp;
1175 return approxidate_str(date, tv, &errors);
1178 timestamp_t approxidate_careful(const char *date, int *error_ret)
1180 struct timeval tv;
1181 timestamp_t timestamp;
1182 int offset;
1183 int dummy = 0;
1184 if (!error_ret)
1185 error_ret = &dummy;
1187 if (!parse_date_basic(date, &timestamp, &offset)) {
1188 *error_ret = 0;
1189 return timestamp;
1192 gettimeofday(&tv, NULL);
1193 return approxidate_str(date, &tv, error_ret);
1196 int date_overflows(timestamp_t t)
1198 time_t sys;
1200 /* If we overflowed our timestamp data type, that's bad... */
1201 if ((uintmax_t)t >= TIME_MAX)
1202 return 1;
1205 * ...but we also are going to feed the result to system
1206 * functions that expect time_t, which is often "signed long".
1207 * Make sure that we fit into time_t, as well.
1209 sys = t;
1210 return t != sys || (t < 1) != (sys < 1);