名稱空間
變體
操作

std::time_put_byname

來自 cppreference.com
< cpp‎ | locale
 
 
 
 
定義於標頭檔案 <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

[編輯] 成員函式

(建構函式)
構造一個新的 time_put_byname facet
(公有成員函式) [編輯]
(解構函式)
銷燬一個 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 );
(C++11 起)

構造一個用於具有 name 的區域設定的新 std::time_put_byname facet。

refs 用於資源管理:如果 refs == 0,則當最後一個持有它的 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> 的公有成員函式) [編輯]

受保護的成員函式

[虛擬函式]
格式化日期/時間並寫入輸出流
(std::time_put<CharT,OutputIt> 的虛保護成員函式) [編輯]

[編輯] 示例

使用“C”區域設定列印當前時間,其中 time_put facet 被各種 std::time_put_byname 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 的內容格式化為字元序列輸出
(類模板) [編輯]