Avatar ·

How to get current date and time in C++? (cross-platform solution)

Hi everyone. I'm a beginner C++ developer, writing a small console application for logging. I need each log entry to start with a timestamp — the current date and time in "YYYY-MM-DD HH:MM:SS" format. The problem is that I work on both Windows (in Visual Studio) and Linux (via SSH). I tried using `` and the `GetLocalTime()` function, but the code doesn't compile on Linux. After googling, I came across `time()` and `localtime()`, but they only output the number of seconds since 1970. I tried using `strftime`, but I get confused with format strings and don't understand how to properly write the result into a `std::string`. Here's what I have so far (works only on Windows): ```cpp #include #include int main() { SYSTEMTIME st;