Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::get_time

      From cppreference.com
      <cpp‎ |io‎ |manip
       
       
       
      Input/output manipulators
      Floating-point formatting
      Integer formatting
      Boolean formatting
      Field width and fill control
      Other formatting
      Whitespace processing
      Output flushing
      Status flags manipulation
      Time and money I/O
      (C++11)
      get_time
      (C++11)
      (C++11)
      (C++11)
      Quoted manipulator
      (C++14)
       
      Defined in header<iomanip>
      template<class CharT>
      /*unspecified*/ get_time(std::tm* tmb,const CharT* fmt);
      (since C++11)

      When used in an expressionin>> get_time(tmb, fmt), parses the character input as a date/time value according to format stringfmt according to thestd::time_get facet of the locale currently imbued in the input streamin. The resultant value is stored in astd::tm object pointed to bytmb.

      Contents

      [edit]Parameters

      tmb - valid pointer to thestd::tm object where the result will be stored
      fmt - pointer to a null-terminatedCharT 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 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

      An object of unspecified type such that

      • ifin is an object of typestd::basic_istream<CharT, Traits>, the expressionin>> get_time(tmb, fmt)
        • has typestd::basic_istream<CharT, Traits>&
        • has valuein
        • behaves as if it calledf(in, tmb, fmt)

      where the functionf is defined as:

      template<class CharT,class Traits>void f(std::basic_ios<CharT, Traits>& str,std::tm* tmb,const CharT* fmt){using Iter=std::istreambuf_iterator<CharT, Traits>;using TimeGet= time_get<CharT, Iter>; std::ios_base::iostate err=std::ios_base::goodbit;const TimeGet& tg=std::use_facet<TimeGet>(str.getloc());     tg.get(Iter(str.rdbuf()), Iter(), str, err, tmb,        fmt, fmt+ Traits::length(fmt)); if(err!=std::ios_base::goodbit)        str.setstate(err);}

      [edit]Notes

      As specified instd::time_get::do_get, which this function calls, it's unspecified if this function zero out the fields in*tmb that are not set directly by the conversion specifiers that appear infmt: portable programs should initialize every field of*tmb to zero before callingstd::get_time.

      [edit]Example

      Note: choose clang or gcc >= 12.1 to observe the output. libstdc++ before 12.1 does not correctly implement the%b specifier:bug #78714.

      Run this code
      #include <iomanip>#include <iostream>#include <locale>#include <sstream> 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";elsestd::cout<<std::put_time(&t,"%c")<<'\n';}

      Possible output:

      Sun Feb 18 23:12:34 2011

      [edit]See also

      parses time/date values from an input character sequence intostd::tm
      (class template)[edit]
      (C++11)
      formats and outputs a date/time value according to the specified format
      (function template)[edit]
      (C++20)
      parses achrono object from a stream
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/manip/get_time&oldid=158978"

      [8]ページ先頭

      ©2009-2025 Movatter.jp