summaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2013-02-27 10:45:51 +0100
committerLukas Fleischer <calcurse@cryptocrack.de>2013-02-27 11:36:28 +0100
commit43bdd12254082c58b04e0d3b3032569443e96e4f (patch)
tree1b6b86cbb066ede439a80b01dcefc6aa9e667e22 /src/utils.c
parentba2c5c14f6408872ec34a223d131266ab83e4b7d (diff)
downloadcalcurse-43bdd12254082c58b04e0d3b3032569443e96e4f.zip
parse_{date,time}(): Split out date/time validation
Split date/time validation into separate functions check_date() and check_time(). These will be used to validate date/time information when reading items from the appointments file. Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c26
1 files changed, 23 insertions, 3 deletions
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];