[英]std::get_time and other locale functionality not working correctly on Windows
在嘗試使libc ++及其測試在Windows上運行時,遇到了一個似乎無法解決的問題。 以下代碼取自libc ++測試代碼,並在Mac(也可能還有FreeBSD)上傳遞,但不適用於MinGW-w64或MSVC 2010 SP1。
#include <iomanip>
#include <iostream>
#include <cassert>
template <class CharT>
struct testbuf
: public std::basic_streambuf<CharT>
{
typedef std::basic_string<CharT> string_type;
typedef std::basic_streambuf<CharT> base;
private:
string_type str_;
public:
testbuf() {}
testbuf(const string_type& str)
: str_(str)
{
base::setg(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data()) + str_.size());
}
};
#if _WIN32
#define LOCALE_en_US_UTF_8 "English_USA.1252"
#else
#define LOCALE_en_US_UTF_8 "en_US.UTF-8"
#endif
int main()
{
testbuf<char> sb(" Sat Dec 31 23:55:59 2061");
std::istream is(&sb);
is.imbue(std::locale(LOCALE_en_US_UTF_8));
std::tm t = {0};
is >> std::get_time(&t, "%c");
std::cout << t.tm_sec << "\n";
std::cout << t.tm_min << "\n";
std::cout << t.tm_hour << "\n";
std::cout << t.tm_mday << "\n";
std::cout << t.tm_mon << "\n";
std::cout << t.tm_year << "\n";
std::cout << t.tm_wday << "\n";
assert(t.tm_sec == 59);
assert(t.tm_min == 55);
assert(t.tm_hour == 23);
assert(t.tm_mday == 31);
assert(t.tm_mon == 11);
assert(t.tm_year == 161);
assert(t.tm_wday == 6);
}
對於Mac / FreeBSD,測試通過了,但是對於Windows,不同的元素都為0。 對於MinGW-w64 + libc ++和MSVC10 + Microsoft的STL來說,這是正確的。
這僅僅是Windows中糟糕的語言環境支持,還是在這里我可以解決或解決的錯誤的依賴實現的假設(輸入格式)在起作用?
我不相信您使用正確的語言環境字符串。 他們似乎真的很糟糕記錄,雖然有什么看起來像一個良好的名單在這里 。
也許是“ american_us.1252”? (當然,這些字符串都是實現定義的。)
這里暴露的問題不是-錯誤的語言環境名稱-錯誤的std::tm
值
但是,這恰好在於%c
格式說明符無法滿足Windows預期的事實。 通過完整指定格式,很容易解決。 在這種情況下,我使用的是:
"%a %b %d %H" ":" "%M" ":" "%S %Y"
它在%Y
部分仍然存在問題( tm_year
仍為零)...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.