Is one aware of a date parsing function for c. I am looking for something like:
time = parse_time("9/10/2009");
printf("%d\n", time->date);
time2 = parse_time("Monday September 10th 2009")
time2 = parse_time("Monday September 10th 2009 12:30 AM")
Thank you
In Windows, there is VarDateFromStr which can automatically parse many formats if used like this:
It will generally recognize numeric formats, but can also parse "September 10 2009 12:30 AM", without Monday and on my German computer without th, but that might be locale-dependent. The words must be in the local language, for example it will need "June" on English systems but "Juni" on German systems.
If the format is consistent you can use
scanf
family functionsGit has a portable date parsing library, released under GPLv2. You may be able to use that. I think you want
approxidate_careful()
.The notmuch mail project has a GPLv2+ parser for date strings. It supports absolute and relative dates in a variety of user friendly formats, although relative dates only refer to the past. The code is in the parse-time-string subdirectory of the notmuch source tree.
There are two fairly common approaches in C:
Use strptime() with an array of supported formats you accept.
Bang head against table a lot, and then either give up or use another language which has a usable library already (like perl or python).
Unfortunately, the only thing in the standard library is
getdate()
, defined by POSIX, not the C standard. It will handle many time formats, but you need to know the format in advance — not just pass a generic string to the function.It's also not supported on Visual C++, if that's an issue for you. The GNU C runtime supports this routine, however.