@matheusbsbs / matheus-bagliano / commits / a8aca418d6
Fork 0

Teach "approxidate" about weekday syntax

On Fri, 18 Nov 2005, David Roundy wrote: > > Don't forget "high noon"! (and perhaps "tea time"?) :) Done. [torvalds@g5 git]$ ./test-date "now" "midnight" "high noon" "tea-time" now -> bad -> Wed Dec 31 16:00:00 1969 now -> Fri Nov 18 08:50:54 2005 midnight -> bad -> Wed Dec 31 16:00:00 1969 midnight -> Fri Nov 18 00:00:00 2005 high noon -> bad -> Wed Dec 31 16:00:00 1969 high noon -> Thu Nov 17 12:00:00 2005 tea-time -> bad -> Wed Dec 31 16:00:00 1969 tea-time -> Thu Nov 17 17:00:00 2005 Thanks for pointing out tea-time. This is also written to easily extended to allow people to add their own important dates like Christmas and their own birthdays. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Nov 18, 2005 at 08:56 UTC a8aca418d6484400d6804e22717bd49ca06c28e9
1 file changed +49 -5
date.c
+49 -5
@@ -468,12 +468,52 @@ static void update_tm(struct tm *tm, unsigned long sec)
468 localtime_r(&n, tm);
469 }
470
471 +static void date_yesterday(struct tm *tm, int *num)
472 +{
473 + update_tm(tm, 24*60*60);
474 +}
475 +
476 +static void date_time(struct tm *tm, int hour)
477 +{
478 + if (tm->tm_hour < hour)
479 + date_yesterday(tm, NULL);
480 + tm->tm_hour = hour;
481 + tm->tm_min = 0;
482 + tm->tm_sec = 0;
483 +}
484 +
485 +static void date_midnight(struct tm *tm, int *num)
486 +{
487 + date_time(tm, 0);
488 +}
489 +
490 +static void date_noon(struct tm *tm, int *num)
491 +{
492 + date_time(tm, 12);
493 +}
494 +
495 +static void date_tea(struct tm *tm, int *num)
496 +{
497 + date_time(tm, 17);
498 +}
499 +
500 +static const struct special {
501 + const char *name;
502 + void (*fn)(struct tm *, int *);
503 +} special[] = {
504 + { "yesterday", date_yesterday },
505 + { "noon", date_noon },
506 + { "midnight", date_midnight },
507 + { "tea", date_tea },
508 + { NULL }
509 +};
510 +
511 static const char *number_name[] = {
512 "zero", "one", "two", "three", "four",
513 "five", "six", "seven", "eight", "nine", "ten",
514 };
515
476 -static struct typelen {
516 +static const struct typelen {
517 const char *type;
518 int length;
519 } typelen[] = {
@@ -487,7 +527,8 @@ static struct typelen {
527
528 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
529 {
490 - struct typelen *tl;
530 + const struct typelen *tl;
531 + const struct special *s;
532 const char *end = date;
533 int n = 1, i;
534
@@ -502,9 +543,12 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
543 }
544 }
545
505 - if (match_string(date, "yesterday") > 8) {
506 - update_tm(tm, 24*60*60);
507 - return end;
546 + for (s = special; s->name; s++) {
547 + int len = strlen(s->name);
548 + if (match_string(date, s->name) == len) {
549 + s->fn(tm, num);
550 + return end;
551 + }
552 }
553
554 if (!*num) {