Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::time_get<CharT,InputIt>::get,std::time_get<CharT,InputIt>::do_get

      From cppreference.com
      <cpp‎ |locale‎ |time get
       
       
       
      Localization library
       
       
      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 beg, 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) Parses the date and time from the input character sequence[beg, end) according to the format provided in the character sequence[fmtbeg, fmtend). The format is expected to follow the format described below, although actual processing of each format specifier can be customized by overridingdo_get. Theget function performs the following:First, clears the error bits inerr by executingerr=std::ios_base::goodbit. Then enters a loop, which terminates whenever any of the following conditions becomes true (checked in this order):
      a) All characters have been read from the format string (fmtbeg== fmtend).
      b) There was a parsing error (err!=std::ios_base::goodbit).
      c) All characters have been read from the input sequence (beg== end. If this condition terminates the loop, the function sets botheofbit andfailbit inerr.
      In the body of the loop, the following steps take place:
      a) If the next character in the format string is'%', followed by one or two characters that form a validstd::get_time conversion specifier (see below), these characters are used in the calldo_get(beg, end, str, err, t, format, modifier), whereformat is the primary conversion specifier character, andmodifier is the optional modifier (which appears between% and the format character, if present). If there is no modifier, the value'\0' is used. If the format string is ambiguous or ends too early to determine the conversion specifier after'%',eofbit is set inerr and the loop is terminated. If, after the call todo_get, no error bits are set inerr, the function incrementsfmtbeg to point right after the conversion specifier and continues the loop.
      b) If the next character is whitespace, as indicated by the locale provided in the streamstr (i.e.std::isspace(*fmtbeg, str.getloc())==true, the function keeps incrementingfmtbeg until it either becomes equal tofmtend or points to a non-whitespace character.
      c) If the next character in the format string is equivalent to the next character in the input stream according to case-insensitive comparison, the function advances both sequences by one character++fmtbeg,++beg; and continues the loop, Otherwise, it sets thefailbit inerr.
      2) Parses one conversion specifier from the input sequence[beg, end) and updates thestd::tm structure pointed to byt accordingly.
      First, clears the error bits inerr by executingerr=std::ios_base::goodbit. Then reads characters from the input sequence[beg, end) that are expected by thestd::time_get format specifier formed by combining'%',modifier (if not'\0'), andformat. If the characters do not combine to form a valid conversion specifier, setsfailbit inerr. If the end of the input stream is reached after reading a character, setseofbit inerr. If the input string was parsed successfully, updates the corresponding fields of*t.
      For complex conversion specifiers, such as'%x' or'%c', or the directives that use the modifiers'E' and'O', the function may fail to determine some of the values to store in*t. In such case, it setseofbit inerr and leaves these fields in unspecified state.

      Contents

      [edit]Parameters

      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 orstd::collate to compare strings
      err - stream error flags object that is modified by this function to indicate errors
      t - pointer to thestd::tm object that will hold the result of this function call
      fmtbeg - pointer to the first character of a sequence ofchar_type characters specifying the conversion format (see below)
      fmtend - pointer one past the last character of a sequence ofchar_type characters specifying the conversion format
      format - the character that names a conversion specifier
      modifier - the optional modifier that may appear between% and the conversion specifier


      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 byE orO modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The format specifiers match the POSIX functionstrptime():

      Conversion
      specifier
      ExplanationWrites to fields
      %matches a literal%. The full conversion specification must be%%(none)
      tmatches any whitespace(none)
      nmatches any whitespace(none)
      Year
      Yparses fullyear as a 4 digit decimal number, leading zeroes permitted but not requiredtm_year
      EYparsesyear in the alternative representation, e.g.平成23年 (year Heisei 23) which writes 2011 to tm_year in ja_JP localetm_year
      yparses last 2 digits ofyear as a decimal number. Range[69,99] results in values 1969 to 1999, range[00,68] results in 2000-2068tm_year
      Oyparses last 2 digits ofyear using the alternative numeric system, e.g. 十一 is parsed as 11 in ja_JP localetm_year
      Eyparsesyear as offset from locale's alternative calendar period%ECtm_year
      Cparses the first 2 digits ofyear as a decimal number (range[00,99])tm_year
      ECparses the name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JPtm_year
      Month
      bparses the month name, either full or abbreviated, e.g.Octtm_mon
      hsynonym ofbtm_mon
      Bsynonym ofbtm_mon
      mparses themonth as a decimal number (range[01,12]), leading zeroes permitted but not requiredtm_mon
      Omparses themonth using the alternative numeric system, e.g. 十二 parses as 12 in ja_JP localetm_mon
      Week
      Uparses theweek of the year as a decimal number (Sunday is the first day of the week) (range[00,53]), leading zeroes permitted but not requiredtm_year,tm_wday,tm_yday
      OUparses theweek of the year, as by%U, using the alternative numeric system, e.g. 五十二 parses as 52 in ja_JP localetm_year,tm_wday,tm_yday
      Wparses theweek of the year as a decimal number (Monday is the first day of the week) (range[00,53]), leading zeroes permitted but not requiredtm_year,tm_wday,tm_yday
      OWparses theweek of the year, as by%W, using the alternative numeric system, e.g. 五十二 parses as 52 in ja_JP localetm_year,tm_wday,tm_yday
      Day of the year/month
      jparsesday of the year as a decimal number (range[001,366]), leading zeroes permitted but not requiredtm_yday
      dparses theday of the month as a decimal number (range[01,31]), leading zeroes permitted but not requiredtm_mday
      Odparses theday of the month using the alternative numeric system, e.g. 二十七 parses as 27 in ja_JP locale, leading zeroes permitted but not requiredtm_mday
      esynonym ofdtm_mday
      Oesynonym ofOdtm_mday
      Day of the week
      aparses the name of the day of the week, either full or abbreviated, e.g.Fritm_wday
      Asynonym ofatm_wday
      wparsesweekday as a decimal number, where Sunday is0 (range[0-6])tm_wday
      Owparsesweekday as a decimal number, where Sunday is0, using the alternative numeric system, e.g. 二 parses as 2 in ja_JP localetm_wday
      Hour, minute, second
      Hparses thehour as a decimal number, 24 hour clock (range[00-23]), leading zeroes permitted but not requiredtm_hour
      OHparseshour from 24-hour clock using the alternative numeric system, e.g. 十八 parses as 18 in ja_JP localetm_hour
      Iparseshour as a decimal number, 12 hour clock (range[01,12]), leading zeroes permitted but not requiredtm_hour
      OIparseshour from 12-hour clock using the alternative numeric system, e.g. 六 reads as 06 in ja_JP localetm_hour
      Mparsesminute as a decimal number (range[00,59]), leading zeroes permitted but not requiredtm_min
      OMparsesminute using the alternative numeric system, e.g. 二十五 parses as 25 in ja_JP localetm_min
      Sparsessecond as a decimal number (range[00,60]), leading zeroes permitted but not requiredtm_sec
      OSparsessecond using the alternative numeric system, e.g. 二十四 parses as 24 in ja_JP localetm_sec
      Other
      cparses the locale's standard date and time string format, e.g.Sun Oct 17 04:41:13 2010 (locale dependent)all
      Ecparses the locale's alternative date and time string format, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP localeall
      xparses the locale's standard date representationall
      Exparses the locale's alternative date representation, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP localeall
      Xparses the locale's standard time representationall
      EXparses the locale's alternative time representationall
      Dequivalent to"%m / %d / %y "tm_mon,tm_mday,tm_year
      rparses locale's standard 12-hour clock time (in POSIX,"%I : %M : %S %p")tm_hour,tm_min,tm_sec
      Requivalent to"%H : %M"tm_hour,tm_min
      Tequivalent to"%H : %M : %S"tm_hour,tm_min,tm_sec
      pparses the locale's equivalent ofa.m. or p.m.tm_hour

      Note:tm_isdst is not written to, and needs to be set explicitly for use with functions such asmktime

      [edit]Return value

      Iterator pointing one past the last character in[beg, end) that was parsed successfully.

      [edit]Notes

      The case-insensitive comparison for the non-whitespace non-'%' characters in the format string, thestd::collate facet of the locale provided bystr is typically, but not necessarily, used.

      If a parsing error is encountered, many implementations of this function leave*t completely untouched.

      It's unspecified if these functions zero out the fields in*t that they do not set directly: portable programs should initialize every field to zero before callingget().

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <locale>#include <sstream> int main(){std::istringstream ss("2026-März-12 23:45:56");    ss.imbue(std::locale("de_DE.utf8")); auto& f=std::use_facet<std::time_get<char>>(ss.getloc());std::tm t{};std::string s="%Y-%b-%d %H:%M:%S";std::ios_base::iostate err=std::ios_base::goodbit;auto ret= f.get({ss},{}, ss, err,&t,&s[0],&s[0]+ s.size());    ss.setstate(err);std::istreambuf_iterator<char> last{}; if(ss){std::cout<<"Successfully parsed as "<<std::put_time(&t,"%c")<<'\n';if(ret!= last){std::cout<<"Remaining content: ";std::copy(ret, last,std::ostreambuf_iterator<char>(std::cout));}elsestd::cout<<"The input was fully consumed.";}else{std::cout<<"Parse failed.\nUnparsed string: ";std::copy(ret, last,std::ostreambuf_iterator<char>(std::cout));}std::cout<<'\n';}

      Output:

      Successfully parsed as Sun Mar 12 23:45:56 2026The input was fully consumed.

      [edit]See also

      (C++11)
      parses a date/time value of specified format
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/time_get/get&oldid=169770"

      [8]ページ先頭

      ©2009-2025 Movatter.jp