diff options
author | Lukas Fleischer <calcurse@cryptocrack.de> | 2015-02-07 11:37:15 +0100 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2015-02-07 11:37:15 +0100 |
commit | 4350494d3b91a3648e926548e283e62b6fe9b3ae (patch) | |
tree | b39da1401f7d70b26cdf99d2ac9cfb7f7f9d7966 | |
parent | 283a86c2177f04ad54e072bd7b38a60f5a61e630 (diff) | |
download | calcurse-4350494d3b91a3648e926548e283e62b6fe9b3ae.tar.gz calcurse-4350494d3b91a3648e926548e283e62b6fe9b3ae.zip |
Add a couple of shorthands to parse_date()
We now understand the shorthands "today", "yesterday", "tomorrow" and
"now" which might come in useful sometimes.
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
-rw-r--r-- | src/utils.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index bfcae99..38078aa 100644 --- a/src/utils.c +++ b/src/utils.c @@ -36,6 +36,7 @@ #include <time.h> #include <string.h> +#include <strings.h> #include <stdlib.h> #include <limits.h> #include <unistd.h> @@ -646,6 +647,16 @@ char *new_tempfile(const char *prefix) return fullname; } +static void ymd_from_time_t(int *year, int *month, int *day, time_t t) +{ + struct tm tm; + + localtime_r(&t, &tm); + *day = tm.tm_mday; + *month = tm.tm_mon + 1; + *year = tm.tm_year + 1900; +} + /* * Check if a date is valid. */ @@ -679,6 +690,20 @@ parse_date(const char *date_string, enum datefmt datefmt, int *year, if (!date_string) return 0; + if (!strcasecmp(date_string, "today")) { + ymd_from_time_t(year, month, day, get_today()); + return 1; + } else if (!strcasecmp(date_string, "yesterday")) { + ymd_from_time_t(year, month, day, get_today() - DAYINSEC); + return 1; + } else if (!strcasecmp(date_string, "tomorrow")) { + ymd_from_time_t(year, month, day, get_today() + DAYINSEC); + return 1; + } else if (!strcasecmp(date_string, "now")) { + ymd_from_time_t(year, month, day, now()); + return 1; + } + /* parse string into in[], read up to three integers */ for (p = date_string; *p; p++) { if (*p == sep) { |