2016-01-12 5 views
5

की तिथि और समय के लिए वर्तमान "स्थानीयकृत पैटर्न" कैसे प्राप्त करूं, अब तक, मैं वर्तमान लोकेल प्राप्त करने में सक्षम हूं, लेकिन मैं उस विशेष लोकेल के लिए दिनांक प्रारूप प्राप्त करना चाहता हूं। क्या यह मानक पुस्तकालय के साथ किया जा सकता है।मैं 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

'std :: time_get :: date_order' –

+0

वास्तव में बढ़ावा देता है :: date_time इस तरह के काम के लिए बहुत उपयोगी है – ead

उत्तर

4

तुम सिर्फ इसी स्ट्रिंग के लिए एक तिथि कास्ट करने के लिए चाहते हैं तो आप 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' का कहना है कि आप केवल तारीख चाहते हैं। अन्य प्रारूप संभव हैं - वे strftime के समान हैं। अब, मेरी जर्मन मशीन पर कार्यक्रम

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"; 
} 

चलाने के बाद मैं देख रहा हूँ:

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

आप को बढ़ावा देने का उपयोग करने के लिए स्वतंत्र हैं, की तुलना में यह 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(); 
} 

और अब

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"; 

} 

उपरोक्त के समान परिणाम उत्पन्न करता है।

आप स्पष्ट रूप से तिथि के क्रम में जानना चाहते हैं, तो मुझे नहीं लगता कि आप से अधिक के लिए पूछ सकते हैं पता करने के लिए कि क्या यह ddmmyy (yy) या MMDDYY (yy) या इसी तरह की है:

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 
     } 

    } 

मैं यह नहीं पता कि वितरण कैसे है, मेरी मशीन पर यह "no_order" है, इसलिए इससे अधिक जानकारी की अपेक्षा न करें।

संबंधित मुद्दे