C++ 如何获取当前的“数据”;“本地化模式”;用于std::locale的日期和时间
到目前为止,我能够获取当前区域设置,但我想获取该特定区域设置的日期格式。这可以通过标准库来实现C++ 如何获取当前的“数据”;“本地化模式”;用于std::locale的日期和时间,c++,windows,locale,c++-standard-library,boost-date-time,C++,Windows,Locale,C++ Standard Library,Boost Date Time,到目前为止,我能够获取当前区域设置,但我想获取该特定区域设置的日期格式。这可以通过标准库来实现 #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 pa
#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";
}
#包括
int _tmain(int argc,_TCHAR*argv[]
{
//打印当前区域设置
std::cout如果只想将日期强制转换为相应的字符串,可以使用std::time\u put
:
在我的德国机器上,我看到:
user settings: 13.01.2016
C settings: 01/13/16
如果您可以自由使用boost,那么使用它会更容易:
#include有std::time\u get::date\u order
实际上boost::date\u time对这类工作非常有用
int main(){
std::time_t timestamp;
std::time( ×tamp );
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
#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";
}
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
}
}