How to check the validity of the dateformat (ex, %d/%m/%Y-%H:%M:%S)?
I am using boost optionsparser to parse the command-line arguments passed by the user. Now the program had an option for the user to specify his/her choice of dateformat.
like,
program -d %d/%m/%Y-%H:%M:%S
, program -d %d/%m/%Y
and so on.
The problem I am facing is, *How do I check the validity of the format string passed by the user? *
The only way I can think of now is passing the format string on to the date class and using the exception handling there.
Howeve开发者_C百科r, if there is another way to check the validity at the time of parsing the options then I wouldn't need to pass around the data and do stuff unncessarily since I do some calculation before actually using the format to generate the date string.
I wouldn't need to pass around the data and do stuff unncessarily since I do some calculation before actually using the format to generate the date string.
Instead of jumping through hoops to calculate something to pass to the date class to validate the format string, why don't you just ask the date class to format today's date for you and see if it generates an exception or not?
If you try to parse it yourself you're just writing code that's duplicating what the date class does, but that also has the chance of missing some detail. You may annoy your user by disallowing something that should be allowed (I've had this happen with applications that tell me my perfectly valid email isn't), and you'll have to handle the exception from the date class anyway in case you don't cover all the bases that it covers when parsing the format
精彩评论