语言环境“en_US”中std :: time_get :: get_time()的正确时间格式是什么?

时间:2014-02-18 11:39:53

标签: c++ locale

locale("en_US")我无法通过std::time_get::get_time()读取时间。 我尝试了以下格式,但它不起作用。 当我将语言环境更改为locale("en_GB")时效果很好。 那么语言环境“en_US”的正确时间格式是什么?

来源是:

void try_get_time_from_str(const std::string& s) {
    std::istringstream str(s);
    std::ios_base::iostate err = std::ios_base::goodbit;
    std::tm t;
    std::istreambuf_iterator<char> ret =
        std::use_facet<std::time_get<char> >(str.getloc()).get_time(
            std::istreambuf_iterator<char>(str),
            std::istreambuf_iterator<char>(),
            str, err, &t
        );
    str.setstate(err);
    if(str) {
        std::cout << "Hours: "   << t.tm_hour << ' '
                  << "Minutes: " << t.tm_min  << ' '
                  << "Seconds: " << t.tm_sec  << '\n';
    } else {
        std::cout << "Parse failed. Unparsed string: ";
        std::copy(ret, std::istreambuf_iterator<char>(),
                  std::ostreambuf_iterator<char>(std::cout));
        std::cout << '\n';
    }
}
int main() {
    std::locale::global(std::locale("en_US"));
    try_get_time_from_str("01:01:01");
    try_get_time_from_str("1:01:01");
    try_get_time_from_str("1:1:1");
    try_get_time_from_str("01:01");
    try_get_time_from_str("1:01");
    try_get_time_from_str("1:1");
    try_get_time_from_str("01:01:01 PM");
    try_get_time_from_str("1:01:01 PM");
    try_get_time_from_str("1:1:1 PM");
    try_get_time_from_str("01:01 PM");
    try_get_time_from_str("1:01 PM");
    try_get_time_from_str("1:1 PM");
    return 0;
}

结果是:

Parse failed. Unparsed string: 01:01:01
Parse failed. Unparsed string: 1:01:01
Parse failed. Unparsed string: 1:1:1
Parse failed. Unparsed string: 01:01
Parse failed. Unparsed string: 1:01
Parse failed. Unparsed string: 1:1
Parse failed. Unparsed string: 01:01:01 PM
Parse failed. Unparsed string: 1:01:01 PM
Parse failed. Unparsed string: 1:1:1 PM
Parse failed. Unparsed string: 01:01 PM
Parse failed. Unparsed string: 1:01 PM
Parse failed. Unparsed string: 1:1 PM

我已尝试在格式为“%X”的locale("en_US")中输出时间,结果为“上午10:27:06”。 并且文档说std::time_get::get_time()的默认格式是“%X”,但它确实无法读取。

PS:我在LINUX上使用g ++(GCC)4.4.7 20120313(Red Hat 4.4.7-3)。

1 个答案:

答案 0 :(得分:0)

它看起来像编译器错误。有g ++ 4.72并且正在运行:

#include <ctime>
#include <iostream>
#include <locale>
#include <sstream>

void try_get_time_from_str(const std::string& s) {
    std::istringstream str(s);
    std::ios_base::iostate err = std::ios_base::goodbit;
    std::tm t;
    std::istreambuf_iterator<char> ret =
        std::use_facet<std::time_get<char> >(str.getloc()).get_time(
            std::istreambuf_iterator<char>(str),
            std::istreambuf_iterator<char>(),
            str, err, &t
        );
    str.setstate(err);
    if(str) {
        std::cout << "Hours: "   << t.tm_hour << ' '
                  << "Minutes: " << t.tm_min  << ' '
                  << "Seconds: " << t.tm_sec  << '\n';
    } else {
        std::cout << "Parse failed. Unparsed string: ";
        std::copy(ret, std::istreambuf_iterator<char>(),
                  std::ostreambuf_iterator<char>(std::cout));
        std::cout << '\n';
    }
}

int main() {
    std::time_t timestamp;
    std::time ( &timestamp );
    std::tm * now = std::localtime ( &timestamp );
    std::cout <<  "GB: ";
    {
        std::locale::global(std::locale("en_GB.utf8"));
        std::ostringstream out;
        std::use_facet< std::time_put<char> >(out.getloc()).put(
            out,
            out,
            ' ',
            now,
            'X');
        try_get_time_from_str(out.str());
    }
    std::cout <<  "US: ";
    {
        std::locale::global(std::locale("en_US.utf8"));
        std::ostringstream out;
        std::use_facet< std::time_put<char> >(out.getloc()).put(
            out,
            out,
            ' ',
            now,
            'X');
        try_get_time_from_str(out.str());
    }
    return 0;
}

给出:

GB: Hours: 14 Minutes: 59 Seconds: 53
US: Parse failed. Unparsed string: 02:59:53 PM