std::get_time
來自 cppreference.com
定義於標頭檔案 <iomanip> |
||
template< class CharT > /*未指定*/ get_time( std::tm* tmb, const CharT* fmt ); |
(C++11 起) | |
當在表示式 in >> get_time(tmb, fmt) 中使用時,根據輸入流 in 中當前注入的區域設定的 std::time_get 面,將字元輸入解析為符合格式字串 fmt 的日期/時間值。結果值儲存在由 tmb 指向的 std::tm 物件中。
目錄 |
[編輯] 引數
tmb | - | 指向 std::tm 物件的有效指標,結果將儲存在此處 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fmt | - | 指向 null 終止的 CharT 字串的指標,指定轉換格式格式字串由零個或多個轉換說明符、空白字元和普通字元(除了
注意:
|
[編輯] 返回值
一個未指定型別的物件,使得
- 如果 in 是 std::basic_istream<CharT, Traits> 型別,則表示式 in >> get_time(tmb, fmt)
- 型別為 std::basic_istream<CharT, Traits>&
- 值為 in
- 其行為如同呼叫了 f(in, tmb, fmt)
其中函式 f 定義為
template<class CharT, class Traits> void f(std::basic_ios<CharT, Traits>& str, std::tm* tmb, const CharT* fmt) { using Iter = std::istreambuf_iterator<CharT, Traits>; using TimeGet = time_get<CharT, Iter>; std::ios_base::iostate err = std::ios_base::goodbit; const TimeGet& tg = std::use_facet<TimeGet>(str.getloc()); tg.get(Iter(str.rdbuf()), Iter(), str, err, tmb, fmt, fmt + Traits::length(fmt)); if (err != std::ios_base::goodbit) str.setstate(err); }
[編輯] 注意
根據此函式呼叫的 std::time_get::do_get 中指定,此函式是否將 *tmb 中未由 fmt 中出現的轉換說明符直接設定的欄位清零是未指定的:可移植程式應在呼叫 std::get_time
之前將 *tmb 的每個欄位初始化為零。
[編輯] 示例
注意:選擇 clang 或 gcc >= 12.1 以觀察輸出。12.1 之前的 libstdc++ 沒有正確實現 %b 說明符:bug #78714。
執行此程式碼
#include <iomanip> #include <iostream> #include <locale> #include <sstream> int main() { std::tm t = {}; std::istringstream ss("2011-Februar-18 23:12:34"); ss.imbue(std::locale("de_DE.utf-8")); ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S"); if (ss.fail()) std::cout << "Parse failed\n"; else std::cout << std::put_time(&t, "%c") << '\n'; }
可能的輸出
Sun Feb 18 23:12:34 2011
[編輯] 參閱
從輸入字元序列解析時間/日期值到 std::tm (類模板) | |
(C++11) |
根據指定格式格式化並輸出日期/時間值 (函式模板) |
(C++20) |
從流解析 chrono 物件(函式模板) |