views:

110

answers:

2

Does anyone know of a library that offers something similar to .NET's Parse/TryParse for dates and times that can be used on Linux from C++?

I've looked at the Boost date/time code but I'm not sure that I can do it without specifying the particular input format before attempting to parse. Basically, I might have dates in any number of formats, and want to simply throw it at a function and see if I get something useful out of it before having to deal with edge cases and whatnot.

Thanks for any suggestions.

+1  A: 

glib has date parsing code. It is a C library but widely available on Linux. You could also use the C++ bindings, glibmm.

Here is the date parsing API for glib and glibmm.

anthony
Interesting. I will check it out.
Joe
Same as below, it appears the glib code is only for dates, but I need dates and times.
Joe
+1  A: 

In C++ we use the stream operators for that type of thing:
See http://www.boost.org/doc/libs/1_39_0/doc/html/date_time/date_time_io.html

std::stringstream ss;
ss << "2004-Jan-01";

date d;
ss >> d; // Should throw on bad format.
Martin York
Can the base date type do times as well? I was using local_date_time but it is more limited on the stream operators (it worked when I would use a "well formatted" 2005-Jul-04 22:12:38 but not when I'd try something varied, like 06/26/2007 11:17:04, both of which I'd expect to work.)
Joe