0

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

4

2 回答 2

0

我不相信你有正确的语言环境字符串。它们的文档似乎真的很糟糕,尽管这里有一个看起来不错的列表。

也许是“american_us.1252”?(当然,这些字符串都是实现定义的。)

于 2011-10-02T18:40:14.130 回答
0

这里暴露的问题不是 - 错误的语言环境名称 - 错误的std::tm

但正是因为%c格式说明符在 Windows 上没有达到预期的效果。通过完整指定格式,这很容易解决。在这种情况下,我使用的是:

"%a %b %d %H" ":" "%M" ":" "%S %Y"

它仍然有%Y部分问题(tm_year仍然为零)......

于 2011-10-03T16:22:10.050 回答