Skip to content

Commit

Permalink
Merge branch 'bug/40'
Browse files Browse the repository at this point in the history
* bug/40:
  fix, check for dates in args[0U] first to decide whether it's a date/time (dt_given_p)
  chore, provide regression test for issue #40
  • Loading branch information
hroptatyr committed Nov 23, 2015
2 parents fc130e9 + 2c172e7 commit d0008f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/dadd.c
Expand Up @@ -218,7 +218,6 @@ main(int argc, char *argv[])
yuck_t argi[1U];
struct dt_dt_s d;
struct __strpdtdur_st_s st = __strpdtdur_st_initialiser();
char *inp;
const char *ofmt;
char **fmt;
size_t nfmt;
Expand Down Expand Up @@ -260,34 +259,37 @@ main(int argc, char *argv[])
dt_set_base(base);
}

/* sanity checks, decide whether we're a mass date adder
* or a mass duration adder, or both, a date and durations are
* present on the command line */
with (const char *inp = argi->args[0U]) {
/* date parsing needed postponing as we need to find out
* about the durations */
if (!dt_unk_p(dt_io_strpdt(inp, fmt, nfmt, NULL))) {
dt_given_p = true;
}
}

/* check first arg, if it's a date the rest of the arguments are
* durations, if not, dates must be read from stdin */
for (size_t i = 0; i < argi->nargs; i++) {
inp = argi->args[i];
for (size_t i = dt_given_p; i < argi->nargs; i++) {
const char *inp = argi->args[i];
do {
if (dt_io_strpdtdur(&st, inp) < 0) {
if (UNLIKELY(i == 0)) {
/* that's ok, must be a date then */
dt_given_p = true;
} else {
serror("Error: \
serror("Error: \
cannot parse duration string `%s'", st.istr);
rc = 1;
goto dur_out;
}
rc = 1;
goto dur_out;
}
} while (__strpdtdur_more_p(&st));
}
/* check if there's only d durations */
hackz = durs_only_d_p(st.durs, st.ndurs) ? NULL : fromz;

/* sanity checks, decide whether we're a mass date adder
* or a mass duration adder, or both, a date and durations are
* present on the command line */
/* read the first argument again in light of a completely parsed
* duration sequence */
if (dt_given_p) {
/* date parsing needed postponing as we need to find out
* about the durations */
inp = argi->args[0U];
const char *inp = argi->args[0U];
if (dt_unk_p(d = dt_io_strpdt(inp, fmt, nfmt, hackz))) {
error("\
Error: cannot interpret date/time string `%s'", inp);
Expand Down
1 change: 1 addition & 0 deletions test/Makefile.am
Expand Up @@ -298,6 +298,7 @@ dt_tests += dadd.085.clit
dt_tests += dadd.086.clit
dt_tests += dadd.087.clit
dt_tests += dadd.088.clit
dt_tests += dadd.089.clit

dt_tests += dtest.001.clit
dt_tests += dtest.002.clit
Expand Down
7 changes: 7 additions & 0 deletions test/dadd.089.clit
@@ -0,0 +1,7 @@
#!/usr/bin/clitoris

## this is don_crissti's issue
## http://unix.stackexchange.com/questions/24626/quickly-calculate-date-differences/36193?noredirect=1#comment420193_36193
$ dadd -i '%m%d%Y' 01012015 +1d
2015-01-02
$

0 comments on commit d0008f9

Please sign in to comment.