问题描述
我正在使用 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) 但我不知道如何在阅读 wiki 文章后复制它.
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.
有谁知道 Boost 是否支持这个,或者可能 Qt?
Does anyone know if Boost has support for this, or possibly Qt?
推荐答案
如果到最接近秒的时间足够精确,可以使用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 << "
";
}
如果您需要更高的精度,可以使用 Boost:
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
";
}
这篇关于如何在 C++ 中构建 ISO 8601 日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)