is repeating only once

Asked

Viewed 53 times

0

Code:

#include <iomanip>
#include <iostream>

int main() 
{

auto time = std::time(nullptr);

std::string lang_utf[6]={"pt_BR.utf8","en_US.utf8","en_GB.utf8","de_DE.utf8","ja_JP.utf8","ru_RU.utf8"};
    std::string lang[6]={"pt_BR: ","en_US: ","en_GB: ","de_DE: ","ja_JP: ","ru_RU: "};

    //std::cout << "Undefined: " << std::put_time(std::gmtime(&time), "%c") << "\n";

    for(int i=0; i<6;i++)
    {
     std::cout.imbue(std::locale(lang_utf[i]));
     for(int j=0; j<6;j++)
     { 
      std::cout << lang[j] << std::put_time(std::gmtime(&time), "%c") << "\n";;        
     }
    }

    return 0;
}

Exit:

pt_BR: seg 16 out 2017 20:07:50 GMT
en_US: seg 16 out 2017 20:07:50 GMT
en_GB: seg 16 out 2017 20:07:50 GMT
de_DE: seg 16 out 2017 20:07:50 GMT
ja_JP: seg 16 out 2017 20:07:50 GMT
ru_RU: seg 16 out 2017 20:07:50 GMT
pt_BR: Mon 16 Oct 2017 08:07:50 PM GMT
en_US: Mon 16 Oct 2017 08:07:50 PM GMT
en_GB: Mon 16 Oct 2017 08:07:50 PM GMT
de_DE: Mon 16 Oct 2017 08:07:50 PM GMT
ja_JP: Mon 16 Oct 2017 08:07:50 PM GMT
ru_RU: Mon 16 Oct 2017 08:07:50 PM GMT
pt_BR: Mon 16 Oct 2017 20:07:50 GMT
en_US: Mon 16 Oct 2017 20:07:50 GMT
en_GB: Mon 16 Oct 2017 20:07:50 GMT
de_DE: Mon 16 Oct 2017 20:07:50 GMT
ja_JP: Mon 16 Oct 2017 20:07:50 GMT
ru_RU: Mon 16 Oct 2017 20:07:50 GMT
pt_BR: Mo 16 Okt 2017 20:07:50 GMT
en_US: Mo 16 Okt 2017 20:07:50 GMT
en_GB: Mo 16 Okt 2017 20:07:50 GMT
de_DE: Mo 16 Okt 2017 20:07:50 GMT
ja_JP: Mo 16 Okt 2017 20:07:50 GMT
ru_RU: Mo 16 Okt 2017 20:07:50 GMT
pt_BR: 2017年10月16日 20時07分50秒
en_US: 2017年10月16日 20時07分50秒
en_GB: 2017年10月16日 20時07分50秒
de_DE: 2017年10月16日 20時07分50秒
ja_JP: 2017年10月16日 20時07分50秒
ru_RU: 2017年10月16日 20時07分50秒
pt_BR: Пн 16 окт 2017 20:07:50
en_US: Пн 16 окт 2017 20:07:50
en_GB: Пн 16 окт 2017 20:07:50
de_DE: Пн 16 окт 2017 20:07:50
ja_JP: Пн 16 окт 2017 20:07:50
ru_RU: Пн 16 окт 2017 20:07:50
bash-4.4$ g++ -o loc locdate.cxx
bash-4.4$ ./loc
pt_BR: seg 16 out 2017 20:08:36 GMT
en_US: seg 16 out 2017 20:08:36 GMT
en_GB: seg 16 out 2017 20:08:36 GMT
de_DE: seg 16 out 2017 20:08:36 GMT
ja_JP: seg 16 out 2017 20:08:36 GMT
ru_RU: seg 16 out 2017 20:08:36 GMT
pt_BR: Mon 16 Oct 2017 08:08:36 PM GMT
en_US: Mon 16 Oct 2017 08:08:36 PM GMT
en_GB: Mon 16 Oct 2017 08:08:36 PM GMT
de_DE: Mon 16 Oct 2017 08:08:36 PM GMT
ja_JP: Mon 16 Oct 2017 08:08:36 PM GMT
ru_RU: Mon 16 Oct 2017 08:08:36 PM GMT
pt_BR: Mon 16 Oct 2017 20:08:36 GMT
en_US: Mon 16 Oct 2017 20:08:36 GMT
en_GB: Mon 16 Oct 2017 20:08:36 GMT
de_DE: Mon 16 Oct 2017 20:08:36 GMT
ja_JP: Mon 16 Oct 2017 20:08:36 GMT
ru_RU: Mon 16 Oct 2017 20:08:36 GMT
pt_BR: Mo 16 Okt 2017 20:08:36 GMT
en_US: Mo 16 Okt 2017 20:08:36 GMT
en_GB: Mo 16 Okt 2017 20:08:36 GMT
de_DE: Mo 16 Okt 2017 20:08:36 GMT
ja_JP: Mo 16 Okt 2017 20:08:36 GMT
ru_RU: Mo 16 Okt 2017 20:08:36 GMT
pt_BR: 2017年10月16日 20時08分36秒
en_US: 2017年10月16日 20時08分36秒
en_GB: 2017年10月16日 20時08分36秒
de_DE: 2017年10月16日 20時08分36秒
ja_JP: 2017年10月16日 20時08分36秒
ru_RU: 2017年10月16日 20時08分36秒
pt_BR: Пн 16 окт 2017 20:08:36
en_US: Пн 16 окт 2017 20:08:36
en_GB: Пн 16 окт 2017 20:08:36
de_DE: Пн 16 окт 2017 20:08:36
ja_JP: Пн 16 окт 2017 20:08:36
ru_RU: Пн 16 окт 2017 20:08:36

What the exit should be like:

pt_BR: seg 16 out 2017 20:12:12 GMT
en_US: Mon 16 Oct 2017 08:12:12 PM GMT
en_GB: Mon 16 Oct 2017 20:12:12 GMT
de_DE: Mo 16 Okt 2017 20:12:12 GMT
ja_JP: 2017年10月16日 20時12分12秒
ru_RU: Пн 16 окт 2017 20:12:12

You can do it the right way?

  • If you take the second for becomes as?

  • error it does not find variable i.

  • Now I understood I took a for and put the variable i out of it..

  • for(int i=0; i<6;i++) { Std::Cout.Imbue(Std::locale(lang_utf[i]); Std::Cout << lang[i] << Std::put_time(Std::gmtime(&time), "%c") << " n"; } .

  • worked out here just now...

1 answer

1


You don’t need two ties to solve the problem, only one is enough:

#include <iomanip>
#include <iostream>

int main(void)
{
    std::time_t now = std::time(nullptr);

    std::string lang[6] = { "pt_BR", "en_US", "en_GB", "de_DE", "ja_JP", "ru_RU" };

    for( int i = 0; i < 6; i++ )
    {
        std::string loc = lang[i] + ".utf8";

        try
        {
            std::cout.imbue(std::locale(loc));
            std::cout << lang[i] << ": " << std::put_time(std::gmtime(&now), "%c") << std::endl;
        }
        catch( std::exception &e )
        {
            std::cout << lang[i] << " nao suportada!" << std::endl;
        }
    }

    return 0;
}

Compiling code in default C++11 using the compiler GCC v5:

g++ -std=c++11 main.cpp -o teste

Exit:

pt_BR nao suportada!
en_US: Tue 17 Oct 2017 03:25:59 PM GMT
en_GB: Tue 17 Oct 2017 15:25:59 GMT
de_DE: Di 17 Okt 2017 15:25:59 GMT
ja_JP: 2017年10月17日 15時25分59秒
ru_RU: Вт 17 окт 2017 15:25:59

Code in the coliru

Reference: http://en.cppreference.com/w/cpp/io/manip/put_time

  • Hadn’t I thought of a way like this to do? sometimes I think I’m smart in the way of thinking but I see that from what I know nothing I know... Now it was cool worked here... kkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.