summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/calcurse.h2
-rw-r--r--src/utils.c26
2 files changed, 25 insertions, 3 deletions
diff --git a/src/calcurse.h b/src/calcurse.h
index 64c8808..e8313b0 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -974,7 +974,9 @@ char *nowstr(void);
void print_bool_option_incolor(WINDOW *, unsigned, int, int);
const char *get_tempdir(void);
char *new_tempfile(const char *, int);
+int check_date(unsigned, unsigned, unsigned);
int parse_date(const char *, enum datefmt, int *, int *, int *, struct date *);
+int check_time(unsigned, unsigned);
int parse_time(const char *, unsigned *, unsigned *);
int parse_duration(const char *, unsigned *);
void file_close(FILE *, const char *);
diff --git a/src/utils.c b/src/utils.c
index 11b7d60..03cf7d6 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -634,6 +634,17 @@ char *new_tempfile(const char *prefix, int trailing_len)
}
/*
+ * Check if a date is valid.
+ */
+int
+check_date(unsigned year, unsigned month, unsigned day)
+{
+ return (year >= 1902 && year <= 2037 && month >= 1 && month <= 12 &&
+ day >= 1 && day <= days[month - 1] + (month == 2 &&
+ ISLEAP(year)) ? 1 : 0);
+}
+
+/*
* Convert a string containing a date into three integers containing the year,
* month and day.
*
@@ -707,8 +718,7 @@ parse_date(const char *date_string, enum datefmt datefmt, int *year,
}
/* check if date is valid, take leap years into account */
- if (y < 1902 || y > 2037 || m < 1 || m > 12 || d < 1 ||
- d > days[m - 1] + (m == 2 && ISLEAP(y)) ? 1 : 0)
+ if (!check_date(y, m, d))
return 0;
if (year)
@@ -722,6 +732,16 @@ parse_date(const char *date_string, enum datefmt datefmt, int *year,
}
/*
+ * Check if time is valid.
+ */
+int
+check_time(unsigned hours, unsigned minutes)
+{
+ return (hours < DAYINHOURS && minutes < HOURINMIN);
+}
+
+
+/*
* Converts a time string into hours and minutes. Short forms like "23:"
* (23:00) or ":45" (0:45) are allowed.
*
@@ -749,7 +769,7 @@ int parse_time(const char *string, unsigned *hour, unsigned *minute)
}
}
- if (n != 1 || in[0] >= DAYINHOURS || in[1] >= HOURINMIN)
+ if (n != 1 || !check_time(in[0], in[1]))
return 0;
*hour = in[0];