获取当前时间作为W3C UTC时间字符串
C++: Getting the Current Time as a W3C UTC Time String
如何将当前时间作为W3C UTC时间字符串记录在W3C日期和时间格式(ISO 8601配置文件)?
具体来说,这是我想要的格式:
yyyy-mm-ddThh:mm:ssZ
的例子:
2013-05-24T20:07:47Z
注意:我使用的是Visual Studio 2010。
这是我的解决方案。参考:time(), gmtime_s()
#include <iomanip>
#include <sstream>
#include <time.h>
/**
* Get the current time as a W3C UTC time string.
*
* @return A time string; the empty string if something goes wrong.
*/
std::string GetUTCTimeString(void)
{
time_t seconds_since_the_epoch;
struct tm tm_struct;
errno_t err;
std::ostringstream buf;
// Return the time as seconds elapsed since midnight, January 1, 1970,
// or -1 in the case of an error.
time(&seconds_since_the_epoch);
if (seconds_since_the_epoch == -1) {
return "";
}
// Convert the time in seconds to the time structure.
err = gmtime_s(&tm_struct, &seconds_since_the_epoch);
if (err) {
return "";
}
// Format the structure as a W3C UTC date/time string with the special
// UTC designator 'Z'.
buf
<< std::setw(4) << std::setfill('0') << tm_struct.tm_year + 1900
<< "-" << std::setw(2) << std::setfill('0') << tm_struct.tm_mon + 1
<< "-" << std::setw(2) << std::setfill('0') << tm_struct.tm_mday
<< "T" << std::setw(2) << std::setfill('0') << tm_struct.tm_hour
<< ":" << std::setw(2) << std::setfill('0') << tm_struct.tm_min
<< ":" << std::setw(2) << std::setfill('0') << tm_struct.tm_sec
<< "Z";
return buf.str();
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在已经使用Git的情况下减少编译时间
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 从文本文件中读取时钟时间和事件时间并进行处理
- 具有未知值时的时间复杂性
- 如何减少花费的时间
- C++在变量给定的指定时间内关闭电脑
- rcpp函数中的清理时间很长
- C++:floor unix时间戳到UTC月份
- 如何在c++中录制具有精确帧时间戳的视频
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- KMP算法和LPS表构造的运行时间
- 为什么std::互斥需要很长的、非常不规则的时间来共享
- 获取当前时间作为W3C UTC时间字符串