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>
This commit is contained in:
parent
ba2c5c14f6
commit
43bdd12254
@ -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 *);
|
||||
|
26
src/utils.c
26
src/utils.c
@ -633,6 +633,17 @@ char *new_tempfile(const char *prefix, int trailing_len)
|
||||
return mem_strdup(fullname + prefix_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)
|
||||
@ -721,6 +731,16 @@ parse_date(const char *date_string, enum datefmt datefmt, int *year,
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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];
|
||||
|
Loading…
x
Reference in New Issue
Block a user