![](/img/trans.png)
[英]std::get_time and other locale functionality not working correctly on Windows
[英]std::get_time not working correctly in VS 2017?
使用VS 2017構建和運行時,以下代碼不會引發異常。請注意,日期中的月份無效。
#include <sstream>
#include <iomanip>
#include <cctype>
#include <iostream>
int main()
{
std::string invalidMonth = "2013-13-02T19:58:55Z";
std::tm datetime = {};
std::istringstream iss(invalidMonth);
iss >> std::get_time(&datetime, "%Y-%m-%dT%H:%M:%S");
if (iss.fail())
{
std::ostringstream oss;
oss << "Invalid value '" << invalidMonth << "'\n";
throw std::runtime_error(oss.str());
}
else
{
std::cout << "No error\n";
}
return 0;
}
解析后的datetime
值為:
tm_sec 55 int
tm_min 58 int
tm_hour 19 int
tm_mday 2 int
tm_mon 0 int
tm_year 113 int
tm_wday 0 int
tm_yday 0 int
tm_isdst 0 int
使用VS 2013構建和運行時,此代碼將引發異常。
它可以正常工作。
如果生成的錯誤狀態標志不是ios_base::iostate::goodbit
且成員std::basic_ios::exceptions
設置為針對該狀態拋出,則std::get_time
引發ios_base::failure
std::basic_ios::exceptions
。
檢查iss.exceptions()
返回的值以查看設置了eofbit
, failbit
和badbit
。
這是所有流操作的常見情況,不僅是std::get_time
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.