X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=date.c;h=1c1917b4e8768b0046729e19505d6f1b0845a0d8;hb=4c691724f175573a2dc4118782744cb0e852ab41;hp=d2a67ccf076af7c74f79a23ee83e6b3a173a49b1;hpb=6b7b0427728fe31ee2d9375a36f1c27974432979;p=git.git diff --git a/date.c b/date.c index d2a67ccf..1c1917b4 100644 --- a/date.c +++ b/date.c @@ -123,8 +123,6 @@ static const struct { { "IDLE", +12, 0, }, /* International Date Line East */ }; -#define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0])) - static int match_string(const char *date, const char *str) { int i = 0; @@ -173,7 +171,7 @@ static int match_alpha(const char *date, struct tm *tm, int *offset) } } - for (i = 0; i < NR_TZ; i++) { + for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { int match = match_string(date, timezone_names[i].name); if (match >= 3) { int off = timezone_names[i].offset; @@ -326,7 +324,7 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt /* * NOTE! We will give precedence to day-of-month over month or - * year numebers in the 1-12 range. So 05 is always "mday 5", + * year numbers in the 1-12 range. So 05 is always "mday 5", * unless we already have a mday.. * * IOW, 01 Apr 05 parses as "April 1st, 2005". @@ -468,12 +466,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 +525,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 +541,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) { @@ -596,7 +638,7 @@ unsigned long approxidate(const char *date) } if (number > 0 && number < 32) tm.tm_mday = number; - if (tm.tm_mon > now.tm_mon) + if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year) tm.tm_year--; return mktime(&tm); }