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

相关文章

DeepSeek模型本地部署的详细教程

《DeepSeek模型本地部署的详细教程》DeepSeek作为一款开源且性能强大的大语言模型,提供了灵活的本地部署方案,让用户能够在本地环境中高效运行模型,同时保护数据隐私,在本地成功部署DeepSe... 目录一、环境准备(一)硬件需求(二)软件依赖二、安装Ollama三、下载并部署DeepSeek模型选

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

2.1/5.1和7.1声道系统有什么区别? 音频声道的专业知识科普

《2.1/5.1和7.1声道系统有什么区别?音频声道的专业知识科普》当设置环绕声系统时,会遇到2.1、5.1、7.1、7.1.2、9.1等数字,当一遍又一遍地看到它们时,可能想知道它们是什... 想要把智能电视自带的音响升级成专业级的家庭影院系统吗?那么你将面临一个重要的选择——使用 2.1、5.1 还是

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j