本地化库
本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 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 |
调用 do_get_time & 从输入流释出时、分、秒
std::time_get<CharT,InputIt>::get_time,
std::time_get<CharT,InputIt>::do_get_time
public: iter_type get_time( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t) const; | (1) | |
protected: virtual iter_type get_time( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t) const; | (2) |
1) 公开成员函数,调用最终导出类的受保护虚成员函数 do_get_time
。
2) 从字符序列 [beg, end)
读取相继的字符,并分析出遵循与下列格式指定符相同规则的时间值
'%X' | (C++11 前) |
"%H:%M:%S" | (C++11 起) |
格式指定符为函数 std::get_time 、 time_get::get 和 POSIX 函数 strptime()
所用。
存储分析的时间到参数 t
所指向的 std::tm 结构体的对应域中。
若在读到合法值前抵达尾迭代器,则函数设置 err
中的 std::ios_base::eofbit 。若遇到分析错误,则函数设置 err
中的 std::ios_base::failbit 。
参数
beg | - | 指代要分析的序列起始的迭代器 |
end | - | 要分析的序列的尾后一位置迭代器 |
str | - | 此函数在需要时用以获得 locale 平面的流对象,例如用 std::ctype 跳过空白符 |
err | - | 此函数所修改以指示错误的流错误标志对象 |
t | - | 指向 std::tm 对象的指针,该对象将保有此函数调用结果 |
返回值
指向 [beg, end)
中辨识为合法日期一部分的末字符后一位置的迭代器。
注意
对于默认时间格式的字母组分(若存在),此函数通常不区别大小写。
若遇到分析错误,则此函数的大多数实现保留 *t
不修改。
调用示例 windows
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#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 try_get_time(const std::string& str)
{
std::cout << "Parsing the time out of '" << str <<
"' in the locale " << std::locale().name() << ' ';
std::istringstream istringstream(str);
std::tm tm = {};
std::ios_base::iostate err = std::ios_base::goodbit;
std::istreambuf_iterator<char> ret =
std::use_facet<std::time_get<char>>(istringstream.getloc()).get_time(
{istringstream}, {}, istringstream, err, &tm);
istringstream.setstate(err);
if (istringstream)
{
std::cout << "Hours: " << tm.tm_hour << ' '
<< "Minutes: " << tm.tm_min << ' '
<< "Seconds: " << tm.tm_sec << std::endl;
}
else
{
std::cout << "Parse failed. Unparsed string: ";
std::copy(ret, std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(std::cout));
std::cout << std::endl;
}
}
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)));
try_get_time("21:40:11");
}
return 0;
}
输出
Parsing the time out of '21:40:11' in the locale de-DE_phoneb Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale es-ES_tradnl Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale hu-HU_technl Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale ja-JP_radstr Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale ka-GE_modern Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale x-IV_mathan Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-CN_phoneb Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-CN_stroke Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-HK_radstr Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-MO_radstr Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-MO_stroke Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-SG_phoneb Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-SG_stroke Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-TW_pronun Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21:40:11' in the locale zh-TW_radstr Hours: 21 Minutes: 40 Seconds: 11