@matheusbsbs / matheus-bagliano / commits / 3c07b1d194
Fork 0

git's rev-parse.c function show_datestring presumes gnu date

Ok. This is the insane patch to do this. It really isn't very careful, and the reason I call it "approxidate()" will become obvious when you look at the code. It is very liberal in what it accepts, to the point where sometimes the results may not make a whole lot of sense. It accepts "last week" as a date string, by virtue of "last" parsing as the number 1, and it totally ignoring superfluous fluff like "ago", so "last week" ends up being exactly the same thing as "1 week ago". Fine so far. It has strange side effects: "last december" will actually parse as "Dec 1", which actually _does_ turn out right, because it will then notice that it's not December yet, so it will decide that you must be talking about a date last year. So it actually gets it right, but it's kind of for the "wrong" reasons. It also accepts the numbers 1..10 in string format ("one" .. "ten"), so you can do "ten weeks ago" or "ten hours ago" and it will do the right thing. But it will do some really strange thigns too: the string "this will last forever", will not recognize anyting but "last", which is recognized as "1", which since it doesn't understand anything else it will think is the day of the month. So if you do gitk --since="this will last forever" the date will actually parse as the first day of the current month. And it will parse the string "now" as "now", but only because it doesn't understand it at all, and it makes everything relative to "now". Similarly, it doesn't actually parse the "ago" or "from now", so "2 weeks ago" is exactly the same as "2 weeks from now". It's the current date minus 14 days. But hey, it's probably better (and certainly faster) than depending on GNU date. So now you can portably do things like gitk --since="two weeks and three days ago" git log --since="July 5" git-whatchanged --since="10 hours ago" git log --since="last october" and it will actually do exactly what you thought it would do (I think). It will count 17 days backwards, and it will do so even if you don't have GNU date installed. (I don't do "last monday" or similar yet, but I can extend it to that too if people want). It was kind of fun trying to write code that uses such totally relaxed "understanding" of dates yet tries to get it right for the trivial cases. The result should be mixed with a few strange preprocessor tricks, and be submitted for the IOCCC ;) Feel free to try it out, and see how many strange dates it gets right. Or wrong. And if you find some interesting (and valid - not "interesting" as in "strange", but "interesting" as in "I'd be interested in actually doing this) thing it gets wrong - usually by not understanding it and silently just doing some strange things - please holler. Now, as usual this certainly hasn't been getting a lot of testing. But my code always works, no? Linus Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Nov 14, 2005 at 19:29 UTC 3c07b1d19491aa9acb9f8e86486f0b80f976edf9
3 files changed +126 -14
cache.h
+1
@@ -259,6 +259,7 @@ extern void *read_object_with_reference(const unsigned char *sha1,
259 const char *show_date(unsigned long time, int timezone);
260 int parse_date(const char *date, char *buf, int bufsize);
261 void datestamp(char *buf, int bufsize);
262 +unsigned long approxidate(const char *);
263
264 extern int setup_ident(void);
265 extern char *get_ident(const char *name, const char *email, const char *date_str);
date.c
+124
@@ -5,6 +5,7 @@
5 */
6
7 #include <time.h>
8 +#include <sys/time.h>
9
10 #include "cache.h"
11
@@ -460,3 +461,126 @@ void datestamp(char *buf, int bufsize)
461
462 date_string(now, offset, buf, bufsize);
463 }
464 +
465 +static void update_tm(struct tm *tm, unsigned long sec)
466 +{
467 + time_t n = mktime(tm) - sec;
468 + localtime_r(&n, tm);
469 +}
470 +
471 +static const char *number_name[] = {
472 + "zero", "one", "two", "three", "four",
473 + "five", "six", "seven", "eight", "nine", "ten",
474 +};
475 +
476 +static struct typelen {
477 + const char *type;
478 + int length;
479 +} typelen[] = {
480 + { "seconds", 1 },
481 + { "minutes", 60 },
482 + { "hours", 60*60 },
483 + { "days", 24*60*60 },
484 + { "weeks", 7*24*60*60 },
485 + { NULL }
486 +};
487 +
488 +static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
489 +{
490 + struct typelen *tl;
491 + const char *end = date;
492 + int n = 1, i;
493 +
494 + while (isalpha(*++end))
495 + n++;
496 +
497 + for (i = 0; i < 12; i++) {
498 + int match = match_string(date, month_names[i]);
499 + if (match >= 3) {
500 + tm->tm_mon = i;
501 + return end;
502 + }
503 + }
504 +
505 + if (match_string(date, "yesterday") > 8) {
506 + update_tm(tm, 24*60*60);
507 + return end;
508 + }
509 +
510 + if (!*num) {
511 + for (i = 1; i < 11; i++) {
512 + int len = strlen(number_name[i]);
513 + if (match_string(date, number_name[i]) == len) {
514 + *num = i;
515 + return end;
516 + }
517 + }
518 + if (match_string(date, "last") == 4)
519 + *num = 1;
520 + return end;
521 + }
522 +
523 + tl = typelen;
524 + while (tl->type) {
525 + int len = strlen(tl->type);
526 + if (match_string(date, tl->type) >= len-1) {
527 + update_tm(tm, tl->length * *num);
528 + *num = 0;
529 + return end;
530 + }
531 + tl++;
532 + }
533 +
534 + if (match_string(date, "months") >= 5) {
535 + int n = tm->tm_mon - *num;
536 + *num = 0;
537 + while (n < 0) {
538 + n += 12;
539 + tm->tm_year--;
540 + }
541 + tm->tm_mon = n;
542 + return end;
543 + }
544 +
545 + if (match_string(date, "years") >= 4) {
546 + tm->tm_year -= *num;
547 + *num = 0;
548 + return end;
549 + }
550 +
551 + return end;
552 +}
553 +
554 +unsigned long approxidate(const char *date)
555 +{
556 + int number = 0;
557 + struct tm tm, now;
558 + struct timeval tv;
559 + char buffer[50];
560 +
561 + if (parse_date(date, buffer, sizeof(buffer)) > 0)
562 + return strtoul(buffer, NULL, 10);
563 +
564 + gettimeofday(&tv, NULL);
565 + localtime_r(&tv.tv_sec, &tm);
566 + now = tm;
567 + for (;;) {
568 + unsigned char c = *date;
569 + if (!c)
570 + break;
571 + date++;
572 + if (isdigit(c)) {
573 + char *end;
574 + number = strtoul(date-1, &end, 10);
575 + date = end;
576 + continue;
577 + }
578 + if (isalpha(c))
579 + date = approxidate_alpha(date-1, &tm, &number);
580 + }
581 + if (number > 0 && number < 32)
582 + tm.tm_mday = number;
583 + if (tm.tm_mon > now.tm_mon)
584 + tm.tm_year--;
585 + return mktime(&tm);
586 +}
rev-parse.c
+1 -14
@@ -131,25 +131,12 @@ static int show_reference(const char *refname, const unsigned char *sha1)
131
132 static void show_datestring(const char *flag, const char *datestr)
133 {
134 - FILE *date;
134 static char buffer[100];
136 - static char cmd[1000];
137 - int len;
135
136 /* date handling requires both flags and revs */
137 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
138 return;
142 - len = strlen(flag);
143 - memcpy(buffer, flag, len);
144 -
145 - snprintf(cmd, sizeof(cmd), "date --date=%s +%%s", sq_quote(datestr));
146 - date = popen(cmd, "r");
147 - if (!date || !fgets(buffer + len, sizeof(buffer) - len, date))
148 - die("git-rev-list: bad date string");
149 - pclose(date);
150 - len = strlen(buffer);
151 - if (buffer[len-1] == '\n')
152 - buffer[--len] = 0;
139 + snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
140 show(buffer);
141 }
142