aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@calcurse.org>2017-10-26 20:46:46 +0200
committerLukas Fleischer <lfleischer@calcurse.org>2017-10-26 20:49:30 +0200
commit301f240881428e95954b10fc4d61b31720067160 (patch)
tree295cb09c3a9d3f21a5683a19da1f7f7cd0345e87
parent760c297027efca75e095940d76d2078b1da1b677 (diff)
downloadcalcurse-301f240881428e95954b10fc4d61b31720067160.tar.gz
calcurse-301f240881428e95954b10fc4d61b31720067160.zip
Make parsing of date-time more strict
In parse_datetime() it was possible to enter any number of dates and times in any order. Allow just date or time or date followed by time. Time must be looked for before date because of conflicting time and date formats. Suggested-by: Lars Henriksen <LarsHenriksen@get2net.dk> Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
-rw-r--r--src/utils.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/utils.c b/src/utils.c
index 8f27756..706484f 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1126,29 +1126,34 @@ int parse_duration(const char *string, unsigned *duration)
int parse_datetime(const char *string, long *ts)
{
char *t = mem_strdup(string);
- char *p = strtok(t, " ");
+ char *p = strchr(t, ' ');
unsigned int hour, minute;
int year, month, day;
struct date new_date;
int ret = 0;
- while (p) {
- if (parse_date(p, conf.input_datefmt, &year, &month, &day,
- ui_calendar_get_slctd_day())) {
- new_date.dd = day;
- new_date.mm = month;
- new_date.yyyy = year;
- *ts = date2sec(new_date, 0, 0) + get_item_time(*ts);
- ret |= PARSE_DATETIME_HAS_DATE;
- } else if (parse_time(p, &hour, &minute) == 1) {
- *ts = update_time_in_date(*ts, hour, minute);
- ret |= PARSE_DATETIME_HAS_TIME;
- } else {
- return 0;
+ if (p) {
+ *p = '\0';
+ if (parse_date_interactive(t, &year, &month, &day) &&
+ parse_time(p + 1, &hour, &minute)) {
+ ret |= PARSE_DATETIME_HAS_DATE |
+ PARSE_DATETIME_HAS_TIME;
}
- p = strtok(NULL, " ");
+ } else if (parse_date_interactive(t, &year, &month, &day)) {
+ ret |= PARSE_DATETIME_HAS_DATE;
+ } else if (parse_time(t, &hour, &minute)) {
+ ret |= PARSE_DATETIME_HAS_TIME;
+ }
+
+ if (ret & PARSE_DATETIME_HAS_DATE) {
+ new_date.dd = day;
+ new_date.mm = month;
+ new_date.yyyy = year;
+ *ts = date2sec(new_date, 0, 0) + get_item_time(*ts);
}
+ if (ret & PARSE_DATETIME_HAS_TIME)
+ *ts = update_time_in_date(*ts, hour, minute);
mem_free(t);
return ret;