iter_type get_time(iter_type s, iter_type end, ios_base& str,
ios_base::iostate& err, tm* t) const; // (1) C++98
概要
入力イテレータ範囲[s, end)から時刻を解析する。解析結果は*tの対応するメンバへ格納される。
戻り値
do_get_time(s, end, str, err, t)
例
#include <iostream>
#include <sstream>
#include <locale>
#include <iterator>
#include <ctime>
#include <stdexcept>
int main()
{
const char* names[] = {"C", "en_US.UTF-8", "ja_JP.UTF-8", "de_DE.UTF-8"};
for (int i = 0; i < 4; ++i) {
try {
std::istringstream iss{"13:05:30"};
iss.imbue(std::locale{names[i]});
const auto& facet = std::use_facet<std::time_get<char>>(iss.getloc());
std::tm t{};
std::ios_base::iostate err = std::ios_base::goodbit;
facet.get_time(std::istreambuf_iterator<char>{iss},
std::istreambuf_iterator<char>{},
iss, err, &t);
std::cout << names[i] << " : 13:05:30 -> " << t.tm_hour << ':' << t.tm_min << ':' << t.tm_sec << std::endl;
}
catch (const std::runtime_error&) {
// 指定した名前のロケールが利用できない場合
std::cout << names[i] << " : not available" << std::endl;
}
}
}
出力例
C : 13:05:30 -> 13:5:30
en_US.UTF-8 : 13:05:30 -> 13:5:30
ja_JP.UTF-8 : 13:05:30 -> 13:5:30
de_DE.UTF-8 : 13:05:30 -> 13:5:30
- 時刻の書式は規格により
"%H:%M:%S"と定められているため、いずれのロケールでも同じ結果となる - 妥当なロケール名は処理系定義である。指定した名前のロケールが利用できない場合、
std::localeのコンストラクタはstd::runtime_errorを送出し、上記の例ではnot availableが出力される
バージョン
言語
- C++98