如何获得当前的“本地化模式"?用于std :: locale的日期和时间 [英] How do I get the current "localized pattern" for the date and time of an std::locale
问题描述
到目前为止,我能够获取当前的语言环境,但是我想获取该特定语言环境的日期格式.可以使用标准库吗?
So far, I'm able to get the current locale, but I want to get the date format for that particular locale. Can this be done with the standard library.
#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";
}
推荐答案
如果只想将日期转换为相应的字符串,则可以使用std::time_put<char>
:
If you just want to cast a date to the corresponding string you can use 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 相同.现在,在运行程序之后
'x'
says that you only want the date. Other formats are possible - they are the same as for strftime. Now, after running the program
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";
}
在我的德国机器上,我看到:
on my German machine I see:
user settings: 13.01.2016
C settings: 01/13/16
如果您可以自由使用boost,那么 boost :: data_time :
If you are free to use boost, than it is a little bit easier with 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";
}
产生与上面相同的结果.
yields the same result as above.
如果您想明确地知道日期顺序,那么我想问您除了知道它是ddmmyy(yy)还是mmddyy(yy)或类似的东西就没什么了.
If you want to know the date order explicitly, I don't think you can ask for more than to know whether it is ddmmyy(yy) or mmddyy(yy) or similar:
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",所以不要期望太多信息.
I don't know how the distribution is, on my machine it is "no_order", so don't expect too much information from it.
这篇关于如何获得当前的“本地化模式"?用于std :: locale的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!