gpt4 book ai didi

c++ - 不能将 %F 与 std::get_time() 一起使用

转载 作者:行者123 更新时间:2023-11-28 01:35:25 26 4
gpt4 key购买 nike

我想弄清楚为什么我不能在 C++11 中将 std::get_time()%F 格式说明符一起使用。它在 clang 3.8.0 或 gcc 5.4.0 中对我不起作用。我的印象是 strftime()%F 格式说明符是 C++11 标准的一部分,而 std::get_time() 使用 strftime() 格式说明符?我是不是错了,还是我的代码有问题?

#include <cstdio>   // printf()
#include <ctime> // std::tm
#include <cstring> // memcpy()
#include <sstream> // std::istringstream
#include <iomanip> // std::get_time()

void print(const std::istringstream& iss, const struct tm& time_struct)
{
if(! iss.fail())
{
printf("struct tm {\n"
" tm_sec = %d\n"
" tm_min = %d\n"
" tm_hour = %d\n"
" tm_mday = %d\n"
" tm_mon = %d\n"
" tm_year = %d\n"
" tm_wday = %d\n"
" tm_yday = %d\n"
" tm_isdst = %d\n"
"}\n",
time_struct.tm_sec,
time_struct.tm_min,
time_struct.tm_hour,
time_struct.tm_mday,
time_struct.tm_mon,
time_struct.tm_year,
time_struct.tm_wday,
time_struct.tm_yday,
time_struct.tm_isdst);
}
else
printf("failed to parse\n");
}

int main(int argc, char** argv)
{
struct tm time_struct;

std::istringstream iss(argv[1]);
memset(&time_struct, 0, sizeof(struct tm));
iss >> std::get_time(&time_struct, "%Y-%m-%d");

print(iss, time_struct);

std::istringstream iss2(argv[1]);
memset(&time_struct, 0, sizeof(struct tm));
iss2 >> std::get_time(&time_struct, "%F");

print(iss2, time_struct);

return 0;
}

这是输出。如您所见,"%Y-%m-%d" 格式按预期工作,但假定等效的 "%F" 却没有(它无法解析).

$ clang++ -std=c++11 main.cpp
$ ./a.out 2015-09-10
struct tm {
tm_sec = 0
tm_min = 0
tm_hour = 0
tm_mday = 10
tm_mon = 8
tm_year = 115
tm_wday = 0
tm_yday = 0
tm_isdst = 0
}
failed to parse

最佳答案

根据 http://en.cppreference.com/w/cpp/io/manip/get_time :

The format specifiers match the POSIX function strptime()

Which没有提到 %F 说明符。

关于c++ - 不能将 %F 与 std::get_time() 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49517216/

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