在我尝试使 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 中糟糕的语言环境支持,还是在这里我可以修复或解决错误的依赖于实现的假设(输入格式)?