X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=date.c;h=3e11500eda5e75a7ec354441fe4a24fd5ebf8bbc;hb=975b31dc6e12fba8f7b067ddbe32230995e05400;hp=73c063b9abf818bfd927577401048e16892759fb;hpb=1e9eb2e93747d2ee3fb44a5a2c8a48e7d19819b9;p=git.git diff --git a/date.c b/date.c index 73c063b9..3e11500e 100644 --- a/date.c +++ b/date.c @@ -34,7 +34,7 @@ static const char *month_names[] = { }; static const char *weekday_names[] = { - "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" + "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" }; /* @@ -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) { @@ -531,6 +575,22 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, int *num) tl++; } + for (i = 0; i < 7; i++) { + int match = match_string(date, weekday_names[i]); + if (match >= 3) { + int diff, n = *num -1; + *num = 0; + + diff = tm->tm_wday - i; + if (diff <= 0) + n++; + diff += 7*n; + + update_tm(tm, diff * 24 * 60 * 60); + return end; + } + } + if (match_string(date, "months") >= 5) { int n = tm->tm_mon - *num; *num = 0;