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

相关文章

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题

linux环境openssl、openssh升级流程

《linux环境openssl、openssh升级流程》该文章详细介绍了在Ubuntu22.04系统上升级OpenSSL和OpenSSH的方法,首先,升级OpenSSL的步骤包括下载最新版本、安装编译... 目录一.升级openssl1.官网下载最新版openssl2.安装编译环境3.下载后解压安装4.备份

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定