From 4350494d3b91a3648e926548e283e62b6fe9b3ae Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Sat, 7 Feb 2015 11:37:15 +0100 Subject: 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 --- src/utils.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src') 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 #include +#include #include #include #include @@ -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) { -- cgit v1.2.3-70-g09d2