我正在使用 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 日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!