Tested in Visual C++, GNU C++, Emscripten
#include <ctime>
#include <chrono>
#include <iostream>
#include <locale>
#if defined (_WIN32)
#define WINDOWSLIB 1
#elif defined (__APPLE__)//iOS, Mac OS
#define MACOSLIB 1
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__) || defined(__linux) || defined(linux)//_Ubuntu - Fedora - Centos - RedHat
#define LINUXLIB 1
#elif defined (__EMSCRIPTEN__)
#define EMSCRIPTENLIB 1
#endif
#define WriteLine(data)std::cout<< data <<std::endl;
typedef std::string String;
String CurrentISO8601DateTime(bool toUTC=true)
{
using namespace std::chrono;
system_clock::time_point now = system_clock::now();
time_t timet = system_clock::to_time_t(now);
std::tm tm{};
String localeStr = setlocale(LC_ALL, nullptr);
setlocale(LC_ALL, u8"");
String format = String(u8"%FT%T.").append(std::to_string(duration_cast<milliseconds>(now.time_since_epoch()).count() % static_cast<long long>(1000)));
if (toUTC)
{
#ifdef WINDOWSLIB
gmtime_s(&tm, &timet);
#elif LINUXLIB
gmtime_r(&timet, &tm);
#elif EMSCRIPTENLIB
gmtime_r(&timet, &tm);
#endif
format = format.append(u8"Z");
}
else
{
#ifdef WINDOWSLIB
localtime_s(&tm, &timet);
#elif LINUXLIB
localtime_r(&timet, &tm);
#elif EMSCRIPTENLIB
localtime_r(&timet, &tm);
#endif
format.append(u8"%z");
}
String result = String(255, 0);
const size_t length = std::strftime(&result[0], result.size(), format.c_str(), &tm);
result.resize(length);
setlocale(LC_ALL, localeStr.c_str());
return result;
}
#define ConsoleWriteLn(data) std::cout<< data <<std::endl;
int main()
{
ConsoleWriteLn(u8"UTC : " + CurrentISO8601DateTime());
ConsoleWriteLn(u8"LOCAL: " + CurrentISO8601DateTime(false));
}
Results
UTC : 2020-04-12T17:00:18.632Z
LOCAL: 2020-04-12T12:00:18.633-0500
You can deserialize normally with Json.NET
%Y-%m-%dT%H:%M:%SZ
is equivalent to%FT%TZ
for those who still have to cope with a C++03 compiler – Horizontal