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

前端 未结 9 1233
小蘑菇
小蘑菇 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 <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

    0 讨论(0)
  • 2020-12-05 02:39

    In Qt, that would be:

    QDateTime dt = QDateTime::currentDateTime();
    dt.setTimeSpec(Qt::UTC);  // or Qt::OffsetFromUTC for offset from UTC
    qDebug() << QDateTime::currentDateTime().toString(Qt::ISODate);
    
    0 讨论(0)
  • 2020-12-05 02:40

    I should point out I am a C++ newb.

    I needed string with a UTC ISO 8601 formatted date and time that included milliseconds. I did not have access to boost.

    This is more of a hack than a solution, but it worked well enough for me.

    std::string getTime()
    {
        timeval curTime;
    
        gettimeofday(&curTime, NULL);
    
        int milli = curTime.tv_usec / 1000;
        char buf[sizeof "2011-10-08T07:07:09.000Z"];
        strftime(buf, sizeof buf, "%FT%T", gmtime(&curTime.tv_sec));
        sprintf(buf, "%s.%dZ", buf, milli);
    
        return buf;
    }
    

    The output looks like: 2016-04-13T06:53:15.485Z

    0 讨论(0)
提交回复
热议问题