命名空间
变体
操作

std::chrono::from_stream (std::chrono::tai_time)

来自 cppreference.cn
< cpp‎ | chrono‎ | tai clock
 
 
 
 
template< class CharT, class Traits, class Duration, class Alloc = std::allocator<CharT> >

std::basic_istream<CharT, Traits>&
    from_stream( std::basic_istream<CharT, Traits>& is, const CharT* fmt,
                 std::chrono::tai_time<Duration>& tp,
                 std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,

                 std::chrono::minutes* offset = nullptr );
(since C++20) (C++20 起)

Attempts to parse the input stream is into the time point tp according to the format string fmt. 尝试根据格式字符串 fmt 将输入流 is 解析为时间点 tp

Behaves as an UnformattedInputFunction, except that it has an unspecified effect on is.gcount(). 行为类似于 UnformattedInputFunction,但它对 is.gcount() 具有未指定的副作用。After constructing and checking the sentry object, attempts to parse the input stream is into tp according to the format string fmt. 在构造并检查哨兵对象之后,尝试根据格式字符串 fmt 将输入流 is 解析为 tp。If the parse fails to decode a valid time point, is.setstate(std::ios_base::failbit) is called and tp is not modified. 如果解析未能解码有效的时间点,则调用 is.setstate(std::ios_base::failbit) 并且不修改 tp

If the format specifier %Z is used and successfully parsed, the parsed value is assigned to *abbrev if abbrev is not null. 如果使用格式说明符 %Z 并成功解析,则当 abbrev 不为空时,解析后的值将赋值给 *abbrev。If the format specifier %z or a modified variant is used and successfully parsed, the parsed value is assigned to *offset if offset is not null, and the parsed offset will be subtracted from the parsed timestamp before the difference is assigned to tp. 如果使用格式说明符 %z 或其修改后的变体并成功解析,则当 offset 不为空时,解析后的值将赋值给 *offset,并且解析后的偏移量将从解析后的时间戳中减去,然后将差值赋值给 tp

目录

[edit] 参数

is - 一个输入流
fmt - 一个格式字符串(见下文)
tp - 用于保存解析结果的时间点对象
abbrev - 如果非空,则指向将保存与 %Z 说明符对应时区缩写或名称的对象的指针
offset - 如果非空,则指向将保存与 %z 说明符对应的 UTC 偏移量的对象的指针

[edit] 格式字符串

The format string consists of zero or more conversion specifiers and ordinary characters. 格式字符串由零个或多个转换说明符和普通字符组成。Each ordinary character, excluding whitespace characters and the terminating null character, matches one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal. 每个普通字符(不包括空白字符和终止空字符)都与输入流中的一个相同字符匹配,或者如果流中的下一个字符不相等,则会导致函数失败。

Each whitespace character matches zero or more whitespace characters in the input stream. 每个空白字符都与输入流中的零个或多个空白字符匹配。

Each unmodified conversion specifier begins with a % character followed by a character that determines the behavior of the specifier. 每个未修改的转换说明符都以 % 字符开头,后跟一个字符,该字符确定说明符的行为。Some conversion specifiers have a modified form in which an E or O modifier character is inserted after the % character. 一些转换说明符具有修改形式,其中在 % 字符后插入 EO 修饰符字符。Some conversion specifiers have a modified form in which a width parameter given as a positive decimal integer (shown as N below) is inserted after the % character. 一些转换说明符具有修改形式,其中在 % 字符后插入宽度参数,该参数以正十进制整数形式给出(如下所示为 N)。Each conversion specifier causes the matched characters to be interpreted as parts of date and time types according to the table below. 每个转换说明符都会导致匹配的字符根据下表解释为日期和时间类型的组成部分。

A character sequence in the format string that begins with a % but does not match one of the conversion specifiers below is interpreted as ordinary characters. 格式字符串中以 % 开头但不匹配以下转换说明符之一的字符序列被解释为普通字符。

If from_stream fails to parse everything specified by the format string, or if insufficient information is parsed to specify a complete result, or if parsing discloses contradictory information, is.setstate(std::ios_base::failbit) is called. 如果 from_stream 无法解析格式字符串指定的所有内容,或者解析的信息不足以指定完整的结果,或者解析显示矛盾的信息,则会调用 is.setstate(std::ios_base::failbit)

The following conversion specifiers are available 以下转换说明符可用

Conversion
specifier
Explanation
%% Matches a literal % character. 匹配字面量 % 字符。
%n Matches one whitespace character. 匹配一个空白字符。
%t Matches zero or one whitespace character. 匹配零个或一个空白字符。
Year 年
%C
%NC
%EC
Parses the century as a decimal number. 将世纪解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %EC interprets the locale's alternative representation of the century. 修改后的命令 %EC 解释区域设置的世纪的替代表示形式。

%y
%Ny
%Ey
%Oy
Parses the last two decimal digits of the year. 解析年份的最后两位十进制数字。If the century is not otherwise specified (e.g. with %C), values in the range [69, 99] are presumed to refer to the years 1969 to 1999, and values in the range [00, 68] are presumed to refer to the years 2000 to 2068. 如果未另行指定世纪(例如,使用 %C),则范围 [69, 99] 中的值假定为指 1969 年至 1999 年,范围 [00, 68] 中的值假定为指 2000 年至 2068 年。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified commands %Ey and %Oy interpret the locale's alternative representation. 修改后的命令 %Ey%Oy 解释区域设置的替代表示形式。

%Y
%NY
%EY
Parses the year as a decimal number. 将年份解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 4. 默认宽度为 4。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %EY interprets the locale's alternative representation. 修改后的命令 %EY 解释区域设置的替代表示形式。

Month 月
%b
%B
%h
Parses the locale's full or abbreviated case-insensitive month name. 解析区域设置的完整或缩写的不区分大小写的月份名称。
%m
%Nm
%Om
Parses the month as a decimal number (January is 1). 将月份解析为十进制数字(一月为 1)。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %Om interprets the locale's alternative representation. 修改后的命令 %Om 解释区域设置的替代表示形式。

Day 日
%d
%Nd
%Od
%e
%Ne
%Oe
Parses the day of month as a decimal number. 将月份中的日期解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified commands %Od and %Oe interpret the locale's alternative representation. 修改后的命令 %Od%Oe 解释区域设置的替代表示形式。

Day of the week 星期几
%a
%A
Parses the locale's full or abbreviated case-insensitive weekday name. 解析区域设置的完整或缩写的不区分大小写的星期几名称。
%u
%Nu
Parses the ISO weekday as a decimal number (1-7), where Monday is 1. 将 ISO 星期几解析为十进制数字 (1-7),其中星期一为 1。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 1. 默认宽度为 1。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。
%w
%Nw
%Ow
Parses the weekday as a decimal number (0-6), where Sunday is 0. 将星期几解析为十进制数字 (0-6),其中星期日为 0。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 1. 默认宽度为 1。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %Ow interprets the locale's alternative representation. 修改后的命令 %Ow 解释区域设置的替代表示形式。

ISO 8601 week-based year ISO 8601 基于周的年份

In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements 在 ISO 8601 中,周从星期一开始,并且一年的第一周必须满足以下要求

  • Includes January 4 包括 1 月 4 日
  • Includes first Thursday of the year 包括一年中的第一个星期四
%g
%Ng
Parses the last two decimal digits of the ISO 8601 week-based year. 解析 ISO 8601 基于周的年份的最后两位十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。
%G
%NG
Parses the ISO 8601 week-based year as a decimal number. 将 ISO 8601 基于周的年份解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 4. 默认宽度为 4。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。
%V
%NV
Parses the ISO 8601 week of the year as a decimal number. 将 ISO 8601 年周解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。
Week/day of the year 年周/日
%j
%Nj
Parses the day of the year as a decimal number (January 1 is 1). 将一年中的第几天解析为十进制数字(1 月 1 日为 1)。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 3. 默认宽度为 3。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。
%U
%NU
%OU
Parses the week number of the year as a decimal number. 将一年中的第几周解析为十进制数字。The first Sunday of the year is the first day of week 01. 一年中的第一个星期日是一年第 01 周的第一天。Days of the same year prior to that are in week 00. 同一年中在此之前的日子在第 00 周。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %OU interprets the locale's alternative representation. 修改后的命令 %OU 解释区域设置的替代表示形式。

%W
%NW
%OW
Parses the week number of the year as a decimal number. 将一年中的第几周解析为十进制数字。The first Monday of the year is the first day of week 01. 一年中的第一个星期一是一年第 01 周的第一天。Days of the same year prior to that are in week 00. 同一年中在此之前的日子在第 00 周。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %OW interprets the locale's alternative representation. 修改后的命令 %OW 解释区域设置的替代表示形式。

Date 日期
%D Equivalent to "%m/%d/%y". 等效于 "%m/%d/%y"
%F
%NF
Equivalent to "%Y-%m-%d". 等效于 "%Y-%m-%d"。If the width is specified, it is only applied to the %Y. 如果指定了宽度,则仅应用于 %Y
%x
%Ex
Parses the locale's date representation. 解析区域设置的日期表示形式。

The modified command %Ex interprets the locale's alternate date representation. 修改后的命令 %Ex 解释区域设置的备用日期表示形式。

一天中的时间
%H
%NH
%OH
Parses the hour (24-hour clock) as a decimal number. 将小时(24 小时制)解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %OH interprets the locale's alternative representation. 修改后的命令 %OH 解释区域设置的替代表示形式。

%I
%NI
%OI
Parses the hour (12-hour clock) as a decimal number. 将小时(12 小时制)解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %OI interprets the locale's alternative representation. 修改后的命令 %OI 解释区域设置的替代表示形式。

%M
%NM
%OM
Parses the minute as a decimal number. 将分钟解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2. 默认宽度为 2。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %OM interprets the locale's alternative representation. 修改后的命令 %OM 解释区域设置的替代表示形式。

%S
%NS
%OS
Parses the second as a decimal number. 将秒解析为十进制数字。The width N specifies the maximum number of characters to read. 宽度 N 指定要读取的最大字符数。The default width is 2 if Duration is convertible to std::chrono::seconds, otherwise it is determined by the decimal precision of Duration and the field is interpreted as a long double in a fixed format, with the decimal point character determined by the locale (if encountered). 如果 Duration 可转换为 std::chrono::seconds,则默认宽度为 2,否则由 Duration 的十进制精度确定,并且该字段被解释为固定格式的长双精度浮点数,小数点字符由区域设置确定(如果遇到)。Leading zeroes are permitted but not required. 允许前导零,但不是必需的。

The modified command %OS interprets the locale's alternative representation. 修改后的命令 %OS 解释区域设置的替代表示形式。

%p Parses the locale's equivalent of the AM/PM designations associated with a 12-hour clock. 解析区域设置中与 12 小时制时钟关联的 AM/PM 指示符的等效项。
%R Equivalent to "%H:%M". 等效于 "%H:%M"
%T Equivalent to "%H:%M:%S". 等效于 "%H:%M:%S"
%r Parses the locale's 12-hour clock time. 解析区域设置的 12 小时制时钟时间。
%X
%EX
Parses the locale's time representation. 解析区域设置的时间表示形式。

The modified command %EX interprets the locale's alternate time representation. 修改后的命令 %EX 解释区域设置的备用时间表示形式。

Miscellaneous 杂项
%c
%Ec
Parses the locale's date and time representation. 解析区域设置的日期和时间表示形式。

The modified command %Ec interprets the locale's alternative date and time representation. 修改后的命令 %Ec 解释区域设置的替代日期和时间表示形式。

%z
%Ez
%Oz
Parses the offset from UTC in the format [+|-]hh[mm]. 解析 UTC 的偏移量,格式为 [+|-]hh[mm]。For example -0430 refers to 4 hours 30 minutes behind UTC and 04 refers to 4 hours ahead of UTC. 例如,-0430 指的是比 UTC 晚 4 小时 30 分钟,而 04 指的是比 UTC 早 4 小时。

The modified commands %Ez and %Oz parses the format [+|-]h[h][:mm] (i.e., requiring a : between the hours and minutes and making the leading zero for hour optional). 修改后的命令 %Ez%Oz 解析格式 [+|-]h[h][:mm](即,要求小时和分钟之间有一个 :,并使小时的前导零变为可选)。

%Z Parses the time zone abbreviation or name, taken as the longest sequence of characters that only contains the characters A through Z, a through z, 0 through 9, -, +, _, and /. 解析时区缩写或名称,取为仅包含字符 AZaz09-+_/ 的最长字符序列。

[edit] 返回值

is

[edit] 示例

[edit] 参见

(C++20)
parses a chrono object from a stream 从流中解析 chrono 对象
(function template) [编辑]