diff -c/--cc: do not mistake "resolved as deletion" as "use working tree"
[git/mingw.git] / date.c
blob6bae49ca330a69df5e2cf29e1e461f2490d99b2f
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(unsigned long 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;
49 return time + minutes * 60;
53 * The "tz" thing is passed in as this strange "decimal parse of tz"
54 * thing, which means that tz -0100 is passed in as the integer -100,
55 * even though it means "sixty minutes off"
57 static struct tm *time_to_tm(unsigned long time, int tz)
59 time_t t = gm_time_t(time, tz);
60 return gmtime(&t);
64 * What value of "tz" was in effect back then at "time" in the
65 * local timezone?
67 static int local_tzoffset(unsigned long time)
69 time_t t, t_local;
70 struct tm tm;
71 int offset, eastwest;
73 t = time;
74 localtime_r(&t, &tm);
75 t_local = tm_to_time_t(&tm);
77 if (t_local < t) {
78 eastwest = -1;
79 offset = t - t_local;
80 } else {
81 eastwest = 1;
82 offset = t_local - t;
84 offset /= 60; /* in minutes */
85 offset = (offset % 60) + ((offset / 60) * 100);
86 return offset * eastwest;
89 const char *show_date_relative(unsigned long time, int tz,
90 const struct timeval *now,
91 char *timebuf,
92 size_t timebuf_size)
94 unsigned long diff;
95 if (now->tv_sec < time)
96 return "in the future";
97 diff = now->tv_sec - time;
98 if (diff < 90) {
99 snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
100 return timebuf;
102 /* Turn it into minutes */
103 diff = (diff + 30) / 60;
104 if (diff < 90) {
105 snprintf(timebuf, timebuf_size, "%lu minutes ago", diff);
106 return timebuf;
108 /* Turn it into hours */
109 diff = (diff + 30) / 60;
110 if (diff < 36) {
111 snprintf(timebuf, timebuf_size, "%lu hours ago", diff);
112 return timebuf;
114 /* We deal with number of days from here on */
115 diff = (diff + 12) / 24;
116 if (diff < 14) {
117 snprintf(timebuf, timebuf_size, "%lu days ago", diff);
118 return timebuf;
120 /* Say weeks for the past 10 weeks or so */
121 if (diff < 70) {
122 snprintf(timebuf, timebuf_size, "%lu weeks ago", (diff + 3) / 7);
123 return timebuf;
125 /* Say months for the past 12 months or so */
126 if (diff < 365) {
127 snprintf(timebuf, timebuf_size, "%lu months ago", (diff + 15) / 30);
128 return timebuf;
130 /* Give years and months for 5 years or so */
131 if (diff < 1825) {
132 unsigned long years = diff / 365;
133 unsigned long months = (diff % 365 + 15) / 30;
134 int n;
135 n = snprintf(timebuf, timebuf_size, "%lu year%s",
136 years, (years > 1 ? "s" : ""));
137 if (months)
138 snprintf(timebuf + n, timebuf_size - n,
139 ", %lu month%s ago",
140 months, (months > 1 ? "s" : ""));
141 else
142 snprintf(timebuf + n, timebuf_size - n, " ago");
143 return timebuf;
145 /* Otherwise, just years. Centuries is probably overkill. */
146 snprintf(timebuf, timebuf_size, "%lu years ago", (diff + 183) / 365);
147 return timebuf;
150 const char *show_date(unsigned long time, int tz, enum date_mode mode)
152 struct tm *tm;
153 static char timebuf[200];
155 if (mode == DATE_RAW) {
156 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
157 return timebuf;
160 if (mode == DATE_RELATIVE) {
161 struct timeval now;
162 gettimeofday(&now, NULL);
163 return show_date_relative(time, tz, &now,
164 timebuf, sizeof(timebuf));
167 if (mode == DATE_LOCAL)
168 tz = local_tzoffset(time);
170 tm = time_to_tm(time, tz);
171 if (!tm)
172 return NULL;
173 if (mode == DATE_SHORT)
174 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
175 tm->tm_mon + 1, tm->tm_mday);
176 else if (mode == DATE_ISO8601)
177 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
178 tm->tm_year + 1900,
179 tm->tm_mon + 1,
180 tm->tm_mday,
181 tm->tm_hour, tm->tm_min, tm->tm_sec,
182 tz);
183 else if (mode == DATE_RFC2822)
184 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
185 weekday_names[tm->tm_wday], tm->tm_mday,
186 month_names[tm->tm_mon], tm->tm_year + 1900,
187 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
188 else
189 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
190 weekday_names[tm->tm_wday],
191 month_names[tm->tm_mon],
192 tm->tm_mday,
193 tm->tm_hour, tm->tm_min, tm->tm_sec,
194 tm->tm_year + 1900,
195 (mode == DATE_LOCAL) ? 0 : ' ',
196 tz);
197 return timebuf;
201 * Check these. And note how it doesn't do the summer-time conversion.
203 * In my world, it's always summer, and things are probably a bit off
204 * in other ways too.
206 static const struct {
207 const char *name;
208 int offset;
209 int dst;
210 } timezone_names[] = {
211 { "IDLW", -12, 0, }, /* International Date Line West */
212 { "NT", -11, 0, }, /* Nome */
213 { "CAT", -10, 0, }, /* Central Alaska */
214 { "HST", -10, 0, }, /* Hawaii Standard */
215 { "HDT", -10, 1, }, /* Hawaii Daylight */
216 { "YST", -9, 0, }, /* Yukon Standard */
217 { "YDT", -9, 1, }, /* Yukon Daylight */
218 { "PST", -8, 0, }, /* Pacific Standard */
219 { "PDT", -8, 1, }, /* Pacific Daylight */
220 { "MST", -7, 0, }, /* Mountain Standard */
221 { "MDT", -7, 1, }, /* Mountain Daylight */
222 { "CST", -6, 0, }, /* Central Standard */
223 { "CDT", -6, 1, }, /* Central Daylight */
224 { "EST", -5, 0, }, /* Eastern Standard */
225 { "EDT", -5, 1, }, /* Eastern Daylight */
226 { "AST", -3, 0, }, /* Atlantic Standard */
227 { "ADT", -3, 1, }, /* Atlantic Daylight */
228 { "WAT", -1, 0, }, /* West Africa */
230 { "GMT", 0, 0, }, /* Greenwich Mean */
231 { "UTC", 0, 0, }, /* Universal (Coordinated) */
232 { "Z", 0, 0, }, /* Zulu, alias for UTC */
234 { "WET", 0, 0, }, /* Western European */
235 { "BST", 0, 1, }, /* British Summer */
236 { "CET", +1, 0, }, /* Central European */
237 { "MET", +1, 0, }, /* Middle European */
238 { "MEWT", +1, 0, }, /* Middle European Winter */
239 { "MEST", +1, 1, }, /* Middle European Summer */
240 { "CEST", +1, 1, }, /* Central European Summer */
241 { "MESZ", +1, 1, }, /* Middle European Summer */
242 { "FWT", +1, 0, }, /* French Winter */
243 { "FST", +1, 1, }, /* French Summer */
244 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
245 { "EEST", +2, 1, }, /* Eastern European Daylight */
246 { "WAST", +7, 0, }, /* West Australian Standard */
247 { "WADT", +7, 1, }, /* West Australian Daylight */
248 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
249 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
250 { "EAST", +10, 0, }, /* Eastern Australian Standard */
251 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
252 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
253 { "NZT", +12, 0, }, /* New Zealand */
254 { "NZST", +12, 0, }, /* New Zealand Standard */
255 { "NZDT", +12, 1, }, /* New Zealand Daylight */
256 { "IDLE", +12, 0, }, /* International Date Line East */
259 static int match_string(const char *date, const char *str)
261 int i = 0;
263 for (i = 0; *date; date++, str++, i++) {
264 if (*date == *str)
265 continue;
266 if (toupper(*date) == toupper(*str))
267 continue;
268 if (!isalnum(*date))
269 break;
270 return 0;
272 return i;
275 static int skip_alpha(const char *date)
277 int i = 0;
278 do {
279 i++;
280 } while (isalpha(date[i]));
281 return i;
285 * Parse month, weekday, or timezone name
287 static int match_alpha(const char *date, struct tm *tm, int *offset)
289 int i;
291 for (i = 0; i < 12; i++) {
292 int match = match_string(date, month_names[i]);
293 if (match >= 3) {
294 tm->tm_mon = i;
295 return match;
299 for (i = 0; i < 7; i++) {
300 int match = match_string(date, weekday_names[i]);
301 if (match >= 3) {
302 tm->tm_wday = i;
303 return match;
307 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
308 int match = match_string(date, timezone_names[i].name);
309 if (match >= 3 || match == strlen(timezone_names[i].name)) {
310 int off = timezone_names[i].offset;
312 /* This is bogus, but we like summer */
313 off += timezone_names[i].dst;
315 /* Only use the tz name offset if we don't have anything better */
316 if (*offset == -1)
317 *offset = 60*off;
319 return match;
323 if (match_string(date, "PM") == 2) {
324 tm->tm_hour = (tm->tm_hour % 12) + 12;
325 return 2;
328 if (match_string(date, "AM") == 2) {
329 tm->tm_hour = (tm->tm_hour % 12) + 0;
330 return 2;
333 /* BAD CRAP */
334 return skip_alpha(date);
337 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
339 if (month > 0 && month < 13 && day > 0 && day < 32) {
340 struct tm check = *tm;
341 struct tm *r = (now_tm ? &check : tm);
342 time_t specified;
344 r->tm_mon = month - 1;
345 r->tm_mday = day;
346 if (year == -1) {
347 if (!now_tm)
348 return 1;
349 r->tm_year = now_tm->tm_year;
351 else if (year >= 1970 && year < 2100)
352 r->tm_year = year - 1900;
353 else if (year > 70 && year < 100)
354 r->tm_year = year;
355 else if (year < 38)
356 r->tm_year = year + 100;
357 else
358 return 0;
359 if (!now_tm)
360 return 1;
362 specified = tm_to_time_t(r);
364 /* Be it commit time or author time, it does not make
365 * sense to specify timestamp way into the future. Make
366 * sure it is not later than ten days from now...
368 if (now + 10*24*3600 < specified)
369 return 0;
370 tm->tm_mon = r->tm_mon;
371 tm->tm_mday = r->tm_mday;
372 if (year != -1)
373 tm->tm_year = r->tm_year;
374 return 1;
376 return 0;
379 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
381 time_t now;
382 struct tm now_tm;
383 struct tm *refuse_future;
384 long num2, num3;
386 num2 = strtol(end+1, &end, 10);
387 num3 = -1;
388 if (*end == c && isdigit(end[1]))
389 num3 = strtol(end+1, &end, 10);
391 /* Time? Date? */
392 switch (c) {
393 case ':':
394 if (num3 < 0)
395 num3 = 0;
396 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
397 tm->tm_hour = num;
398 tm->tm_min = num2;
399 tm->tm_sec = num3;
400 break;
402 return 0;
404 case '-':
405 case '/':
406 case '.':
407 now = time(NULL);
408 refuse_future = NULL;
409 if (gmtime_r(&now, &now_tm))
410 refuse_future = &now_tm;
412 if (num > 70) {
413 /* yyyy-mm-dd? */
414 if (is_date(num, num2, num3, refuse_future, now, tm))
415 break;
416 /* yyyy-dd-mm? */
417 if (is_date(num, num3, num2, refuse_future, now, tm))
418 break;
420 /* Our eastern European friends say dd.mm.yy[yy]
421 * is the norm there, so giving precedence to
422 * mm/dd/yy[yy] form only when separator is not '.'
424 if (c != '.' &&
425 is_date(num3, num, num2, refuse_future, now, tm))
426 break;
427 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
428 if (is_date(num3, num2, num, refuse_future, now, tm))
429 break;
430 /* Funny European mm.dd.yy */
431 if (c == '.' &&
432 is_date(num3, num, num2, refuse_future, now, tm))
433 break;
434 return 0;
436 return end - date;
440 * Have we filled in any part of the time/date yet?
441 * We just do a binary 'and' to see if the sign bit
442 * is set in all the values.
444 static inline int nodate(struct tm *tm)
446 return (tm->tm_year &
447 tm->tm_mon &
448 tm->tm_mday &
449 tm->tm_hour &
450 tm->tm_min &
451 tm->tm_sec) < 0;
455 * We've seen a digit. Time? Year? Date?
457 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
459 int n;
460 char *end;
461 unsigned long num;
463 num = strtoul(date, &end, 10);
466 * Seconds since 1970? We trigger on that for any numbers with
467 * more than 8 digits. This is because we don't want to rule out
468 * numbers like 20070606 as a YYYYMMDD date.
470 if (num >= 100000000 && nodate(tm)) {
471 time_t time = num;
472 if (gmtime_r(&time, tm)) {
473 *tm_gmt = 1;
474 return end - date;
479 * Check for special formats: num[-.:/]num[same]num
481 switch (*end) {
482 case ':':
483 case '.':
484 case '/':
485 case '-':
486 if (isdigit(end[1])) {
487 int match = match_multi_number(num, *end, date, end, tm);
488 if (match)
489 return match;
494 * None of the special formats? Try to guess what
495 * the number meant. We use the number of digits
496 * to make a more educated guess..
498 n = 0;
499 do {
500 n++;
501 } while (isdigit(date[n]));
503 /* Four-digit year or a timezone? */
504 if (n == 4) {
505 if (num <= 1400 && *offset == -1) {
506 unsigned int minutes = num % 100;
507 unsigned int hours = num / 100;
508 *offset = hours*60 + minutes;
509 } else if (num > 1900 && num < 2100)
510 tm->tm_year = num - 1900;
511 return n;
515 * Ignore lots of numerals. We took care of 4-digit years above.
516 * Days or months must be one or two digits.
518 if (n > 2)
519 return n;
522 * NOTE! We will give precedence to day-of-month over month or
523 * year numbers in the 1-12 range. So 05 is always "mday 5",
524 * unless we already have a mday..
526 * IOW, 01 Apr 05 parses as "April 1st, 2005".
528 if (num > 0 && num < 32 && tm->tm_mday < 0) {
529 tm->tm_mday = num;
530 return n;
533 /* Two-digit year? */
534 if (n == 2 && tm->tm_year < 0) {
535 if (num < 10 && tm->tm_mday >= 0) {
536 tm->tm_year = num + 100;
537 return n;
539 if (num >= 70) {
540 tm->tm_year = num;
541 return n;
545 if (num > 0 && num < 13 && tm->tm_mon < 0)
546 tm->tm_mon = num-1;
548 return n;
551 static int match_tz(const char *date, int *offp)
553 char *end;
554 int offset = strtoul(date+1, &end, 10);
555 int min, hour;
556 int n = end - date - 1;
558 min = offset % 100;
559 hour = offset / 100;
562 * Don't accept any random crap.. At least 3 digits, and
563 * a valid minute. We might want to check that the minutes
564 * are divisible by 30 or something too.
566 if (min < 60 && n > 2) {
567 offset = hour*60+min;
568 if (*date == '-')
569 offset = -offset;
571 *offp = offset;
573 return end - date;
576 static int date_string(unsigned long date, int offset, char *buf, int len)
578 int sign = '+';
580 if (offset < 0) {
581 offset = -offset;
582 sign = '-';
584 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
587 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
588 (i.e. English) day/month names, and it doesn't work correctly with %z. */
589 int parse_date(const char *date, char *result, int maxlen)
591 struct tm tm;
592 int offset, tm_gmt;
593 time_t then;
595 memset(&tm, 0, sizeof(tm));
596 tm.tm_year = -1;
597 tm.tm_mon = -1;
598 tm.tm_mday = -1;
599 tm.tm_isdst = -1;
600 tm.tm_hour = -1;
601 tm.tm_min = -1;
602 tm.tm_sec = -1;
603 offset = -1;
604 tm_gmt = 0;
606 for (;;) {
607 int match = 0;
608 unsigned char c = *date;
610 /* Stop at end of string or newline */
611 if (!c || c == '\n')
612 break;
614 if (isalpha(c))
615 match = match_alpha(date, &tm, &offset);
616 else if (isdigit(c))
617 match = match_digit(date, &tm, &offset, &tm_gmt);
618 else if ((c == '-' || c == '+') && isdigit(date[1]))
619 match = match_tz(date, &offset);
621 if (!match) {
622 /* BAD CRAP */
623 match = 1;
626 date += match;
629 /* mktime uses local timezone */
630 then = tm_to_time_t(&tm);
631 if (offset == -1)
632 offset = (then - mktime(&tm)) / 60;
634 if (then == -1)
635 return -1;
637 if (!tm_gmt)
638 then -= offset * 60;
639 return date_string(then, offset, result, maxlen);
642 enum date_mode parse_date_format(const char *format)
644 if (!strcmp(format, "relative"))
645 return DATE_RELATIVE;
646 else if (!strcmp(format, "iso8601") ||
647 !strcmp(format, "iso"))
648 return DATE_ISO8601;
649 else if (!strcmp(format, "rfc2822") ||
650 !strcmp(format, "rfc"))
651 return DATE_RFC2822;
652 else if (!strcmp(format, "short"))
653 return DATE_SHORT;
654 else if (!strcmp(format, "local"))
655 return DATE_LOCAL;
656 else if (!strcmp(format, "default"))
657 return DATE_NORMAL;
658 else if (!strcmp(format, "raw"))
659 return DATE_RAW;
660 else
661 die("unknown date format %s", format);
664 void datestamp(char *buf, int bufsize)
666 time_t now;
667 int offset;
669 time(&now);
671 offset = tm_to_time_t(localtime(&now)) - now;
672 offset /= 60;
674 date_string(now, offset, buf, bufsize);
678 * Relative time update (eg "2 days ago"). If we haven't set the time
679 * yet, we need to set it from current time.
681 static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
683 time_t n;
685 if (tm->tm_mday < 0)
686 tm->tm_mday = now->tm_mday;
687 if (tm->tm_mon < 0)
688 tm->tm_mon = now->tm_mon;
689 if (tm->tm_year < 0) {
690 tm->tm_year = now->tm_year;
691 if (tm->tm_mon > now->tm_mon)
692 tm->tm_year--;
695 n = mktime(tm) - sec;
696 localtime_r(&n, tm);
697 return n;
700 static void date_now(struct tm *tm, struct tm *now, int *num)
702 update_tm(tm, now, 0);
705 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
707 update_tm(tm, now, 24*60*60);
710 static void date_time(struct tm *tm, struct tm *now, int hour)
712 if (tm->tm_hour < hour)
713 date_yesterday(tm, now, NULL);
714 tm->tm_hour = hour;
715 tm->tm_min = 0;
716 tm->tm_sec = 0;
719 static void date_midnight(struct tm *tm, struct tm *now, int *num)
721 date_time(tm, now, 0);
724 static void date_noon(struct tm *tm, struct tm *now, int *num)
726 date_time(tm, now, 12);
729 static void date_tea(struct tm *tm, struct tm *now, int *num)
731 date_time(tm, now, 17);
734 static void date_pm(struct tm *tm, struct tm *now, int *num)
736 int hour, n = *num;
737 *num = 0;
739 hour = tm->tm_hour;
740 if (n) {
741 hour = n;
742 tm->tm_min = 0;
743 tm->tm_sec = 0;
745 tm->tm_hour = (hour % 12) + 12;
748 static void date_am(struct tm *tm, struct tm *now, int *num)
750 int hour, n = *num;
751 *num = 0;
753 hour = tm->tm_hour;
754 if (n) {
755 hour = n;
756 tm->tm_min = 0;
757 tm->tm_sec = 0;
759 tm->tm_hour = (hour % 12);
762 static void date_never(struct tm *tm, struct tm *now, int *num)
764 time_t n = 0;
765 localtime_r(&n, tm);
768 static const struct special {
769 const char *name;
770 void (*fn)(struct tm *, struct tm *, int *);
771 } special[] = {
772 { "yesterday", date_yesterday },
773 { "noon", date_noon },
774 { "midnight", date_midnight },
775 { "tea", date_tea },
776 { "PM", date_pm },
777 { "AM", date_am },
778 { "never", date_never },
779 { "now", date_now },
780 { NULL }
783 static const char *number_name[] = {
784 "zero", "one", "two", "three", "four",
785 "five", "six", "seven", "eight", "nine", "ten",
788 static const struct typelen {
789 const char *type;
790 int length;
791 } typelen[] = {
792 { "seconds", 1 },
793 { "minutes", 60 },
794 { "hours", 60*60 },
795 { "days", 24*60*60 },
796 { "weeks", 7*24*60*60 },
797 { NULL }
800 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
802 const struct typelen *tl;
803 const struct special *s;
804 const char *end = date;
805 int i;
807 while (isalpha(*++end));
810 for (i = 0; i < 12; i++) {
811 int match = match_string(date, month_names[i]);
812 if (match >= 3) {
813 tm->tm_mon = i;
814 *touched = 1;
815 return end;
819 for (s = special; s->name; s++) {
820 int len = strlen(s->name);
821 if (match_string(date, s->name) == len) {
822 s->fn(tm, now, num);
823 *touched = 1;
824 return end;
828 if (!*num) {
829 for (i = 1; i < 11; i++) {
830 int len = strlen(number_name[i]);
831 if (match_string(date, number_name[i]) == len) {
832 *num = i;
833 *touched = 1;
834 return end;
837 if (match_string(date, "last") == 4) {
838 *num = 1;
839 *touched = 1;
841 return end;
844 tl = typelen;
845 while (tl->type) {
846 int len = strlen(tl->type);
847 if (match_string(date, tl->type) >= len-1) {
848 update_tm(tm, now, tl->length * *num);
849 *num = 0;
850 *touched = 1;
851 return end;
853 tl++;
856 for (i = 0; i < 7; i++) {
857 int match = match_string(date, weekday_names[i]);
858 if (match >= 3) {
859 int diff, n = *num -1;
860 *num = 0;
862 diff = tm->tm_wday - i;
863 if (diff <= 0)
864 n++;
865 diff += 7*n;
867 update_tm(tm, now, diff * 24 * 60 * 60);
868 *touched = 1;
869 return end;
873 if (match_string(date, "months") >= 5) {
874 int n;
875 update_tm(tm, now, 0); /* fill in date fields if needed */
876 n = tm->tm_mon - *num;
877 *num = 0;
878 while (n < 0) {
879 n += 12;
880 tm->tm_year--;
882 tm->tm_mon = n;
883 *touched = 1;
884 return end;
887 if (match_string(date, "years") >= 4) {
888 update_tm(tm, now, 0); /* fill in date fields if needed */
889 tm->tm_year -= *num;
890 *num = 0;
891 *touched = 1;
892 return end;
895 return end;
898 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
900 char *end;
901 unsigned long number = strtoul(date, &end, 10);
903 switch (*end) {
904 case ':':
905 case '.':
906 case '/':
907 case '-':
908 if (isdigit(end[1])) {
909 int match = match_multi_number(number, *end, date, end, tm);
910 if (match)
911 return date + match;
915 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
916 if (date[0] != '0' || end - date <= 2)
917 *num = number;
918 return end;
922 * Do we have a pending number at the end, or when
923 * we see a new one? Let's assume it's a month day,
924 * as in "Dec 6, 1992"
926 static void pending_number(struct tm *tm, int *num)
928 int number = *num;
930 if (number) {
931 *num = 0;
932 if (tm->tm_mday < 0 && number < 32)
933 tm->tm_mday = number;
934 else if (tm->tm_mon < 0 && number < 13)
935 tm->tm_mon = number-1;
936 else if (tm->tm_year < 0) {
937 if (number > 1969 && number < 2100)
938 tm->tm_year = number - 1900;
939 else if (number > 69 && number < 100)
940 tm->tm_year = number;
941 else if (number < 38)
942 tm->tm_year = 100 + number;
943 /* We screw up for number = 00 ? */
948 static unsigned long approxidate_str(const char *date,
949 const struct timeval *tv,
950 int *error_ret)
952 int number = 0;
953 int touched = 0;
954 struct tm tm, now;
955 time_t time_sec;
957 time_sec = tv->tv_sec;
958 localtime_r(&time_sec, &tm);
959 now = tm;
961 tm.tm_year = -1;
962 tm.tm_mon = -1;
963 tm.tm_mday = -1;
965 for (;;) {
966 unsigned char c = *date;
967 if (!c)
968 break;
969 date++;
970 if (isdigit(c)) {
971 pending_number(&tm, &number);
972 date = approxidate_digit(date-1, &tm, &number);
973 touched = 1;
974 continue;
976 if (isalpha(c))
977 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
979 pending_number(&tm, &number);
980 if (!touched)
981 *error_ret = 1;
982 return update_tm(&tm, &now, 0);
985 unsigned long approxidate_relative(const char *date, const struct timeval *tv)
987 char buffer[50];
988 int errors = 0;
990 if (parse_date(date, buffer, sizeof(buffer)) > 0)
991 return strtoul(buffer, NULL, 0);
993 return approxidate_str(date, tv, &errors);
996 unsigned long approxidate_careful(const char *date, int *error_ret)
998 struct timeval tv;
999 char buffer[50];
1000 int dummy = 0;
1001 if (!error_ret)
1002 error_ret = &dummy;
1004 if (parse_date(date, buffer, sizeof(buffer)) > 0) {
1005 *error_ret = 0;
1006 return strtoul(buffer, NULL, 0);
1009 gettimeofday(&tv, NULL);
1010 return approxidate_str(date, &tv, error_ret);