- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
使用 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_time
和 std::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/
我在尝试使用 put_time 和 get_time 函数时遇到了一些问题。 我拿了这段代码: #include #include #include #include int main() {
我正在解决网站上的问题。我的代码如下: string timeConversion(string s) { std::tm t = {}; string result = "";
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
如果我尝试使用 std::get_time 在 tm 中设置日期什么也没有发生,但是输入流处于失败状态,这意味着解析错误已经发生了。 下面的代码有什么问题? { // setting time w
我正在使用以下代码将字符串流解析为 tm 结构: std::tm tm; std::stringstream ss("Jan 9 2014 12:35:34"); ss >> std::get_tim
我正在尝试解析日期时间字符串并将结果放入 std::tm 结构中。下面是代码, #include #include #include #include std::stringstream ss
我在这个例子中花了很多时间,每次我得到错误 Unable to read cin with an ios_base::iostate equal to failbit 来自这段代码: #include
我有这个简单的功能: char* get_time() { char *buffer = malloc(sizeof(char)*10); /* HOW TO FREE IT ? */
使用 std::get_time 解析字符串是否需要分隔符?我找不到一个引用来说明它是。我正在尝试解析 ISO 日期/时间字符串,例如“20140105T123456”——例如: 例如, #inclu
我想弄清楚为什么我不能在 C++11 中将 std::get_time() 与 %F 格式说明符一起使用。它在 clang 3.8.0 或 gcc 5.4.0 中对我不起作用。我的印象是 strfti
我的类中有以下 C++ 代码,用于将 ISO 8601 字符串转换为 time_t 结构: #include #include #include #include #include ....
我正在尝试这样做但失败了: std::istringstream ss("1212"); ss >> std::get_time(&t, "%y%m"); if (ss.fail()) // ever
我正在使用 Visual Studio 2015 在 Windows 上执行以下代码。基本上我使用 std::get_time 来解析日期,但是当日期无效时,例如,一天大于31,它似乎没有在流上设置失
我正在尝试在 visual studio 2012 中使用 std::get_time 解析日期和时间字符串。时间字符串似乎可以正确解析,但不能正确解析日期字符串。我想最终将日期字符串转换为整数以进行
在 locale("en_US") 中,我无法通过 std::time_get::get_time() 读取时间。我尝试了以下格式,但它不起作用。当我将区域设置更改为 locale("en_GB")
我正在尝试按时间顺序对照片进行排序。因此,我从 EXIF 数据中将时间提取为字符串,然后将其转换为 std::time_t。但是我有时会得到不正确的结果。我已将问题简化为这个最小的例子。它具有三个时间
我今天正在处理一些时间函数,并注意到使用 %r(或 %p)的标准转换似乎不适用于通过 的输入g++ 或 clang++ 上的 std::get_time()。见 this live code vers
我正在尝试解析格式为 YYMMDD 的日期。作为测试,我尝试了以下代码: #include #include #include #include int main(){ std::tm
我想将 ANSI C 格式的字符串转换为 std::tm 对象。 例子 Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format 更多信息在这里 here
我正在尝试创建简单的 jni,但应用程序崩溃了。我无法找到我所犯的错误。这是我的代码: 示例.c #include double My_variable = 3.0; int fact(int
我是一名优秀的程序员,十分优秀!