How do I construct an ISO 8601 datetime in C++?

前端 未结 9 1243
小蘑菇
小蘑菇 2020-12-05 01:51

I\'m working with the Azure REST API and they are using this to create the request body for table storage:

DateTime.UtcNow.ToString(\"o\")

9条回答
  •  孤城傲影
    2020-12-05 02:34

    Tested in Visual C++, GNU C++, Emscripten

    #include 
    #include 
    #include  
    #include   
    
    #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 <(now.time_since_epoch()).count() % static_cast(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 <


    Results

    UTC : 2020-04-12T17:00:18.632Z
    LOCAL: 2020-04-12T12:00:18.633-0500

    You can deserialize normally with Json.NET

提交回复
热议问题