summaryrefslogtreecommitdiff
path: root/date.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@osdl.org>2005-11-18 08:56:40 -0800
committerJunio C Hamano <junkio@cox.net>2005-11-18 11:21:44 -0800
commita8aca418d6484400d6804e22717bd49ca06c28e9 (patch)
treea0d1363191ab699260a54d09c4e4f23fd662729f /date.c
parent583122cd1b37d12d6f9fb487302ae07f1b446f03 (diff)
downloadgit-a8aca418d6484400d6804e22717bd49ca06c28e9.tar.gz
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>
Diffstat (limited to 'date.c')
-rw-r--r--date.c54
1 files changed, 49 insertions, 5 deletions
diff --git a/date.c b/date.c
index d2a67ccf07..3e11500eda 100644
--- a/date.c
+++ b/date.c
@@ -468,12 +468,52 @@ static void update_tm(struct tm *tm, unsigned long sec)
localtime_r(&n, tm);
}
+static void date_yesterday(struct tm *tm, int *num)
+{
+ update_tm(tm, 24*60*60);
+}
+
+static void date_time(struct tm *tm, int hour)
+{
+ if (tm->tm_hour < hour)
+ date_yesterday(tm, NULL);
+ tm->tm_hour = hour;
+ tm->tm_min = 0;
+ tm->tm_sec = 0;
+}
+
+static void date_midnight(struct tm *tm, int *num)
+{
+ date_time(tm, 0);
+}
+
+static void date_noon(struct tm *tm, int *num)
+{
+ date_time(tm, 12);
+}
+
+static void date_tea(struct tm *tm, int *num)
+{
+ date_time(tm, 17);
+}
+
+static const struct special {
+ const char *name;
+ void (*fn)(struct tm *, int *);
+} special[] = {
+ { "yesterday", date_yesterday },
+ { "noon", date_noon },
+ { "midnight", date_midnight },
+ { "tea", date_tea },
+ { NULL }
+};
+
static const char *number_name[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten",
};
-static struct typelen {
+static const struct typelen {
const char *type;
int length;
} typelen[] = {
@@ -487,7 +527,8 @@ static struct typelen {
static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
{
- struct typelen *tl;
+ const struct typelen *tl;
+ const struct special *s;
const char *end = date;
int n = 1, i;
@@ -502,9 +543,12 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
}
}
- if (match_string(date, "yesterday") > 8) {
- update_tm(tm, 24*60*60);
- return end;
+ for (s = special; s->name; s++) {
+ int len = strlen(s->name);
+ if (match_string(date, s->name) == len) {
+ s->fn(tm, num);
+ return end;
+ }
}
if (!*num) {