Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::parse

      From cppreference.com
      <cpp‎ |chrono
       
       
      Date and time library
       
      Defined in header<chrono>
      template<class CharT,class Parsable>
      /* unspecified */ parse(const CharT* fmt, Parsable& tp);
      (1)(since C++20)
      template<class CharT,class Traits,class Alloc,class Parsable>

      /* unspecified */ parse(conststd::basic_string<CharT, Traits, Alloc>& fmt,

                               Parsable& tp);
      (2)(since C++20)
      template<class CharT,class Traits,class Alloc,class Parsable>

      /* unspecified */ parse(const CharT* fmt, Parsable& tp,

                               std::basic_string<CharT, Traits, Alloc>& abbrev);
      (3)(since C++20)
      template<class CharT,class Traits,class Alloc,class Parsable>

      /* unspecified */ parse(conststd::basic_string<CharT, Traits, Alloc>& fmt,
                               Parsable& tp,

                               std::basic_string<CharT, Traits, Alloc>& abbrev);
      (4)(since C++20)
      template<class CharT,class Parsable>

      /* unspecified */ parse(const CharT* fmt, Parsable& tp,

                               std::chrono::minutes& offset);
      (5)(since C++20)
      template<class CharT,class Traits,class Alloc,class Parsable>

      /* unspecified */ parse(conststd::basic_string<CharT, Traits, Alloc>& fmt,

                               Parsable& tp,std::chrono::minutes& offset);
      (6)(since C++20)
      template<class CharT,class Traits,class Alloc,class Parsable>

      /* unspecified */ parse(const CharT* fmt, Parsable& tp,
                               std::basic_string<CharT, Traits, Alloc>& abbrev,

                               std::chrono::minutes& offset);
      (7)(since C++20)
      template<class CharT,class Traits,class Alloc,class Parsable>

      /* unspecified */ parse(conststd::basic_string<CharT, Traits, Alloc>& fmt,
                               Parsable& tp,
                               std::basic_string<CharT, Traits, Alloc>& abbrev,

                               std::chrono::minutes& offset);
      (8)(since C++20)

      Returns an objectmanip of unspecified type such that, given astd::basic_istream<CharT, Traits> objectis, the expressionis>> manip callsfrom_stream (unqualified, to enableargument-dependent lookup) as follows:

      1)from_stream(is, fmt, tp)
      2)from_stream(is, fmt.c_str(), tp)
      3)from_stream(is, fmt, tp,std::addressof(abbrev))
      4)from_stream(is, fmt.c_str(), tp,std::addressof(abbrev))
      5)from_stream(is, fmt, tp,
                 static_cast<std::basic_string<CharT, Traits, Alloc>*>(nullptr),&offset)
      6)from_stream(is, fmt.c_str(), tp,
                 static_cast<std::basic_string<CharT, Traits, Alloc>*>(nullptr),&offset)
      7)from_stream(is, fmt, tp,std::addressof(abbrev),&offset)
      8)from_stream(is, fmt.c_str(), tp,std::addressof(abbrev),&offset).

      The expressionis>> manip is an lvalue of typestd::basic_istream<CharT, Traits> with the valueis.

      These overloads participate in overload resolution only if the correspondingfrom_stream expression is well-formed.

      Implementations are recommended to make it difficult to use potentially dangling references to the format string, e.g., by making return types non-movable and preventingoperator>> from accepting lvalues of return types.

      Contents

      [edit]Parameters

      fmt - a format string (see below)
      tp - object to hold the parse result
      abbrev - string to hold the time zone abbreviation or name corresponding to the%Z specifier
      offset - duration to represent the offset from UTC corresponding to the%z specifier

      [edit]Format string

      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 anE orO modifier character is inserted after the% character. Some conversion specifiers have a modified form in which a width parameter given as a positive decimal integer (shown asN below) is inserted after the% character. 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.

      Iffrom_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.

      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 widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%EC interprets the locale's alternative representation of the century.

      %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. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified commands%Ey and%Oy interpret the locale's alternative representation.

      %Y
      %NY
      %EY
      Parses the year as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 4. Leading zeroes are permitted but not required.

      The modified command%EY interprets the locale's alternative representation.

      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 is1). The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%Om interprets the locale's alternative representation.

      Day
      %d
      %Nd
      %Od
      %e
      %Ne
      %Oe
      Parses the day of month as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified commands%Od and%Oe interpret the locale's alternative representation.

      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 is1. The widthN specifies the maximum number of characters to read. The default width is 1. Leading zeroes are permitted but not required.
      %w
      %Nw
      %Ow
      Parses the weekday as a decimal number (0-6), where Sunday is0. The widthN specifies the maximum number of characters to read. The default width is 1. Leading zeroes are permitted but not required.

      The modified command%Ow interprets the locale's alternative representation.

      ISO 8601 week-based year

      In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

      • Includes January 4
      • Includes first Thursday of the year
      %g
      %Ng
      Parses the last two decimal digits of the ISO 8601 week-based year. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.
      %G
      %NG
      Parses the ISO 8601 week-based year as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 4. Leading zeroes are permitted but not required.
      %V
      %NV
      Parses the ISO 8601 week of the year as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 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 is1). The widthN specifies the maximum number of characters to read. The default width is 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. Days of the same year prior to that are in week 00. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%OU interprets the locale's alternative representation.

      %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. Days of the same year prior to that are in week 00. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%OW interprets the locale's alternative representation.

      Date
      %D Equivalent to"%m/%d/%y".
      %F
      %NF
      Equivalent to"%Y-%m-%d". If the width is specified, it is only applied to the%Y.
      %x
      %Ex
      Parses the locale's date representation.

      The modified command%Ex interprets the locale's alternate date representation.

      Time of day
      %H
      %NH
      %OH
      Parses the hour (24-hour clock) as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%OH interprets the locale's alternative representation.

      %I
      %NI
      %OI
      Parses the hour (12-hour clock) as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%OI interprets the locale's alternative representation.

      %M
      %NM
      %OM
      Parses the minute as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%OM interprets the locale's alternative representation.

      %S
      %NS
      %OS
      Parses the second as a decimal number. The widthN specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

      The modified command%OS interprets the locale's alternative representation.

      %p Parses the locale's equivalent of the AM/PM designations associated with a 12-hour clock.
      %R Equivalent to"%H:%M".
      %T Equivalent to"%H:%M:%S".
      %r Parses the locale's 12-hour clock time.
      %X
      %EX
      Parses the locale's time representation.

      The modified command%EX interprets the locale's alternate time representation.

      Miscellaneous
      %c
      %Ec
      Parses the locale's date and time representation.

      The modified command%Ec interprets the locale's alternative date and time representation.

      %z
      %Ez
      %Oz
      Parses the offset from UTC in the format[+|-]hh[mm]. For example-0430 refers to 4 hours 30 minutes behind UTC and04 refers to 4 hours ahead of UTC.

      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).

      %Z Parses the time zone abbreviation or name, taken as the longest sequence of characters that only contains the charactersA throughZ,a throughz,0 through9,-,+,_, and/.

      [edit]Example

      Run this code
      #include <chrono>#include <iostream>#include <locale>#include <sstream> int main(){auto parse=[&](auto str,auto fmt,auto o){std::istringstream is{str};        is.imbue(std::locale("en_US.utf-8"));        is>> std::chrono::parse(fmt, o);        is.fail()?std::cout<<"Parse failed!\n":std::cout<< o<<'\n';};    parse("01:02:03","%H:%M:%S",std::chrono::hours{});    parse("01:02:03","%H:%M:%S",std::chrono::minutes{});    parse("01:02:03","%H:%M:%S",std::chrono::seconds{});}

      Possible output:

      1h62min3723s

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3554C++20overloads for plain null-terminated character type sequences were missingadded

      [edit]See also

      parses asys_time from a stream according to the provided format
      (function template)[edit]
      parses autc_time from a stream according to the provided format
      (function template)[edit]
      parses atai_time from a stream according to the provided format
      (function template)[edit]
      parses agps_time from a stream according to the provided format
      (function template)[edit]
      parses afile_time from a stream according to the provided format
      (function template)[edit]
      parses alocal_time from a stream according to the provided format
      (function template)[edit]
      parses ayear from a stream according to the provided format
      (function template)[edit]
      parses amonth from a stream according to the provided format
      (function template)[edit]
      parses aday from a stream according to the provided format
      (function template)[edit]
      parses aweekday from a stream according to the provided format
      (function template)[edit]
      parses amonth_day from a stream according to the provided format
      (function template)[edit]
      parses ayear_month from a stream according to the provided format
      (function template)[edit]
      parses ayear_month_day from a stream according to the provided format
      (function template)[edit]
      (C++11)
      parses a date/time value of specified format
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/parse&oldid=181811"

      [8]ページ先頭

      ©2009-2025 Movatter.jp