我如何构建在C ++中ISO 8601日期时间? [英] How do I construct an ISO 8601 datetime in C++?

查看:350
本文介绍了我如何构建在C ++中ISO 8601日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Azure的REST API工作,他们利用这种创建表的存储请求体:

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")

主要生产:

2012-03-02T04:07:34.0218628Z

2012-03-02T04:07:34.0218628Z

这就是所谓的往返,显然这是一个ISO标准(见 http://en.wikipedia.org/wiki / ISO_8601 ),但我不知道如何阅读维基文章后,复制它。

It is called "round-trip" and apparently it's an ISO standard (see http://en.wikipedia.org/wiki/ISO_8601) but I have no idea how to replicate it after reading the wiki article.

有谁知道,如果提升已经支持这一点,或者可能的 Qt的

Does anyone know if Boost has support for this, or possibly Qt?

请注意:我会比较它与C#后,周末送还给你。

Note: I'll get back to you after the weekend after comparing it to C#.

推荐答案

如果时间精确到秒为precise的话,你可以使用的strftime

If the time to the nearest second is precise enough, you can use strftime:

#include <ctime>
#include <iostream>

int main() {
    time_t now;
    time(&now);
    char buf[sizeof "2011-10-08T07:07:09Z"];
    strftime(buf, sizeof buf, "%FT%TZ", gmtime(&now));
    // this will work too, if your compiler doesn't support %F or %T:
    //strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
    std::cout << buf << "\n";
}

如果您需要更多的precision,你可以使用升压:

If you need more precision, you can use Boost:

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>

int main() {
    using namespace boost::posix_time;
    ptime t = microsec_clock::universal_time();
    std::cout << to_iso_extended_string(t) << "Z\n";
}

这篇关于我如何构建在C ++中ISO 8601日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆