Reputation: 635
Whats a good way to check a date format, i want the format to be 2011-12-08 16:59:18 I have boost would using regex be the best way to go or is there some C++ way of doing it. Thanks in advance. here are some test conditions but not limited to these.
So for example someone enters
2011-2-08 16:59:18 //incorrect date month needs to have 2 digits 02
2011-02-08 16:9:18 //incorrect minuets needs to have 2 digits 09
2011-02-0X 16:09:18 //incorect alpha character for day no alpha except - and :
2011-12-08 16:59:18 // correct
Upvotes: 3
Views: 5116
Reputation: 24403
You can use strptime to convert a date from string format to a tm structure.
Also you can use Boost Date Time Input/Output
Upvotes: 2
Reputation: 13852
Regular expressions would work, or you could just walk the string and test each character to be sure it is what you expect it to be. You will probably spend less time doing the latter.
Upvotes: 5