I am writing a simple logging class in C++ for learning purposes. My code contains a function that returns a string of today's date. However, I get a compiler error whenever 'localtime' is called.
std::string get_date_string(time_t *time) {
struct tm *now = localtime(time);
std::string date = std::to_string(now->tm_mday) + std::to_string(now->tm_mon) + std::to_string(now->tm_year);
return date;
}
I have tried using #define _CRT_SECURE_NO_WARNINGS
. It didn't work and the same error appeared. I also tried putting _CRT_SECURE_NO_WARNINGS
inside the preprocessor definitions in the project properties. This gave an unresolved external error.
Does anyone have any ideas on what to do?
localtime
can be dangerous to use because it returns a pointer to a memory area which it owns, so if you call it multiple times you need to make sure each time you copy the struct. Also, by the way, the way you create a string, if you get "2112016" you don't know if that's 21/1/2016 or 2/11/2016. – Mcglone