Parsing datetime string

Can anybody please guide how can i get functionality to parse date time string to time_t and std:tm

On Linux and Mac i was able to carry out the task using strptime(), but the same does not comply for windows. Hence i am looking if there exists an maintained addon there for it, which handles this complexity.

A few projects back I ran into the same problem. Solved it by adding a replacement function which kept the code platform independent.

#include <time.h>
#include <iomanip>
#include <sstream>

extern "C" char* strptime(const char* s, const char* f, struct tm* tm) {
	std::istringstream input(s);
	input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
	input >> std::get_time(tm, f);
	if (input.fail()) {
		return nullptr;
	}
	return (char*)(s + input.tellg());
}

See original code and extra info here on Stackoverflow.

1 Like

Also ofxPoco (included in the core) has options:

https://pocoproject.org/docs/Poco.DateTimeParser.html

1 Like

Above function works fine. Following this path, next thing for me is to get conversion of struct tm to time_t correct. mktime™ seems to be failing at times.

It is failing to parse on visual studio 2017, OF 0.10.0

I did use the strptime function in combination with mktime without any problems. But this was with OF version 0.98 on visual studio 2015. There are no plans to update the app so I’m afraid i cant help you any further.
Maybe try and go with poco as bakercp suggests?

Ultimately i am now using sscanf to parse the limited date formats i required.
(so link that suggested the same https://stackoverflow.com/a/3137634/273548)

So far my experience exploring both ways in brief

  1. strptime as wrapper for the existing getTime function
    -It failed to parsed the datetime format on windows, which had succeeded on Linux/Mac. Might be due pre-initialisation of std::TM structure with current time.

  2. Poco library
    -OF 0.10.0 does not come with POCO lib anymore, they seemed to moved to boost for most of their functionality
    -As my primary means to reference dateTime is time_t, Poco lib required lot of conversions for target conversion string -> time_t or string->TM
    -Conversion for format like “%H:%M” were failing

Above are results i faced from limited exploration i had, it might be possible that i had missed something required.

By the way, 0.10.0 does indeed come with Poco. It was moved to the ofxPoco addon so you just have to include it.