Further 'approxidate' improvements
[git/dscho.git] / date.c
blob1de184587ba33e8c677ec8040c4c4e177ef41c55
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 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(unsigned long time, int tz, enum date_mode mode)
91 struct tm *tm;
92 static char timebuf[200];
94 if (mode == DATE_RAW) {
95 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
96 return timebuf;
99 if (mode == DATE_RELATIVE) {
100 unsigned long diff;
101 struct timeval now;
102 gettimeofday(&now, NULL);
103 if (now.tv_sec < time)
104 return "in the future";
105 diff = now.tv_sec - time;
106 if (diff < 90) {
107 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
108 return timebuf;
110 /* Turn it into minutes */
111 diff = (diff + 30) / 60;
112 if (diff < 90) {
113 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
114 return timebuf;
116 /* Turn it into hours */
117 diff = (diff + 30) / 60;
118 if (diff < 36) {
119 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
120 return timebuf;
122 /* We deal with number of days from here on */
123 diff = (diff + 12) / 24;
124 if (diff < 14) {
125 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
126 return timebuf;
128 /* Say weeks for the past 10 weeks or so */
129 if (diff < 70) {
130 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
131 return timebuf;
133 /* Say months for the past 12 months or so */
134 if (diff < 360) {
135 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
136 return timebuf;
138 /* Give years and months for 5 years or so */
139 if (diff < 1825) {
140 unsigned long years = (diff + 183) / 365;
141 unsigned long months = (diff % 365 + 15) / 30;
142 int n;
143 n = snprintf(timebuf, sizeof(timebuf), "%lu year%s",
144 years, (years > 1 ? "s" : ""));
145 if (months)
146 snprintf(timebuf + n, sizeof(timebuf) - n,
147 ", %lu month%s ago",
148 months, (months > 1 ? "s" : ""));
149 else
150 snprintf(timebuf + n, sizeof(timebuf) - n,
151 " ago");
152 return timebuf;
154 /* Otherwise, just years. Centuries is probably overkill. */
155 snprintf(timebuf, sizeof(timebuf), "%lu years ago", (diff + 183) / 365);
156 return timebuf;
159 if (mode == DATE_LOCAL)
160 tz = local_tzoffset(time);
162 tm = time_to_tm(time, tz);
163 if (!tm)
164 return NULL;
165 if (mode == DATE_SHORT)
166 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
167 tm->tm_mon + 1, tm->tm_mday);
168 else if (mode == DATE_ISO8601)
169 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
170 tm->tm_year + 1900,
171 tm->tm_mon + 1,
172 tm->tm_mday,
173 tm->tm_hour, tm->tm_min, tm->tm_sec,
174 tz);
175 else if (mode == DATE_RFC2822)
176 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
177 weekday_names[tm->tm_wday], tm->tm_mday,
178 month_names[tm->tm_mon], tm->tm_year + 1900,
179 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
180 else
181 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
182 weekday_names[tm->tm_wday],
183 month_names[tm->tm_mon],
184 tm->tm_mday,
185 tm->tm_hour, tm->tm_min, tm->tm_sec,
186 tm->tm_year + 1900,
187 (mode == DATE_LOCAL) ? 0 : ' ',
188 tz);
189 return timebuf;
193 * Check these. And note how it doesn't do the summer-time conversion.
195 * In my world, it's always summer, and things are probably a bit off
196 * in other ways too.
198 static const struct {
199 const char *name;
200 int offset;
201 int dst;
202 } timezone_names[] = {
203 { "IDLW", -12, 0, }, /* International Date Line West */
204 { "NT", -11, 0, }, /* Nome */
205 { "CAT", -10, 0, }, /* Central Alaska */
206 { "HST", -10, 0, }, /* Hawaii Standard */
207 { "HDT", -10, 1, }, /* Hawaii Daylight */
208 { "YST", -9, 0, }, /* Yukon Standard */
209 { "YDT", -9, 1, }, /* Yukon Daylight */
210 { "PST", -8, 0, }, /* Pacific Standard */
211 { "PDT", -8, 1, }, /* Pacific Daylight */
212 { "MST", -7, 0, }, /* Mountain Standard */
213 { "MDT", -7, 1, }, /* Mountain Daylight */
214 { "CST", -6, 0, }, /* Central Standard */
215 { "CDT", -6, 1, }, /* Central Daylight */
216 { "EST", -5, 0, }, /* Eastern Standard */
217 { "EDT", -5, 1, }, /* Eastern Daylight */
218 { "AST", -3, 0, }, /* Atlantic Standard */
219 { "ADT", -3, 1, }, /* Atlantic Daylight */
220 { "WAT", -1, 0, }, /* West Africa */
222 { "GMT", 0, 0, }, /* Greenwich Mean */
223 { "UTC", 0, 0, }, /* Universal (Coordinated) */
225 { "WET", 0, 0, }, /* Western European */
226 { "BST", 0, 1, }, /* British Summer */
227 { "CET", +1, 0, }, /* Central European */
228 { "MET", +1, 0, }, /* Middle European */
229 { "MEWT", +1, 0, }, /* Middle European Winter */
230 { "MEST", +1, 1, }, /* Middle European Summer */
231 { "CEST", +1, 1, }, /* Central European Summer */
232 { "MESZ", +1, 1, }, /* Middle European Summer */
233 { "FWT", +1, 0, }, /* French Winter */
234 { "FST", +1, 1, }, /* French Summer */
235 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
236 { "EEST", +2, 1, }, /* Eastern European Daylight */
237 { "WAST", +7, 0, }, /* West Australian Standard */
238 { "WADT", +7, 1, }, /* West Australian Daylight */
239 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
240 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
241 { "EAST", +10, 0, }, /* Eastern Australian Standard */
242 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
243 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
244 { "NZT", +12, 0, }, /* New Zealand */
245 { "NZST", +12, 0, }, /* New Zealand Standard */
246 { "NZDT", +12, 1, }, /* New Zealand Daylight */
247 { "IDLE", +12, 0, }, /* International Date Line East */
250 static int match_string(const char *date, const char *str)
252 int i = 0;
254 for (i = 0; *date; date++, str++, i++) {
255 if (*date == *str)
256 continue;
257 if (toupper(*date) == toupper(*str))
258 continue;
259 if (!isalnum(*date))
260 break;
261 return 0;
263 return i;
266 static int skip_alpha(const char *date)
268 int i = 0;
269 do {
270 i++;
271 } while (isalpha(date[i]));
272 return i;
276 * Parse month, weekday, or timezone name
278 static int match_alpha(const char *date, struct tm *tm, int *offset)
280 int i;
282 for (i = 0; i < 12; i++) {
283 int match = match_string(date, month_names[i]);
284 if (match >= 3) {
285 tm->tm_mon = i;
286 return match;
290 for (i = 0; i < 7; i++) {
291 int match = match_string(date, weekday_names[i]);
292 if (match >= 3) {
293 tm->tm_wday = i;
294 return match;
298 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
299 int match = match_string(date, timezone_names[i].name);
300 if (match >= 3) {
301 int off = timezone_names[i].offset;
303 /* This is bogus, but we like summer */
304 off += timezone_names[i].dst;
306 /* Only use the tz name offset if we don't have anything better */
307 if (*offset == -1)
308 *offset = 60*off;
310 return match;
314 if (match_string(date, "PM") == 2) {
315 tm->tm_hour = (tm->tm_hour % 12) + 12;
316 return 2;
319 if (match_string(date, "AM") == 2) {
320 tm->tm_hour = (tm->tm_hour % 12) + 0;
321 return 2;
324 /* BAD CRAP */
325 return skip_alpha(date);
328 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
330 if (month > 0 && month < 13 && day > 0 && day < 32) {
331 struct tm check = *tm;
332 struct tm *r = (now_tm ? &check : tm);
333 time_t specified;
335 r->tm_mon = month - 1;
336 r->tm_mday = day;
337 if (year == -1) {
338 if (!now_tm)
339 return 1;
340 r->tm_year = now_tm->tm_year;
342 else if (year >= 1970 && year < 2100)
343 r->tm_year = year - 1900;
344 else if (year > 70 && year < 100)
345 r->tm_year = year;
346 else if (year < 38)
347 r->tm_year = year + 100;
348 else
349 return 0;
350 if (!now_tm)
351 return 1;
353 specified = tm_to_time_t(r);
355 /* Be it commit time or author time, it does not make
356 * sense to specify timestamp way into the future. Make
357 * sure it is not later than ten days from now...
359 if (now + 10*24*3600 < specified)
360 return 0;
361 tm->tm_mon = r->tm_mon;
362 tm->tm_mday = r->tm_mday;
363 if (year != -1)
364 tm->tm_year = r->tm_year;
365 return 1;
367 return 0;
370 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
372 time_t now;
373 struct tm now_tm;
374 struct tm *refuse_future;
375 long num2, num3;
377 num2 = strtol(end+1, &end, 10);
378 num3 = -1;
379 if (*end == c && isdigit(end[1]))
380 num3 = strtol(end+1, &end, 10);
382 /* Time? Date? */
383 switch (c) {
384 case ':':
385 if (num3 < 0)
386 num3 = 0;
387 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
388 tm->tm_hour = num;
389 tm->tm_min = num2;
390 tm->tm_sec = num3;
391 break;
393 return 0;
395 case '-':
396 case '/':
397 case '.':
398 now = time(NULL);
399 refuse_future = NULL;
400 if (gmtime_r(&now, &now_tm))
401 refuse_future = &now_tm;
403 if (num > 70) {
404 /* yyyy-mm-dd? */
405 if (is_date(num, num2, num3, refuse_future, now, tm))
406 break;
407 /* yyyy-dd-mm? */
408 if (is_date(num, num3, num2, refuse_future, now, tm))
409 break;
411 /* Our eastern European friends say dd.mm.yy[yy]
412 * is the norm there, so giving precedence to
413 * mm/dd/yy[yy] form only when separator is not '.'
415 if (c != '.' &&
416 is_date(num3, num, num2, refuse_future, now, tm))
417 break;
418 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
419 if (is_date(num3, num2, num, refuse_future, now, tm))
420 break;
421 /* Funny European mm.dd.yy */
422 if (c == '.' &&
423 is_date(num3, num, num2, refuse_future, now, tm))
424 break;
425 return 0;
427 return end - date;
431 * Have we filled in any part of the time/date yet?
432 * We just do a binary 'and' to see if the sign bit
433 * is set in all the values.
435 static inline int nodate(struct tm *tm)
437 return (tm->tm_year &
438 tm->tm_mon &
439 tm->tm_mday &
440 tm->tm_hour &
441 tm->tm_min &
442 tm->tm_sec) < 0;
446 * We've seen a digit. Time? Year? Date?
448 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
450 int n;
451 char *end;
452 unsigned long num;
454 num = strtoul(date, &end, 10);
457 * Seconds since 1970? We trigger on that for any numbers with
458 * more than 8 digits. This is because we don't want to rule out
459 * numbers like 20070606 as a YYYYMMDD date.
461 if (num >= 100000000 && nodate(tm)) {
462 time_t time = num;
463 if (gmtime_r(&time, tm)) {
464 *tm_gmt = 1;
465 return end - date;
470 * Check for special formats: num[-.:/]num[same]num
472 switch (*end) {
473 case ':':
474 case '.':
475 case '/':
476 case '-':
477 if (isdigit(end[1])) {
478 int match = match_multi_number(num, *end, date, end, tm);
479 if (match)
480 return match;
485 * None of the special formats? Try to guess what
486 * the number meant. We use the number of digits
487 * to make a more educated guess..
489 n = 0;
490 do {
491 n++;
492 } while (isdigit(date[n]));
494 /* Four-digit year or a timezone? */
495 if (n == 4) {
496 if (num <= 1400 && *offset == -1) {
497 unsigned int minutes = num % 100;
498 unsigned int hours = num / 100;
499 *offset = hours*60 + minutes;
500 } else if (num > 1900 && num < 2100)
501 tm->tm_year = num - 1900;
502 return n;
506 * Ignore lots of numerals. We took care of 4-digit years above.
507 * Days or months must be one or two digits.
509 if (n > 2)
510 return n;
513 * NOTE! We will give precedence to day-of-month over month or
514 * year numbers in the 1-12 range. So 05 is always "mday 5",
515 * unless we already have a mday..
517 * IOW, 01 Apr 05 parses as "April 1st, 2005".
519 if (num > 0 && num < 32 && tm->tm_mday < 0) {
520 tm->tm_mday = num;
521 return n;
524 /* Two-digit year? */
525 if (n == 2 && tm->tm_year < 0) {
526 if (num < 10 && tm->tm_mday >= 0) {
527 tm->tm_year = num + 100;
528 return n;
530 if (num >= 70) {
531 tm->tm_year = num;
532 return n;
536 if (num > 0 && num < 13 && tm->tm_mon < 0)
537 tm->tm_mon = num-1;
539 return n;
542 static int match_tz(const char *date, int *offp)
544 char *end;
545 int offset = strtoul(date+1, &end, 10);
546 int min, hour;
547 int n = end - date - 1;
549 min = offset % 100;
550 hour = offset / 100;
553 * Don't accept any random crap.. At least 3 digits, and
554 * a valid minute. We might want to check that the minutes
555 * are divisible by 30 or something too.
557 if (min < 60 && n > 2) {
558 offset = hour*60+min;
559 if (*date == '-')
560 offset = -offset;
562 *offp = offset;
564 return end - date;
567 static int date_string(unsigned long date, int offset, char *buf, int len)
569 int sign = '+';
571 if (offset < 0) {
572 offset = -offset;
573 sign = '-';
575 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
578 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
579 (i.e. English) day/month names, and it doesn't work correctly with %z. */
580 int parse_date(const char *date, char *result, int maxlen)
582 struct tm tm;
583 int offset, tm_gmt;
584 time_t then;
586 memset(&tm, 0, sizeof(tm));
587 tm.tm_year = -1;
588 tm.tm_mon = -1;
589 tm.tm_mday = -1;
590 tm.tm_isdst = -1;
591 tm.tm_hour = -1;
592 tm.tm_min = -1;
593 tm.tm_sec = -1;
594 offset = -1;
595 tm_gmt = 0;
597 for (;;) {
598 int match = 0;
599 unsigned char c = *date;
601 /* Stop at end of string or newline */
602 if (!c || c == '\n')
603 break;
605 if (isalpha(c))
606 match = match_alpha(date, &tm, &offset);
607 else if (isdigit(c))
608 match = match_digit(date, &tm, &offset, &tm_gmt);
609 else if ((c == '-' || c == '+') && isdigit(date[1]))
610 match = match_tz(date, &offset);
612 if (!match) {
613 /* BAD CRAP */
614 match = 1;
617 date += match;
620 /* mktime uses local timezone */
621 then = tm_to_time_t(&tm);
622 if (offset == -1)
623 offset = (then - mktime(&tm)) / 60;
625 if (then == -1)
626 return -1;
628 if (!tm_gmt)
629 then -= offset * 60;
630 return date_string(then, offset, result, maxlen);
633 enum date_mode parse_date_format(const char *format)
635 if (!strcmp(format, "relative"))
636 return DATE_RELATIVE;
637 else if (!strcmp(format, "iso8601") ||
638 !strcmp(format, "iso"))
639 return DATE_ISO8601;
640 else if (!strcmp(format, "rfc2822") ||
641 !strcmp(format, "rfc"))
642 return DATE_RFC2822;
643 else if (!strcmp(format, "short"))
644 return DATE_SHORT;
645 else if (!strcmp(format, "local"))
646 return DATE_LOCAL;
647 else if (!strcmp(format, "default"))
648 return DATE_NORMAL;
649 else if (!strcmp(format, "raw"))
650 return DATE_RAW;
651 else
652 die("unknown date format %s", format);
655 void datestamp(char *buf, int bufsize)
657 time_t now;
658 int offset;
660 time(&now);
662 offset = tm_to_time_t(localtime(&now)) - now;
663 offset /= 60;
665 date_string(now, offset, buf, bufsize);
669 * Relative time update (eg "2 days ago"). If we haven't set the time
670 * yet, we need to set it from current time.
672 static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
674 time_t n;
676 if (tm->tm_mday < 0)
677 tm->tm_mday = now->tm_mday;
678 if (tm->tm_mon < 0)
679 tm->tm_mon = now->tm_mon;
680 if (tm->tm_year < 0) {
681 tm->tm_year = now->tm_year;
682 if (tm->tm_mon > now->tm_mon)
683 tm->tm_year--;
686 n = mktime(tm) - sec;
687 localtime_r(&n, tm);
688 return n;
691 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
693 update_tm(tm, now, 24*60*60);
696 static void date_time(struct tm *tm, struct tm *now, int hour)
698 if (tm->tm_hour < hour)
699 date_yesterday(tm, now, NULL);
700 tm->tm_hour = hour;
701 tm->tm_min = 0;
702 tm->tm_sec = 0;
705 static void date_midnight(struct tm *tm, struct tm *now, int *num)
707 date_time(tm, now, 0);
710 static void date_noon(struct tm *tm, struct tm *now, int *num)
712 date_time(tm, now, 12);
715 static void date_tea(struct tm *tm, struct tm *now, int *num)
717 date_time(tm, now, 17);
720 static void date_pm(struct tm *tm, struct tm *now, int *num)
722 int hour, n = *num;
723 *num = 0;
725 hour = tm->tm_hour;
726 if (n) {
727 hour = n;
728 tm->tm_min = 0;
729 tm->tm_sec = 0;
731 tm->tm_hour = (hour % 12) + 12;
734 static void date_am(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);
748 static void date_never(struct tm *tm, struct tm *now, int *num)
750 time_t n = 0;
751 localtime_r(&n, tm);
754 static const struct special {
755 const char *name;
756 void (*fn)(struct tm *, struct tm *, int *);
757 } special[] = {
758 { "yesterday", date_yesterday },
759 { "noon", date_noon },
760 { "midnight", date_midnight },
761 { "tea", date_tea },
762 { "PM", date_pm },
763 { "AM", date_am },
764 { "never", date_never },
765 { NULL }
768 static const char *number_name[] = {
769 "zero", "one", "two", "three", "four",
770 "five", "six", "seven", "eight", "nine", "ten",
773 static const struct typelen {
774 const char *type;
775 int length;
776 } typelen[] = {
777 { "seconds", 1 },
778 { "minutes", 60 },
779 { "hours", 60*60 },
780 { "days", 24*60*60 },
781 { "weeks", 7*24*60*60 },
782 { NULL }
785 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num)
787 const struct typelen *tl;
788 const struct special *s;
789 const char *end = date;
790 int i;
792 while (isalpha(*++end));
795 for (i = 0; i < 12; i++) {
796 int match = match_string(date, month_names[i]);
797 if (match >= 3) {
798 tm->tm_mon = i;
799 return end;
803 for (s = special; s->name; s++) {
804 int len = strlen(s->name);
805 if (match_string(date, s->name) == len) {
806 s->fn(tm, now, num);
807 return end;
811 if (!*num) {
812 for (i = 1; i < 11; i++) {
813 int len = strlen(number_name[i]);
814 if (match_string(date, number_name[i]) == len) {
815 *num = i;
816 return end;
819 if (match_string(date, "last") == 4)
820 *num = 1;
821 return end;
824 tl = typelen;
825 while (tl->type) {
826 int len = strlen(tl->type);
827 if (match_string(date, tl->type) >= len-1) {
828 update_tm(tm, now, tl->length * *num);
829 *num = 0;
830 return end;
832 tl++;
835 for (i = 0; i < 7; i++) {
836 int match = match_string(date, weekday_names[i]);
837 if (match >= 3) {
838 int diff, n = *num -1;
839 *num = 0;
841 diff = tm->tm_wday - i;
842 if (diff <= 0)
843 n++;
844 diff += 7*n;
846 update_tm(tm, now, diff * 24 * 60 * 60);
847 return end;
851 if (match_string(date, "months") >= 5) {
852 int n = tm->tm_mon - *num;
853 *num = 0;
854 while (n < 0) {
855 n += 12;
856 tm->tm_year--;
858 tm->tm_mon = n;
859 return end;
862 if (match_string(date, "years") >= 4) {
863 tm->tm_year -= *num;
864 *num = 0;
865 return end;
868 return end;
871 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
873 char *end;
874 unsigned long number = strtoul(date, &end, 10);
876 switch (*end) {
877 case ':':
878 case '.':
879 case '/':
880 case '-':
881 if (isdigit(end[1])) {
882 int match = match_multi_number(number, *end, date, end, tm);
883 if (match)
884 return date + match;
888 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
889 if (date[0] != '0' || end - date <= 2)
890 *num = number;
891 return end;
895 * Do we have a pending number at the end, or when
896 * we see a new one? Let's assume it's a month day,
897 * as in "Dec 6, 1992"
899 static void pending_number(struct tm *tm, int *num)
901 int number = *num;
903 if (number) {
904 *num = 0;
905 if (tm->tm_mday < 0 && number < 32)
906 tm->tm_mday = number;
907 else if (tm->tm_mon < 0 && number < 13)
908 tm->tm_mon = number-1;
909 else if (tm->tm_year < 0) {
910 if (number > 1969 && number < 2100)
911 tm->tm_year = number - 1900;
912 else if (number > 69 && number < 100)
913 tm->tm_year = number;
914 else if (number < 38)
915 tm->tm_year = 100 + number;
916 /* We screw up for number = 00 ? */
921 unsigned long approxidate(const char *date)
923 int number = 0;
924 struct tm tm, now;
925 struct timeval tv;
926 time_t time_sec;
927 char buffer[50];
929 if (parse_date(date, buffer, sizeof(buffer)) > 0)
930 return strtoul(buffer, NULL, 10);
932 gettimeofday(&tv, NULL);
933 time_sec = tv.tv_sec;
934 localtime_r(&time_sec, &tm);
935 now = tm;
937 tm.tm_year = -1;
938 tm.tm_mon = -1;
939 tm.tm_mday = -1;
941 for (;;) {
942 unsigned char c = *date;
943 if (!c)
944 break;
945 date++;
946 if (isdigit(c)) {
947 pending_number(&tm, &number);
948 date = approxidate_digit(date-1, &tm, &number);
949 continue;
951 if (isalpha(c))
952 date = approxidate_alpha(date-1, &tm, &now, &number);
954 pending_number(&tm, &number);
955 return update_tm(&tm, &now, 0);