Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSVCs Implementation of std::put_time

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?

like image 884
Thomas Russell Avatar asked Jun 25 '13 12:06

Thomas Russell


1 Answers

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.

like image 165
Igor Tandetnik Avatar answered Nov 07 '22 06:11

Igor Tandetnik