AFAIK, there is no standard function that does this check. However, it's easy to write your own, since you know that
- the char at index 0 must be between
0
and 2
- the char at index 1 can be any digit
- the char at index 2 must be a colon
- the char at index 3 must be between
0
and 5
- the char at index 4 can be any digit
- the char at index 5 must be a
NUL
terminator
The only extra rule that you need to enforce is that if the first char is a 2
, then the second char must be between 0
and 3
.
So the function (which returns 1 if valid, 0 otherwise) looks like this
int validate( char *str )
{
if ( str[0] < '0' || str[0] > '2' )
return 0;
if ( str[1] < '0' || str[1] > '9' )
return 0;
if ( str[2] != ':' )
return 0;
if ( str[3] < '0' || str[3] > '5' )
return 0;
if ( str[4] < '0' || str[4] > '9' )
return 0;
if ( str[5] != '\0' )
return 0;
if ( str[0] == '2' && str[1] > '3' )
return 0;
return 1;
}