0

Im trying to get rid of a platform dependent code.

This code gets a string formated by a mask ("%Y-%m-%d %H:%M:%S") and convert it to std::tm struct and a std::time seconds.

I want to get rid of the need of preprocessor dependant blocks, as (imho) there must be a standard way of doing it in both platforms in the same way. strptime does not exists in windows, std::get_time does not exists(?) on linux (g++ c++11).

Is there any way to make this code crossplatform (windows/linux) without using a factory pattern or preprocessor dependant blocks?

KDATETIME& KDATETIME::operator= (const char* k)
{
std::string smask;
smask = (std::string)this->mask;
std::string sk;
sk=k;
tm.tm_isdst=-1;

#ifdef WINDOWS    <<---- THIS BLOCK 

std::istringstream ss(sk);
ss >> std::get_time(&tm, smask.c_str());

#else

strptime(sk.c_str(), smask.c_str(), &tm);

#endif          <<------

this->time = mktime(&tm);  // t is now your desired time_t
[...]
}
diego.martinez
  • 1,051
  • 2
  • 11
  • 25
  • Possible duplicate of [How do I check OS with a preprocessor directive?](https://stackoverflow.com/questions/142508/how-do-i-check-os-with-a-preprocessor-directive) – Jean-Baptiste Yunès Oct 02 '19 at 11:12
  • @Jean-BaptisteYunès thanks but no, the preprocessor directive is already known (is set by the crosscompiler) what I want to do, is to get RID of the need of a preprocessor directive to do something both systems should be able to do in a standard way (I suppose). I'll clarify the question. – diego.martinez Oct 02 '19 at 11:16
  • Maybe `strftime()`? – Jean-Baptiste Yunès Oct 02 '19 at 13:59
  • @Jean-BaptisteYunès thank you, but strftime (format a tm struct into a string) does the opposite to strptime (fill a tm struct with the data from a string). As there is no strptime in windows, I had to use this approach. This is the only difference between windows and linux in this class, so my intention is to avoid having to split it in factory classes. – diego.martinez Oct 02 '19 at 14:31
  • Oh sorry, you may try to look at https://stackoverflow.com/questions/13842369/time-t-to-string-back-and-forth-in-windows – Jean-Baptiste Yunès Oct 02 '19 at 15:05
  • i was looking for a standard way. In my humble experience, doing such a standard function myself leads to suffering. – diego.martinez Oct 04 '19 at 09:01

0 Answers0