2016-01-12 13 views
5

Do tej pory jestem w stanie uzyskać bieżące ustawienia regionalne, ale chcę uzyskać format daty dla danego ustawienia narodowego. Można to zrobić za pomocą standardowej biblioteki.Jak uzyskać bieżący "zlokalizowany wzorzec" dla daty i czasu std :: locale

#include <locale> 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    // Print the current locale 
    std::cout << std::locale("").name().c_str() << "\n"; 

    // TODO: get the locale's date pattern, example for US it's (mm/dd/yyyy) 
    std::cout << "date pattern: \n"; 
} 
+0

Istnieje 'std :: time_get :: date_order' –

+0

faktycznie boost :: date_time jest bardzo przydatne dla tego rodzaju pracy – ead

Odpowiedz

4

Jeśli chcesz po prostu rzucić datę do analogicznego ciąg można użyć std::time_put<char>:

#include <locale> 
#include <ctime> 
#include <string> 
#include <sstream> 
std::string get_date_string(const std::time_t &input_time, const std::locale &loc){ 
    std::tm * time = std::localtime (&input_time); 
    // get time_put facet: 
    const std::time_put<char>& tmput = std::use_facet <std::time_put<char> > (loc); 

    std::stringstream s; 
    s.imbue(loc);//set locale loc to the stream, no matter which global locale 

    std::tm *my_time=std::localtime(&input_time); 
    tmput.put (s, s, ' ', my_time, 'x'); 

    return s.str(); 
    } 

'x' mówi, że chcesz tylko datę. Możliwe są inne formaty - są takie same jak w przypadku strftime. Teraz, po uruchomieniu programu

int main(){ 

     std::time_t timestamp; 
     std::time(&timestamp); 

     std::cout<<"user settings: "<<get_date_string(timestamp, std::locale(""))<<"\n"; 
     std::cout<<"C settings: "<<get_date_string(timestamp, std::locale::classic())<<"\n"; 
} 

na mojej niemieckiej maszyny widzę:

user settings: 13.01.2016 
C settings: 01/13/16 

Jeśli jesteś wolny, aby korzystać zastrzyk, niż to jest trochę łatwiej z boost::data_time:

#include <boost/date_time/gregorian/gregorian.hpp 

using namespace boost::gregorian; 


std::string get_date_string_boost(const date &d, const std::locale &loc){ 
    date_facet* f = new date_facet("%x"); 
    std::stringstream s; 
    s.imbue(std::locale(loc, f)); 
    s<<d; 
    return s.str(); 
} 

, a teraz

int main(){ 
    date d(2015, Jan, 13); 
    std::cout<<"user settings with boost: "<<get_date_string_boost(d, std::locale(""))<<"\n"; 
    std::cout<<"C settings with boost: "<<get_date_string_boost(d, std::locale::classic())<<"\n"; 

} 

daje taki sam wynik jak powyżej.

Jeśli chcesz poznać data wyraźnie, nie sądzę, można prosić o więcej, niż wiedzieć, czy jest ddmmrr (yy) lub mmddrr (yy) lub podobne:

std::string date_order(const std::locale &loc){ 

     std::time_get<char>::dateorder order = std::use_facet<std::time_get<char> >(loc).date_order(); 
     switch (order) { 
     case std::time_get<char>::dmy : return "dd/mm/yyyy"; 
     case std::time_get<char>::mdy : return "mm/dd/yyyy"; 
     case std::time_get<char>::ymd : return "yyyy/mm/dd"; 
     case std::time_get<char>::ydm : return "yyyy/dd/mm"; 
     default: 
      return "no_order";//case std::time_get<char>::no_order 
     } 

    } 

I nie wiem jak jest dystrybucja, na mojej maszynie to "no_order", więc nie oczekuj od niej zbyt wielu informacji.

Powiązane problemy