本地化库
本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。
平面类别
表示系统提供的具名本地环境的 std::time_get
std::time_get_byname
template< class CharT, |
std::time_get_byname 是 std::time_get 平面,封装在其构造时指定的 locale 的日期和时间分析规则。
标准库提供二个特化
定义于头文件 | |
std::time_get_byname<char, InputIterator> | 窄/多字节时间分析 |
std::time_get_byname<wchar_t, InputIterator> | 宽字符串时间分析 |
成员类型
成员类型 | 定义 |
char_type | CharT |
iter_type | OutputIterator |
成员函数
(构造函数) | 构造新的 time_get_byname 平面(公开成员函数) |
(析构函数) | 析构 time_get_byname 平面(受保护成员函数) |
std::time_get_byname::time_get_byname
explicit time_get_byname( const char* name, std::size_t refs = 0 ); | ||
explicit time_get_byname( const std::string& name, std::size_t refs = 0 ); | (C++11 起) |
为名为 name
的本地环境构造新的 std::time_get_byname
平面。
refs
用于资源管理:在销毁最后一个保有平面的 std::locale 对象时,若 refs == 0 ,则实现销毁平面对象。否则,不销毁对象。
参数
name | - | 本地环境的名称 |
refs | - | 链接到该平面的引用数 |
std::time_get_byname::~time_get_byname
protected: |
销毁平面。
继承自 std::time_get
成员类型 | 定义 |
char_type | CharT |
iter_type | InputIt |
成员对象
成员名 | 类型 |
id [静态] | std::locale::id |
成员函数
date_order | 调用 do_date_order ( std::time_get<CharT,InputIt> 的公开成员函数) |
get_time | 调用 do_get_time ( std::time_get<CharT,InputIt> 的公开成员函数) |
get_date | 调用 do_get_date ( std::time_get<CharT,InputIt> 的公开成员函数) |
get_weekday | 调用 do_get_weekday ( std::time_get<CharT,InputIt> 的公开成员函数) |
get_monthname | 调用 do_get_monthname ( std::time_get<CharT,InputIt> 的公开成员函数) |
get_year | 调用 do_get_year ( std::time_get<CharT,InputIt> 的公开成员函数) |
get (C++11) | 调用 do_get ( std::time_get<CharT,InputIt> 的公开成员函数) |
受保护成员函数
do_date_order [虚] | 获得偏好的日、月、年顺序 ( std::time_get<CharT,InputIt> 的虚受保护成员函数) |
do_get_time [虚] | 从输入流释出时、分、秒 ( std::time_get<CharT,InputIt> 的虚受保护成员函数) |
do_get_date [虚] | 从输入流输出月、日以及年 ( std::time_get<CharT,InputIt> 的虚受保护成员函数) |
do_get_weekday [虚] | 从输入流释出星期的日名 ( std::time_get<CharT,InputIt> 的虚受保护成员函数) |
do_get_monthname [虚] | 从输入流释出月名 ( std::time_get<CharT,InputIt> 的虚受保护成员函数) |
do_get_year [虚] | 从输入流释出年份 ( std::time_get<CharT,InputIt> 的虚受保护成员函数) |
do_get [虚] (C++11) | 从输入流释出日期/时间组分,按照指定格式 ( std::time_get<CharT,InputIt> 的虚受保护成员函数) |
继承自 std::time_base
类型 | 定义 |
dateorder | 日期顺序枚举类型,定义值 no_order 、 dmy 、 mdy 、 ymd 及 ydm |
调用示例 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;
}
int main()
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);
for (std::vector<std::wstring>::const_iterator str = locals.begin();
str != locals.end(); ++str)
{
std::istringstream istringstream("2024-05-01 23:12:34");
istringstream.imbue(std::locale(stows(*str)));
std::cout << "The locale " << istringstream.getloc().name() << ' ';
auto& use_facet = std::use_facet<std::time_get_byname<char>>(istringstream.getloc());
std::tm tm{};
std::string string = "%Y-%M-%d %H:%M:%S";
std::ios_base::iostate err = std::ios_base::goodbit;
auto ret = use_facet.get({istringstream}, {}, istringstream, err,
&tm, &string[0], &string[0] + string.size());
istringstream.setstate(err);
std::istreambuf_iterator<char> last{};
if (istringstream)
{
std::cout << "Successfully parsed as " << std::put_time(&tm, "%c");
if (ret != last)
{
std::cout << " Remaining content: ";
std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
}
else
{
std::cout << " The input was fully consumed";
}
}
else
{
std::cout << "Parse failed. Unparsed string: ";
std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
}
std::cout << std::endl;
}
return 0;
}
输出
The locale de-DE_phoneb Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale es-ES_tradnl Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale hu-HU_technl Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale ja-JP_radstr Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale ka-GE_modern Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale x-IV_mathan Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-CN_phoneb Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-CN_stroke Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-HK_radstr Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-MO_radstr Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-MO_stroke Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-SG_phoneb Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-SG_stroke Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-TW_pronun Successfully parsed as 01/01/24 23:12:34 The input was fully consumed
The locale zh-TW_radstr Successfully parsed as 01/01/24 23:12:34 The input was fully consumed