c++ - 如何在 C++ 中构造 ISO 8601 日期时间?

标签 c++ qt azure

我正在使用 Azure REST API,他们正在使用它来创建表存储的请求正文:

DateTime.UtcNow.ToString("o")

产生:

2012-03-02T04:07:34.0218628Z

它被称为“往返”,显然它是一个 ISO 标准(请参阅 http://en.wikipedia.org/wiki/ISO_8601 ),但在阅读 wiki 文章后我不知道如何复制它。

有谁知道 Boost对此有支持,或者可能Qt

最佳答案

如果精确到秒的时间足够精确,您可以使用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";
}

如果您需要更高的精度,可以使用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++ - 如何在 C++ 中构造 ISO 8601 日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527960/

相关文章:

c# - 自动执行 C++ 库的 C# 包装

c++ - 从源代码构建 Qt 5.8 - 配置 : Unknown command line option '-c++11'

azure - 使用ARM参数选择对象变量中的字段

c# - Azure Cosmos DB 通过 C# 代码将集合 TTL 设置为 - on(无默认值)

c++ - 使用 RapidXML 和 C++ 从 XML 文件构建树

c++ - OpenXML SDK C++ 示例

c++ - 无法在 Logcat 上显示加密文本

qt - QTreeView 中的 QModelIndexList 提供调试断言

python - 如何在 PySide 的 QTreeView 中隐藏 QFileSystemModel 中的项目?

c# - Azure EventHub - 如何删除事件?