public member function
<locale>

std::time_get::get_date

iter_type get_date (iter_type s, iter_type end, ios_base& str,                    ios_base::iostate& err, tm* t) const;
讀取日期
解析 send 之間的字元序列以獲取日期序列,並將其值儲存到 t 指向的 tm 物件中。

該函式讀取字元,直到讀取的字元不能構成有效的日期序列表達式,或者到達 end。序列中的下一個字元由函式返回的迭代器指向。

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

在內部,此函式僅呼叫虛保護成員 do_get_date,該成員預設按照與 strftime 中的 "%x" 相同的格式解析字元。

必要時,該函式會更新 err 以反映操作結果。
- 如果序列格式無效,函式將設定 failbit
- 否則,這是未定義的,儘管某些實現會將 err 設定為 eofbit(如果函式耗盡了字元序列,即無論成功還是失敗都到達了 end),或者設定為 goodbit
在內部,此函式僅呼叫虛保護成員 do_get_date,該成員預設按照與 date_order 成員的返回值相關的格式進行解析。
date_order()get_date 的格式
no_orderstrftime 中的 "%m%d%y"
dmystrftime 中的 "%d%m%y"
mdystrftime 中的 "%m%d%y"
ymdstrftime 中的 "%y%m%d"
ydmstrftime 中的 "%y%d%m"

必要時,該函式會更新 err 以反映操作結果。
- 如果序列格式無效,函式將設定 failbit
- 如果函式在操作過程中耗盡了字元序列(即到達 end),則會設定 eofbit(單個操作可能會同時設定 failbiteofbit)。
- 否則,行為是未定義的,儘管某些實現會將 err 設定為 goodbit

引數

s, end
指向序列開始和結束字元的迭代器。範圍是 [s,end),其中包含 s 指向的字元和 end 指向的字元之間的所有字元,但不包括 end 指向的字元。
成員型別 iter_type 是該 facet 的迭代器型別(定義為 time_get 的第二個模板引數 InputIterator 的別名)。預設情況下,這是一個 istreambuf_iterator,允許從 basic_istream 物件隱式轉換。
str
派生自 ios_base 的類物件(通常是輸入流物件)。它僅用於獲取格式資訊。
err
流錯誤狀態物件,型別為 ios_base::iostate,用於儲存錯誤狀態。
t
指向 struct tm (在標頭檔案 <ctime> 中定義)的指標,其成員在成功呼叫此成員函式後會被設定。

返回值

在操作之後,序列中緊隨最後一個被使用字元的下一個字元。
成員型別 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
// time_get::get_date example
#include <iostream>       // std::cout, std::ios
#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:
  const std::time_get<char>& tmget = std::use_facet <std::time_get<char> > (loc);

  std::ios::iostate state;
  std::istringstream iss ("01/02/03");
  std::tm when;

  tmget.get_date (iss, std::time_get<char>::iter_type(), iss, state, &when);

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

輸出

year: 3
month: 0
day: 2

請注意,在經典區域設定中,日期順序mdyno_order,而在 tmtm_mon 成員中,第一個月(一月)的值始終為 0tm_year 的值為 3 表示 1903 年(2003 年表示為 103)。

資料競爭

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

異常安全

如果丟擲異常,*facet 物件*不會發生任何更改,儘管某些引數可能會受到影響。

另見