在线文档教程
C++
输入/输出 | Input/output

std::get_time

STD:得到[医]时间

Defined in header
template< class CharT > /*unspecified*/ get_time( std::tm* tmb, const CharT* fmt(since C++11)

在表达式中使用时in >> get_time(tmb, fmt),根据格式字符串将字符输入解析为日期/时间值。fmt根据std::time_get当前注入输入流的区域设置的方面。in结果值存储在std::tm对象所指向的tmb...

参数

tmb-valid pointer to the std::tm object where the result will be stored
fmt-pointer to a null-terminated CharT string specifying the conversion format The format string consists of zero or more conversion specifiers, whitespace characters, and ordinary characters (except %). Each ordinary character is expected to match one character in the input stream in case-insensitive comparison. Each whitespace character matches arbitrary whitespace in the input string. Each conversion specification begins with % character, optionally followed by E or O modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The format specifiers match the POSIX function strptime(): Conversion specifier Explanation Writes to fields % matches a literal %. The full conversion specification must be %%. (none) t matches any whitespace. (none) n matches any whitespace. (none) Year Y parses full year as a 4 digit decimal number, leading zeroes permitted but not required tm_year EY parses year in the alternative representation, e.g.平成23年 (year Heisei 23) which writes 2011 to tm_year in ja_JP locale tm_year y parses last 2 digits of year as a decimal number. Range 69,99 results in values 1969 to 1999, range 00,68 results in 2000-2068 tm_year Oy parses last 2 digits of year using the alternative numeric system, e.g. 十一 is parsed as 11 in ja_JP locale tm_year Ey parses year as offset from locale's alternative calendar period %EC tm_year C parses the first 2 digits of year as a decimal number (range 00,99) tm_year EC parses the name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP tm_year Month b parses the month name, either full or abbreviated, e.g. Oct tm_mon h synonym of b tm_mon B synonym of b tm_mon m parses the month as a decimal number (range 01,12), leading zeroes permitted but not required tm_mon Om parses the month using the alternative numeric system, e.g. 十二 parses as 12 in ja_JP locale tm_mon Week U parses the week of the year as a decimal number (Sunday is the first day of the week) (range 00,53), leading zeroes permitted but not required tm_year, tm_wday, tm_yday OU parses the week of the year, as by %U, using the alternative numeric system, e.g. 五十二 parses as 52 in ja_JP locale tm_year, tm_wday, tm_yday W parses the week of the year as a decimal number (Monday is the first day of the week) (range 00,53), leading zeroes permitted but not required tm_year, tm_wday, tm_yday OW parses the week of the year, as by %W, using the alternative numeric system, e.g. 五十二 parses as 52 in ja_JP locale tm_year, tm_wday, tm_yday Day of the year/month j parses day of the year as a decimal number (range 001,366), leading zeroes permitted but not required tm_yday d parses the day of the month as a decimal number (range 01,31), leading zeroes permitted but not required tm_mday Od parses the day of the month using the alternative numeric system, e.g 二十七 parses as 27 in ja_JP locale, leading zeroes permitted but not required tm_mday e synonym of d tm_mday Oe synonym of Od tm_mday Day of the week a parses the name of the day of the week, either full or abbreviated, e.g. Fri tm_wday A synonym of a tm_wday w parses weekday as a decimal number, where Sunday is 0 (range 0-6) tm_wday Ow parses weekday as a decimal number, where Sunday is 0, using the alternative numeric system, e.g. 二 parses as 2 in ja_JP locale tm_wday Hour, minute, second H parses the hour as a decimal number, 24 hour clock (range 00-23), leading zeroes permitted but not required tm_hour OH parses hour from 24-hour clock using the alternative numeric system, e.g. 十八 parses as 18 in ja_JP locale tm_hour I parses hour as a decimal number, 12 hour clock (range 01,12), leading zeroes permitted but not required tm_hour OI parses hour from 12-hour clock using the alternative numeric system, e.g. 六 reads as 06 in ja_JP locale tm_hour M parses minute as a decimal number (range 00,59), leading zeroes permitted but not required tm_min OM parses minute using the alternative numeric system, e.g. 二十五 parses as 25 in ja_JP locale tm_min S parses second as a decimal number (range 00,60), leading zeroes permitted but not required tm_sec OS parses second using the alternative numeric system, e.g. 二十四 parses as 24 in ja_JP locale tm_sec Other c parses the locale's standard date and time string format, e.g. Sun Oct 17 04:41:13 2010 (locale dependent) all Ec parses the locale's alternative date and time string format, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale all x parses the locale's standard date representation all Ex parses the locale's alternative date representation, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale all X parses the locale's standard time representation all EX parses the locale's alternative time representation all D equivalent to "%m / %d / %y " tm_mon, tm_mday, tm_year r parses locale's standard 12-hour clock time (in POSIX, "%I : %M : %S %p") tm_hour, tm_min, tm_sec R equivalent to "%H : %M" tm_hour, tm_min T equivalent to "%H : %M : %S" tm_hour, tm_min, tm_sec p parses the locale's equivalent of a.m. or p.m. tm_hour Note: tm_isdst is not written to, and needs to be set explicitly for use with functions such as mktime.
Conversion specifierExplanationWrites to fields
%matches a literal %. The full conversion specification must be %%.(none)
tmatches any whitespace.(none)
nmatches any whitespace.(none)

y将全年解析为4位十进制数,允许使用前导零,但不需要tm。[医]年

EY在可选表示中解析年份,例如平成23年%28 Heisei 23%29,其中写入2011到tm。[医]在日本的年份[医]JP地区tm[医]年

y将年的最后2位数字解析为十进制数。范围69,99结果值为1969至1999,范围00682000业绩-2068 tm[医]年

Oy使用替代数字系统分析一年中的最后2位数字,例如,在ja中,十一被解析为11。[医]JP地区tm[医]年

安永将年份与地区%27s替代日历期%ectm相抵。[医]年

C将年的前2位数字解析为十进制数%28范围。0099%29 tm[医]年

EC在地区%27 s替代表示中解析基准年%28周期%29的名称,例如平成%28 Heisei era%29在ja中。[医]JP tm[医]年

B解析月份名称,无论是完整的还是缩写的,例如oct tm。[医]蒙

h同义词b tm[医]蒙

b的同义词b tm[医]蒙

M将月份解析为十进制数%28范围01,12%29,前导零允许但不需要tm[医]蒙

OM使用替代的数字系统解析月份,例如在ja中将十二解析为12。[医]JP地区tm[医]蒙

将一年中的一周解析为十进制数%28星期日是一周的第一天%29%28范围0053%29,前导零允许但不需要tm[医]年份,tm[医]WDAY,tm[医]日

以%U的方式分析一年中的每周,使用替代的数字系统,例如,在ja中五十二解析为52[医]JP地区tm[医]年份,tm[医]WDAY,tm[医]日

将一年中的一周解析为十进制数%28星期一是一周的第一天%29%28范围。0053%29,前导零允许但不需要tm[医]年份,tm[医]WDAY,tm[医]日

现在使用替代数字系统解析一年中的每周,如%W,例如,在ja中五十二解析为52[医]JP地区tm[医]年份,tm[医]WDAY,tm[医]日

年度/月份的日期

J将一年中的一天解析为十进制数%28范围001366%29,前导零允许但不需要tm[医]日

D将月份中的一天解析为十进制数%28范围。01,31%29,前导零允许但不需要tm[医]马日

OD使用可选的数字系统解析一个月中的一天,例如,在ja中,二十七解析为27。[医]jp地区,前导零允许但不需要tm[医]马日

d tm的同义词[医]马日

OE同义词Od tm[医]马日

一周中的一天

A解析一周中的一天的名称,或者是完整的,或者是缩写的,例如Fri tm。[医]WDAY

tm的同义词[医]WDAY

W将工作日解析为十进制数,其中星期日为0%28范围。0-6%29 tm[医]WDAY

现在将工作日解析为十进制数,其中星期日为0,使用可选的数字系统,例如,在ja中,二解析为2。[医]JP地区tm[医]WDAY

小时,分钟,秒

h将小时解析为十进制数,24小时时钟%28范围00-23%29,前导零允许但不需要tm[医]小时

哦,使用替代的数字系统从24小时的时钟分析小时,例如在ja中十八解析为18。[医]JP地区tm[医]小时

我将小时分析为十进制数,12小时时钟%28范围01,12%29,前导零允许但不需要tm[医]小时

Oi使用可选的数字系统从12小时时钟分析小时,例如,在ja中,六读取为06。[医]JP地区tm[医]小时

M将分钟解析为十进制数%28范围0059%29,前导零允许但不需要tm[医]敏

OM使用替代的数字系统解析分钟,例如在ja中使用二十五解析为25[医]JP地区tm[医]敏

s将秒解析为十进制数%28范围0060%29,前导零允许但不需要tm[医]证交会

操作系统使用替代数字系统进行第二次分析,例如在ja中二十四解析为24[医]JP地区tm[医]证交会

其他

C解析区域设置%27s标准日期和时间字符串格式,例如太阳10月17日04:41:13 2010%28 locale依赖于%29 all

EC分析地区%27s的替代日期和时间字符串格式,例如期望平成23年%28a Heisei 23%29而不是在ja中的2011年%28 28 2011%29。[医]JP地区所有

X解析区域设置%27s标准日期表示所有

例如,期望平成23年%28a Heisei 23%29而不是2011年%28a 2011%29在ja[医]JP地区所有

X解析地区%27s标准时间表示全部

ex解析区域设置%27的可选时间表示ALL

d等于“%m/%d/%y”tm[医]星期一,tm[医]mday,tm[医]年

r解析POSIX中标准的12小时时钟时间%28,“%i:%M:%S%p”%29 tm[医]小时,小时[医]min,tm[医]证交会

r等于“%H:%M”tm[医]小时,小时[医]敏

t等于“%H:%M:%S”tm[医]小时,小时[医]min,tm[医]证交会

P解析相当于上午或下午tm的地区%27s。[医]小时

返回值

返回未指定类型的对象,以便在in类型的输入流的名称。std::basic_istream<CharT, Traits>,然后表达in >> get_time(tmb, fmt)行为就像执行了以下代码:

typedefstd::istreambuf_iterator<CharT, Traits> Iter;

typedefstd::time_get<CharT, Iter> TimeGet;

std::ios_base::iostateerr =std::ios_base::goodbit;

const TimeGet& tg =std::use_facet<TimeGet>(in.getloc()

tg.get(Iter(in.rdbuf()), Iter(), in, err, tmb, fmt, fmt + traits::length(fmt)

if(err !=std::ios_base::goodbit)

in.setstate(err

注记

std::time_get::do_get,该函数调用的值为%27s,如果此函数从*tmb中出现的转换说明符没有直接设置的fmt*可移植程序应初始化*tmb呼叫前为零std::get_time...

二次

#include <iostream> #include <sstream> #include <locale> #include <iomanip> 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

二次

另见

time_getparses time/date values from an input character sequence into struct std::tm (class template)
put_time (C++11)formats and outputs a date/time value according to the specified format (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/manip/get[医]时间