本地化库
本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。
平面类别
表示系统提供的具名本地环境的 std::time_put
std::time_put_byname
template< class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> > |
std::time_put_byname 是 std::time_put 平面,封装在其构造时指定的 locale 的日期和时间格式化规则。
标准库提供二个特化
定义于头文件 | |
std::time_put_byname<char, OutputIterator> | 窄/多字节时间格式化 |
std::time_put_byname<wchar_t, OutputIterator> | 宽字符串时间格式化 |
成员类型
成员类型 | 定义 |
char_type | CharT |
iter_type | OutputIterator |
成员函数
(构造函数) | 构造新的 time_put_byname 平面(公开成员函数) |
(析构函数) | 析构 time_put_byname 平面(受保护成员函数) |
std::time_put_byname::time_put_byname
explicit time_put_byname( const char* name, std::size_t refs = 0 ); | ||
explicit time_put_byname( const std::string& name, std::size_t refs = 0 ); | (C++11 起) |
为名为 name
的本地环境构造新的 std::time_put_byname
平面。
refs
用于资源管理:在销毁最后一个保有平面的 std::locale 对象时,若 refs == 0 ,则实现销毁平面对象。否则,不销毁对象。
参数
name | - | 本地环境的名称 |
refs | - | 链接到该平面的引用数 |
std::time_put_byname::~time_put_byname
protected: |
销毁平面。
继承自 std::time_put
成员对象
成员名 | 类型 |
id [静态] | std::locale::id |
成员函数
put | 调用 do_put ( std::time_put<CharT,OutputIt> 的公开成员函数) |
受保护成员函数
do_put [虚] | 格式化日期/时间并写入输出流 ( std::time_put<CharT,OutputIt> 的虚受保护成员函数) |
调用示例 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_time_put(const std::tm* tm, const std::string& strfmt)
{
std::cout.imbue(std::locale());
std::cout << "In the locale '" << std::cout.getloc().name() << "' : '";
std::use_facet<std::time_put_byname<char>>(std::cout.getloc()).put(
{std::cout}, std::cout, ' ', tm, &strfmt[0], &strfmt[0] + strfmt.size());
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::time_t time_t = std::time(NULL);
std::tm tm = *std::localtime(&time_t);
std::string strfmt = "%c";
std::cout << "Using the format string '" << strfmt
<< "' to format the time: " << std::ctime(&time_t);
std::locale::global(std::locale(stows(*str)));
try_time_put(&tm, strfmt);
}
return 0;
}
输出
Using the format string '%c' to format the time: Sun May 19 20:30:56 2024
In the locale 'de-DE_phoneb' : '19.05.2024 20:30:56
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'es-ES_tradnl' : '19/05/2024 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'hu-HU_technl' : '2024. 05. 19. 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'ja-JP_radstr' : '2024/05/19 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'ka-GE_modern' : '19.05.2024 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'x-IV_mathan' : '05/19/2024 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-CN_phoneb' : '2024/5/19 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-CN_stroke' : '2024/5/19 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-HK_radstr' : '19/5/2024 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-MO_radstr' : '19/5/2024 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-MO_stroke' : '19/5/2024 20:30:57
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-SG_phoneb' : '19/5/2024 下午 8:30:
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-SG_stroke' : '19/5/2024 下午 8:30:
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-TW_pronun' : '2024/5/19 下午 08:30:
Using the format string '%c' to format the time: Sun May 19 20:30:57 2024
In the locale 'zh-TW_radstr' : '2024/5/19 下午 08:30: