Pergunta

I am working with Microsoft Visual Studio 2012, and was looking at using std::put_time, so I created the following example:

int main()
{
    std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

    std::locale::global( std::locale("en-GB") );

    std::cout << std::put_time( std::localtime( &t ), "%x" ) << std::endl;
}

This produces the following output:

06/25/2013

Which isn't the date format I would expect from the en-GB locale. I also tried:

std::cout.imbue( std::locale("en-GB") );

But again, with the same output. Is this what output I should be getting for this locale, or have I made a mistake somewhere?

Foi útil?

Solução

Working as intended. std::put_time works off of the stream's locale, not the global locale. cout is created, and imbued with the current global locale, before main is entered. Subsequent change to global locale doesn't affect it. You need to imbue() it explicitly.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top