Allow for passing negative arguments to -d

When specifying date ranges using -d, allow for passing negative values
to indicate that the date range should start a certain number of days
ago.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lukas Fleischer 2017-12-13 09:06:32 +01:00
parent f8e6e0d691
commit b82a3b9276

View File

@ -390,7 +390,7 @@ int parse_args(int argc, char **argv)
int status = 0, gc = 0, import = 0, export = 0, daemon = 0; int status = 0, gc = 0, import = 0, export = 0, daemon = 0;
/* Query ranges */ /* Query ranges */
time_t from = -1, to = -1; time_t from = -1, to = -1;
int range = -1; int range = 0;
int limit = INT_MAX; int limit = INT_MAX;
/* Filters */ /* Filters */
struct item_filter filter = { 0, NULL, NULL, -1, -1, -1, -1, 0, 0, 0 }; struct item_filter filter = { 0, NULL, NULL, -1, -1, -1, -1, 0, 0, 0 };
@ -476,8 +476,11 @@ int parse_args(int argc, char **argv)
cfile = optarg; cfile = optarg;
break; break;
case 'd': case 'd':
if (is_all_digit(optarg)) { if (is_all_digit(optarg) ||
(*optarg == '-' && is_all_digit(optarg + 1))) {
range = atoi(optarg); range = atoi(optarg);
EXIT_IF(range == 0, _("invalid range: %s"),
optarg);
} else { } else {
from = parse_datetimearg(optarg); from = parse_datetimearg(optarg);
EXIT_IF(from == -1, _("invalid date: %s"), EXIT_IF(from == -1, _("invalid date: %s"),
@ -704,17 +707,18 @@ int parse_args(int argc, char **argv)
goto cleanup; goto cleanup;
} }
if (from == -1) { EXIT_IF(to >= 0 && range > 0, _("cannot specify a range and an end date"));
struct date day = { 0, 0, 0 }; EXIT_IF(from >= 0 && range < 0, _("cannot specify a negative range and a start date"));
from = get_sec_date(day);
}
if (to == -1 && range == -1) if (from == -1)
from = get_today();
if (to == -1)
to = date_sec_change(from, 0, 1); to = date_sec_change(from, 0, 1);
else if (to == -1 && range >= 0)
if (range > 0)
to = date_sec_change(from, 0, range); to = date_sec_change(from, 0, range);
else if (to >= 0 && range >= 0) else if (range < 0)
EXIT_IF(to >= 0, _("cannot specify a range and an end date")); from = date_sec_change(to, 0, range);
io_init(cfile, datadir); io_init(cfile, datadir);
io_check_dir(path_dir); io_check_dir(path_dir);