Namespaces
Variants

std:: time_put_byname

From cppreference.net
定义于头文件 <locale>
template <

class CharT,
class OutputIt = std:: ostreambuf_iterator < CharT >

> class time_put_byname : public std:: time_put < CharT, OutputIt >

std::time_put_byname 是一个 std::time_put facet,它封装了在其构造时指定的区域设置的时间与日期格式化规则。

目录

特化

标准库保证提供满足以下类型要求的每个特化:

嵌套类型

类型 定义
char_type CharT
iter_type OutputIt

成员函数

(constructor)
构造新的 time_put_byname facet
(公开成员函数)
(destructor)
销毁 time_put_byname facet
(受保护成员函数)

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 ) ;
(since C++11)

为具有 name 的区域设置构造新的 std::time_put_byname facet。

refs 用于资源管理:如果 refs == 0 ,当最后一个持有该 facet 的 std::locale 对象被销毁时,实现会销毁该 facet。否则,该对象不会被销毁。

参数

name - 区域设置的名称
refs - 链接到该 facet 的引用数量

std::time_put_byname:: ~time_put_byname

protected :
~time_put_byname ( ) ;

销毁该facet。

继承自 std:: time_put

数据成员

成员 说明
std::locale::id id [static] facet 的标识符

成员函数

调用 do_put
( std::time_put<CharT,OutputIt> 的公开成员函数)

受保护成员函数

[virtual]
格式化日期/时间并写入输出流
( std::time_put<CharT,OutputIt> 的虚受保护成员函数)

示例

使用"C"区域设置,并通过不同的 std::time_put_byname facet替换 time_put facet来打印当前时间。所示结果是使用clang编译器获得的。

#include <codecvt>
#include <ctime>
#include <iomanip>
#include <iostream>
int main()
{
    std::time_t t = std::time(nullptr);
    std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv(std::cout.rdbuf());
    std::wostream out(&conv);
    out.imbue(std::locale(out.getloc(),
                          new std::time_put_byname<wchar_t>("ja_JP.utf8")));
    out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
    out.imbue(std::locale(out.getloc(),
                          new std::time_put_byname<wchar_t>("ru_RU.utf8")));
    out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
    out.imbue(std::locale(out.getloc(),
                          new std::time_put_byname<wchar_t>("sv_SE.utf8")));
    out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
}

可能的输出:

木曜日 2023年10月05日 19時44分51秒
Четверг Чт 05 окт 2023 19:44:51
torsdag tor  5 okt 2023 19:44:51

参见

std::tm 的内容格式化为字符序列输出
(类模板)