首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::time_get::get

Defined in header <locale>

public: iter_type get(iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t, const char_type* fmtbeg, const char_type* fmtend) const;

(1)

(since C++11)

protected: virtual iter_type do_get(iter_type neg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm *t, char format, char modifier) const;

(2)

(since C++11)

1%29分析输入字符序列中的日期和时间。[beg, end)根据字符序列中提供的格式[fmtbeg, fmtend)预计该格式将遵循下面描述的格式,尽管可以通过重写来定制每个格式说明符的实际处理。do_get...get函数执行以下操作:首先,清除err通过执行err =std::ios_base::goodbit然后进入一个循环,当下列条件变为真时,该循环将终止%28,按此顺序检查为%29:

%29所有字符都已从格式字符串%28中读取fmtbeg == fmtend%29

B%29解析错误%28err !=std::ios_base::goodbit%29

C%29所有字符都已从输入序列%28中读取beg == end如果此条件终止循环,则该函数将同时设置eofbitfailbiterr

在循环的主体中,执行以下步骤:

如果格式字符串中的下一个字符是'%',后面跟着一个或两个构成有效字符的字符。std::get_time转换说明符%28参见%29下面,这些字符用于调用do_get(beg, end, str, err, t, format, modifier),在哪里format主要转换说明符字符,以及modifier是可选修饰符%28,它出现在%和格式字符,如果存在%29。如果没有修饰符,则值'\0'被使用了。如果格式字符串模棱两可或过早结束,则无法在之后确定转换说明符。'%',,,eofbit设为err循环被终止。如果,在打电话到do_get中没有设置错误位。err,函数递增fmtbeg在转换说明符之后直接指向并继续循环。

B%29如果下一个字符是空格,由流中提供的区域设置指示str%28i.e.std::isspace(*fmtbeg, str.getloc()) == true,该函数不断递增。fmtbeg直到它变成等于fmtend或指向非空白字符。

c%29如果根据大小写不敏感的比较,格式字符串中的下一个字符与输入流中的下一个字符等效,则函数将两个序列推进一个字符。++fmtbeg, ++beg;并继续循环,否则,它将设置failbiterr...

2%29解析输入序列中的一个转换说明符。[beg, end)并更新std::tm结构指向t因此。

首先,清除err通过执行err =std::ios_base::goodbit然后从输入序列中读取字符。[beg, end)预期的std::time_get格式说明符'%',,,modifier%28如果没有'\0'%29,和format如果这些字符未组合成有效的转换说明符,则设置failbiterr如果读取字符后到达输入流的末尾,则设置eofbiterr如果成功地解析了输入字符串,则更新*t对于复杂的转换说明符,如'%x''%c',或者使用修饰符的指令。'E''O',函数可能无法确定要存储在其中的一些值。*t在这种情况下,eofbiterr并使这些字段处于未指定的状态。

参数

beg

-

iterator designating the start of the sequence to parse

end

-

one past the end iterator for the sequence to parse

str

-

a stream object that this function uses to obtain locale facets when needed, e.g. std::ctype to skip whitespace or std::collate to compare strings

err

-

stream error flags object that is modified by this function to indicate errors

t

-

pointer to the std::tm object that will hold the result of this function call

fmtbeg

-

pointer to the first character of a sequence of char_type characters 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 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

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)

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[医]证交会

其他

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。[医]小时

指示字符序列的最后一个字符的指针。[医]指定转换格式的字符类型

格式-命名转换说明符的字符。

修饰符-在%和转换说明符之间可能出现的可选修饰符

返回值

中的最后一个字符[beg, end)分析得很成功。

注记

不区分大小写的非空白非-'%'格式字符串中的字符,则std::collate提供的区域设置的方面。str通常(但不一定)使用。

如果遇到解析错误,则此函数的许多实现将离开。*t完全不动。

如果这些函数在*t可移植程序应该在调用之前将每个字段初始化为零。get()...

二次

代码语言:javascript
复制
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
 
int main()
{
    std::istringstream ss("2011-Februar-18 23:12:34");
    ss.imbue(std::locale("de_DE.utf8"));
 
    auto& f = std::use_facet<std::time_get<char>>(std::locale("de_DE.utf8"));
    std::tm t{};
    std::string s = "%Y-%b-%d %H:%M:%S";
    std::ios_base::iostate err = std::ios_base::goodbit;
    std::istreambuf_iterator<char> ret = f.get(std::istreambuf_iterator<char>(ss),
                                               std::istreambuf_iterator<char>(),
                                               ss, err, &t, &s[0], &s[0] + s.size());
    ss.setstate(err);
 
    if(ss) {
        std::cout << "Successfully parsed as " << std::put_time(&t, "%c");
        if(ret != std::istreambuf_iterator<char>()) {
            std::cout << " Remaining content: ";
            std::copy(ret, std::istreambuf_iterator<char>(),
                           std::ostreambuf_iterator<char>(std::cout));
        } else {
                std::cout << " The input was fully consumed";
        }
    } else {
            std::cout << "Parse failed. Unparsed string: ";
            std::copy(ret, std::istreambuf_iterator<char>(),
                           std::ostreambuf_iterator<char>(std::cout));
    }
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Successfully parsed, as Sun Feb 18 23:12:34 2011 The input was fully consumed

二次

另见

get_time (C++11)

parses a date/time value of specified format (function template)

代码语言:txt
复制
 © cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券