in Include/json_cpp/details/asyncrt_utils.hpp [530:657]
utility::string_t datetime::to_string(date_format format) const
{
#ifdef _WIN32
int status;
ULARGE_INTEGER largeInt;
largeInt.QuadPart = m_interval;
FILETIME ft;
ft.dwHighDateTime = largeInt.HighPart;
ft.dwLowDateTime = largeInt.LowPart;
SYSTEMTIME systemTime;
if (!FileTimeToSystemTime((const FILETIME *)&ft, &systemTime))
{
throw utility::details::create_system_error(GetLastError());
}
std::wostringstream outStream;
outStream.imbue(std::locale::classic());
if (format == RFC_1123)
{
#if _WIN32_WINNT < _WIN32_WINNT_VISTA
TCHAR dateStr[18] = {0};
status = GetDateFormat(LOCALE_INVARIANT, 0, &systemTime, __TEXT("ddd',' dd MMM yyyy"), dateStr, sizeof(dateStr) / sizeof(TCHAR));
#else
wchar_t dateStr[18] = {0};
status = GetDateFormatEx(LOCALE_NAME_INVARIANT, 0, &systemTime, L"ddd',' dd MMM yyyy", dateStr, sizeof(dateStr) / sizeof(wchar_t), NULL);
#endif // _WIN32_WINNT < _WIN32_WINNT_VISTA
if (status == 0)
{
throw utility::details::create_system_error(GetLastError());
}
#if _WIN32_WINNT < _WIN32_WINNT_VISTA
TCHAR timeStr[10] = {0};
status = GetTimeFormat(LOCALE_INVARIANT, TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT, &systemTime, __TEXT("HH':'mm':'ss"), timeStr, sizeof(timeStr) / sizeof(TCHAR));
#else
wchar_t timeStr[10] = {0};
status = GetTimeFormatEx(LOCALE_NAME_INVARIANT, TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT, &systemTime, L"HH':'mm':'ss", timeStr, sizeof(timeStr) / sizeof(wchar_t));
#endif // _WIN32_WINNT < _WIN32_WINNT_VISTA
if (status == 0)
{
throw utility::details::create_system_error(GetLastError());
}
outStream << dateStr << " " << timeStr << " " << "GMT";
}
else if (format == ISO_8601)
{
const size_t buffSize = 64;
#if _WIN32_WINNT < _WIN32_WINNT_VISTA
TCHAR dateStr[buffSize] = {0};
status = GetDateFormat(LOCALE_INVARIANT, 0, &systemTime, __TEXT("yyyy-MM-dd"), dateStr, buffSize);
#else
wchar_t dateStr[buffSize] = {0};
status = GetDateFormatEx(LOCALE_NAME_INVARIANT, 0, &systemTime, L"yyyy-MM-dd", dateStr, buffSize, NULL);
#endif // _WIN32_WINNT < _WIN32_WINNT_VISTA
if (status == 0)
{
throw utility::details::create_system_error(GetLastError());
}
#if _WIN32_WINNT < _WIN32_WINNT_VISTA
TCHAR timeStr[buffSize] = {0};
status = GetTimeFormat(LOCALE_INVARIANT, TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT, &systemTime, __TEXT("HH':'mm':'ss"), timeStr, buffSize);
#else
wchar_t timeStr[buffSize] = {0};
status = GetTimeFormatEx(LOCALE_NAME_INVARIANT, TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT, &systemTime, L"HH':'mm':'ss", timeStr, buffSize);
#endif // _WIN32_WINNT < _WIN32_WINNT_VISTA
if (status == 0)
{
throw utility::details::create_system_error(GetLastError());
}
outStream << dateStr << "T" << timeStr;
uint64_t frac_sec = largeInt.QuadPart % _secondTicks;
if (frac_sec > 0)
{
// Append fractional second, which is a 7-digit value with no trailing zeros
// This way, '1200' becomes '00012'
char buf[9] = { 0 };
sprintf_s(buf, sizeof(buf), ".%07ld", (long int)frac_sec);
// trim trailing zeros
for (int i = 7; buf[i] == '0'; i--) buf[i] = '\0';
outStream << buf;
}
outStream << "Z";
}
return outStream.str();
#else //LINUX
uint64_t input = m_interval;
uint64_t frac_sec = input % _secondTicks;
input /= _secondTicks; // convert to seconds
time_t time = (time_t)input - (time_t)11644473600LL;// diff between windows and unix epochs (seconds)
struct tm datetime;
gmtime_r(&time, &datetime);
const int max_dt_length = 64;
char output[max_dt_length+1] = {0};
if (format != RFC_1123 && frac_sec > 0)
{
// Append fractional second, which is a 7-digit value with no trailing zeros
// This way, '1200' becomes '00012'
char buf[9] = { 0 };
snprintf(buf, sizeof(buf), ".%07ld", (long int)frac_sec);
// trim trailing zeros
for (int i = 7; buf[i] == '0'; i--) buf[i] = '\0';
// format the datetime into a separate buffer
char datetime_str[max_dt_length+1] = {0};
strftime(datetime_str, sizeof(datetime_str), "%Y-%m-%dT%H:%M:%S", &datetime);
// now print this buffer into the output buffer
snprintf(output, sizeof(output), "%s%sZ", datetime_str, buf);
}
else
{
strftime(output, sizeof(output),
format == RFC_1123 ? "%a, %d %b %Y %H:%M:%S GMT" : "%Y-%m-%dT%H:%M:%SZ",
&datetime);
}
return std::string(output);
#endif
}