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

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

提交回复
热议问题