public member function
<locale>

std::time_get::get

(1)
iter_type get (iter_type s, iter_type end, ios_base& str,    ios_base::iostate& err, tm* t, char format, char modifier=0) const;
(2)
iter_type get (iter_type s, iter_type end, ios_base& str,    ios_base::iostate& err, tm* t, const char_type* fmt_begin, const char_type* fmt_end) const;
讀取時間和日期
解析 send 之間的字元序列,以 format(或 fmt_beginfmt_end 之間的序列)指定的格式解析時間和/或日期,並將獲取的值儲存在 t 指向的 tm 物件中。

該函式讀取字元,直到讀取的字元無法構成有效格式序列或 end 被到達。函式返回的迭代器指向最後一個被處理字元之後的下一個字元。

如果成功,該函式將設定 tm 結構體 t 的相關成員。其餘成員將保持不變。

(1) 單個格式說明符
此版本僅呼叫虛保護成員函式 do_get,該函式預設解析序列,其格式與 strftime 生成的格式相同,由百分號('%')後跟引數 *format* 構成字串,並可選地在兩者之間插入 modifier(當不等於零時)。
(2) 格式字串
此版本按順序讀取範圍 [fmt_begin,fmt_end) 中的字元,並以 scanf 處理其 *format string* 的方式來解釋它們,但不同之處在於,函式識別的 *format specifiers* 是 strftime 使用的。對於每個被識別為 strftime 說明符的字元序列,都會呼叫虛保護成員函式 do_get,並傳入適當的引數。
該函式使用 str 的 locale 的 ctype 方面的屬性來確定空格字元。

一個 ios_base::iostate 位掩碼值將儲存在 err 中,表示操作的結果。
err 中的值描述
goodbit成功:整個序列 [fmt_begin,fmt_end) 已被處理,未到達 end
failbit失敗:序列與預期的格式不匹配。
eofbit已到達 end
注意:如果發生在 fmt_end 前一個字元被處理之前,函式將同時設定 failbiteofbit

引數

s, end
指向序列開始和結束字元的迭代器。使用的範圍是 [s,end),包含 s 指向的字元到 end 指向的字元之間的所有字元,但 end 指向的字元不包含在內。
成員型別 iter_type 是該方面的迭代器型別(定義為 time_get 的第二個模板引數 InputIterator 的別名)。預設情況下,這是一個 istreambuf_iterator,允許從 basic_istream 物件隱式轉換。
str
派生自 ios_base 的類物件(通常是輸入流物件)。它僅用於獲取格式資訊。
err
流錯誤狀態物件,型別為 ios_base::iostate,用於儲存錯誤狀態。
t
指向 struct tm (在標頭檔案 <ctime> 中定義)的指標,其成員在成功呼叫此成員函式後會被設定。
format
格式說明符,指示序列的有效格式;對於 do_get 的預設實現,這應該是 strftime 接受的說明符之一。
modifier
某些實現允許對說明符使用格式修飾符。
值為 0'\0')被解釋為無修飾符。
fmt_begin, fmt_end
指向構成函式 *format string* 的序列的開始和結束字元的指標。
成員型別 char_type 是該方面的字元型別(定義為 time_get 的第一個模板引數 charT 的別名)。
請注意,對於這兩個範圍,*空字元*(如果存在)也被視為有效,並且函式會繼續處理它們,直到發生失敗或其中一個範圍被耗盡。

返回值

在操作使用的最後一個字元之後的序列 [s,end) 中的下一個字元。
成員型別 iter_type 是此 facet 的迭代器型別(定義為 time_get 的第二個模板引數 InputIterator 的別名)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// time_get::get example
#include <iostream>       // std::cout, std::ios
#include <string>         // std::string
#include <sstream>        // std::istringstream
#include <ctime>          // std::tm
#include <locale>         // std::locale, std::time_get, std::use_facet

int main ()
{
  std::locale loc;        // classic "C" locale

  // get time_get facet:
  auto& tmget = std::use_facet <std::time_get<char> > (loc);

  std::ios::iostate state;
  std::istringstream iss ("year:2013 month:09 day:10");
  std::string format ("year:%Y month:%m day:%d");
  std::tm when;

  tmget.get (iss, std::time_get<char>::iter_type(), iss, state, &when,
             format.data(), format.data()+format.length() );

  std::cout << "year: " << when.tm_year << '\n';
  std::cout << "mon: " << when.tm_mon << '\n';
  std::cout << "mday: " << when.tm_mday << '\n';
  return 0;
}

輸出

year: 113
mon: 8
mday: 10


資料競爭

該物件以及 send 之間以及 fmt_beginfmt_end 之間的整個範圍都將被訪問。
引數 strerrt 可能會被修改。

異常安全

如果丟擲異常,*方面物件* 不會有任何更改,儘管某些引數可能已被影響。

另見