本地化库
本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。
平面类别
从输入字符序列中解析时间/日期值到 std::tm 中
std::time_get
template< class CharT, |
类模板 std::time_get
封装日期和时间分析规则。 I/O 操纵符 std::get_time 用 I/O 流的 locale 的 std::time_get
平面转换文本输入为 std::tm 对象。
继承图
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。 |
特化
标准库提供二个孤立(独立于本地环境的)全特化和二个部分特化:
定义于头文件 | |
std::time_get<char> | 分析日期和时间的窄字符串表示 |
std::time_get<wchar_t> | 分析日期和时间的宽字符串表示 |
std::time_get<char, InputIt> | 用定制输入迭代器分析日期和时间的窄字符串表示 |
std::time_get<wchar_t, InputIt> | 用定制输入迭代器分析日期和时间的宽字符串表示 |
另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。
成员类型
成员类型 | 定义 |
char_type | CharT |
iter_type | InputIt |
构造新的 time_get 平面
std::time_get<CharT,InputIt>::time_get
explicit time_get( std::size_t refs = 0 ); |
创建 std::time_get 平面并转发引用计数 refs
到基类构造函数 locale::facet::facet() 。
参数
refs | - | 开始的引用计数 |
销毁 time_get 平面
std::time_get<CharT,InputIt>::~time_get
protected: ~time_get(); |
析构 std::time_get 平面。此析构函数为受保护且为虚(由于基类析构函数为虚)。 std::time_get 类型对象,同大多数平面,只能在最后一个实装此平面的 std::locale 离开作用域时,或若用户定义导出自 std::time_get 并实现公开构造函数,才会被销毁。
调用示例
#include <iostream>
#include <locale>
struct Destructible_time_get : public std::time_get<wchar_t>
{
Destructible_time_get(std::size_t refs = 0) : time_get(refs) {}
// 注意:隐式析构函数为公开
};
int main()
{
Destructible_time_get dc;
// std::time_get<wchar_t> c; // 编译错误:受保护析构函数
return 0;
}
调用 do_date_order & 获得偏好的日、月、年顺序
std::time_get<CharT,InputIt>::date_order,
std::time_get<CharT,InputIt>::do_date_order
public: | (1) | |
protected: | (2) |
1) 公开成员函数,调用最终导出类的所保有虚成员函数 do_date_order
。
2) 返回 std::time_base::dateorder 类型值,它描述此 locale 所用的默认日期格式(为 get_date() 所期待并为 std::strftime() 用格式指定符 '%x' 所产生)。
合法值(继承自 std::time_base ):
no_order | 含有可变项目(星期之日、尤里乌斯日等),或未实现此函数 |
dmy | 日、月、年(欧洲本地环境) |
mdy | 月、日、年(美洲本地环境) |
ymd | 年、月、日(亚洲本地环境) |
ydm | 年、日、月(罕见) |
参数
(无)
返回值
dateorder
类型值。
注意
此函数为可选,它可在每种情况下都返回 no_order
。
调用示例 windows
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <Windows.h>
std::vector<std::wstring> locals;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
locals.push_back(pStr);
return TRUE;
}
std::string stows(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest, 0, _Dsize);
wcstombs(_Dest, _Source, _Dsize);
std::string result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
void show_date_order()
{
std::time_base::dateorder dateorder
= std::use_facet<std::time_get<char>>(
std::locale()
).date_order();
switch (dateorder)
{
case std::time_base::no_order:
std::cout << "no_order" << std::endl;
break;
case std::time_base::dmy:
std::cout << "day, month, year" << std::endl;;
break;
case std::time_base::mdy:
std::cout << "month, day, year" << std::endl;;
break;
case std::time_base::ymd:
std::cout << "year, month, day" << std::endl;;
break;
case std::time_base::ydm:
std::cout << "year, day, month" << std::endl;;
break;
}
}
int main()
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);
for (std::vector<std::wstring>::const_iterator str = locals.begin();
str != locals.end(); ++str)
{
std::locale::global(std::locale(stows(*str)));
std::cout << "In " << stows(*str) << " locale, the default date order is: ";
show_date_order();
}
return 0;
}
输出
In de-DE_phoneb locale, the default date order is: day, month, year
In es-ES_tradnl locale, the default date order is: day, month, year
In hu-HU_technl locale, the default date order is: year, month, day
In ja-JP_radstr locale, the default date order is: year, month, day
In ka-GE_modern locale, the default date order is: day, month, year
In x-IV_mathan locale, the default date order is: day, month, year
In zh-CN_phoneb locale, the default date order is: year, month, day
In zh-CN_stroke locale, the default date order is: year, month, day
In zh-HK_radstr locale, the default date order is: year, month, day
In zh-MO_radstr locale, the default date order is: year, month, day
In zh-MO_stroke locale, the default date order is: year, month, day
In zh-SG_phoneb locale, the default date order is: year, month, day
In zh-SG_stroke locale, the default date order is: year, month, day
In zh-TW_pronun locale, the default date order is: year, month, day
In zh-TW_radstr locale, the default date order is: year, month, day