c++11 标准模板(STL)本地化库 - 平面类别 (std::time_put_byname)表示系统提供的具名本地环境的 std::time_put

本文主要是介绍c++11 标准模板(STL)本地化库 - 平面类别 (std::time_put_byname)表示系统提供的具名本地环境的 std::time_put,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

表示系统提供的具名本地环境的 std::time_put

std::time_put_byname

template< class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
class time_put_byname : public std::time_put<CharT, OutputIterator>;

std::time_put_byname 是 std::time_put 平面,封装在其构造时指定的 locale 的日期和时间格式化规则。

标准库提供二个特化

定义于头文件 <locale>

std::time_put_byname<char, OutputIterator>窄/多字节时间格式化
std::time_put_byname<wchar_t, OutputIterator>宽字符串时间格式化

成员类型

成员类型定义
char_typeCharT
iter_typeOutputIterator

成员函数

(构造函数)

构造新的 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:
~time_put_byname();

销毁平面。

继承自 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:

这篇关于c++11 标准模板(STL)本地化库 - 平面类别 (std::time_put_byname)表示系统提供的具名本地环境的 std::time_put的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/998166

相关文章

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col