gpt4 book ai didi

c++ - 使用 std::get_time 解析日期/时间字符串需要分隔符?

转载 作者:可可西里 更新时间:2023-11-01 18:35:52 24 4
gpt4 key购买 nike

使用 std::get_time 解析字符串是否需要分隔符?我找不到一个引用来说明它是。我正在尝试解析 ISO 日期/时间字符串,例如“20140105T123456”——例如:

例如,

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <ctime>

int main(int argc, char* argv[])
{
std::tm t = { 0 };

// fails
std::istringstream ss("20141105T123456");
ss >> std::get_time(&t, "%Y%m%dT%H%M%S");

// works
//std::istringstream ss("2014 11 05 T 12 34 56");
//ss >> std::get_time(&t, "%Y %m %d T %H %M %S");

std::ostringstream os;
std::cout << std::put_time(&t, "%c") << std::endl;
}

我正在使用 Visual Studio 2013。我尝试在 Linux 上构建,但我拥有的最新版本的 GCC 是 4.7.3,它似乎还不支持 get_time。

我的愚蠢错误还是需要分隔符?

最佳答案

根据 the description of second parameter of std::get_time,不需要分隔符。

The format string consists of zero or more conversion specifiers, whitespace characters, and ordinary characters (except %). Each ordinary character is expected to match one character in the input stream in case-insensitive comparison. Each whitespace character matches arbitrary whitespace in the input string. Each conversion specification begins with % character, optionally followed by E or O modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The format specifiers match the POSIX function strptime()

在我的 Mac 上,我使用 clang++(Apple LLVM 5.0 版(clang-500.2.79)(基于 LLVM 3.3svn)) 编译您的代码,并运行程序,输出为:Sun Nov 5 12:34:56 2014。实际上,我安装了 gcc 4.8.2,但它不支持 std::get_timestd::put_time。然后我搜索这个函数的实现状态,找到that is not implemented in GCC 4.8.0

它在 VS2013 中确实失败了,在调用 std::get_time 之后,t 中的所有元素都只是 0。格式说明符不会执行 Windows 上预期的操作。这不是你的错。

关于c++ - 使用 std::get_time 解析日期/时间字符串需要分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21172767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com