Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/time get"

From cppreference.com
< cpp‎ | locale
m (slightly more meanignful example)
m (fmt, ., headers sorted)
 
(27 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/title | time_get}}
+
{{cpp/title|time_get}}
{{cpp/locale/sidebar}}
+
{{cpp/locale/time_get/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | locale}}
+
{{dcl header|locale}}
{{ddcl list item | 1=
+
{{dcl|1=
template< class charT, class InputIterator = std::istreambuf_iterator<charT> >
+
template<  
class time_get : public std::locale::facet, public std::time_base;
+
    class CharT,  
 +
    class InputIt = std::istreambuf_iterator<CharT>  
 +
> class time_get;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
{{todo| inheritance diagram }}
+
Class template {{tt|std::time_get}} encapsulates date and time parsing rules. The I/O manipulator {{lc|std::get_time}} uses the {{tt|std::time_get}} facet of the I/O stream's locale to convert text input to a {{lc|std::tm}} object.
  
Class {{tt|std::time_get}} encapsulates date and time parsing rules. The I/O manipulator {{cpp|std::get_time}} uses the {{tt|std::time_get}} facet of the I/O stream's locale to convert text input to a {{cpp|std::tm}} object.
+
{{inheritance diagram/std-time_get}}
  
Two specializations and two partial specializations are provided by the standard library and are implemented by all locale objects created in a C++ program:
+
If a {{tt|std::time_get}} specialization is not guaranteed to be provided by the standard library (see below), the behaviors of its member functions (except the constructor and destructor) are not guaranteed as specified.
  
{{tdcl list begin}}
+
===Specializations===
{{tdcl list header | locale }}
+
The standard library is guaranteed to provide the following specializations (they are {{rlp|locale|required to be implemented by any locale object}}):
{{tdcl list item | {{cpp|std::time_get<char>}} | parses narrow string representations of date and time }}
+
{{dsc begin}}
{{tdcl list item | {{cpp|std::time_get<wchar_t>}} | parses wide string representations of date and time }}
+
{{dsc header|locale}}
{{tdcl list item | {{cpp|std::time_get<char, InputIterator>}} | parses narrow string representations of date and time using custom input iterator}}
+
{{dsc|{{c/core|std::time_get<char>}}|parses narrow string representations of date and time}}
{{tdcl list item | {{cpp|std::time_get<wchar_t, InputIterator>}} | parses wide string representations of date and time using custom input iterator}}
+
{{dsc|{{c/core|std::time_get<wchar_t>}}|parses wide string representations of date and time}}
{{tdcl list end}}
+
{{dsc end}}
 +
 
 +
In addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:
 +
* {{tt|CharT}} is one of {{c/core|char}} and {{c/core|wchar_t}}, and
 +
* {{tt|InputIt}} must meet the requirements of {{named req|InputIterator}}.
  
 
===Member types===
 
===Member types===
{{tdcl list begin}}
+
{{dsc begin}}
{{tdcl list hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{tdcl list item | {{tt|char_type}} | {{tt|charT}}}}
+
{{dsc|{{tt|char_type}}|{{tt|CharT}}}}
{{tdcl list item | {{tt|iter_type}} | {{tt|InputIterator}}}}
+
{{dsc|{{tt|iter_type}}|{{tt|InputIt}}}}
{{tdcl list end}}
+
{{dsc end}}
 
+
===Member objects===
+
{{tdcl list begin}}
+
{{tdcl list hitem | Member name | Type}}
+
{{tdcl list item | {{tt|id}} {{mark|static}} | {{cpp|std::locale::id}} }}
+
{{tdcl list end}}
+
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/locale/time_get/time_get | constructs a new time_get facet }}
+
{{dsc mem ctor|cpp/locale/time_get/time_get|constructs a new {{tt|time_get}} facet}}
{{dcl list prot mem dtor | cpp/locale/messages/~messages | destructs a messages facet }}
+
{{dsc prot mem dtor|cpp/locale/time_get/~time_get|destructs a {{tt|time_get}} facet}}
 +
{{dsc inc|cpp/locale/time_get/dsc date_order}}
 +
{{dsc inc|cpp/locale/time_get/dsc get_time}}
 +
{{dsc inc|cpp/locale/time_get/dsc get_date}}
 +
{{dsc inc|cpp/locale/time_get/dsc get_weekday}}
 +
{{dsc inc|cpp/locale/time_get/dsc get_monthname}}
 +
{{dsc inc|cpp/locale/time_get/dsc get_year}}
 +
{{dsc inc|cpp/locale/time_get/dsc get}}
 +
{{dsc end}}
  
{{dcl list h2 | Public member functions (pubic interface) }}
+
===Member objects===
{{dcl list mem fun | cpp/locale/time_get/do_date_order | title=date_order | invokes {{tt|do_date_order}} }}
+
{{dsc begin}}
{{dcl list mem fun | cpp/locale/time_get/do_get_time | title=get_time | invokes {{tt|do_get_time}} }}
+
{{dsc mem obj|nolink=true|{{dsc small|static std::locale::id}} id|''id'' of the locale}}
{{dcl list mem fun | cpp/locale/time_get/do_get_date | title=get_date | invokes {{tt|do_get_date}} }}
+
{{dsc end}}
{{dcl list mem fun | cpp/locale/time_get/do_get_weekday | title=get_weekday | invokes {{tt|do_get_weekday}} }}
+
{{dcl list mem fun | cpp/locale/time_get/do_get_monthname | title=get_monthname | invokes {{tt|do_get_monthname}} }}
+
{{dcl list mem fun | cpp/locale/time_get/do_get_year | title=get_year | invokes {{tt|do_get_year}} }}
+
{{dcl list mem fun | cpp/locale/time_get/do_get | title=get | invokes {{tt|do_get}} }}
+
  
{{dcl list h2 | Virtual member functions (can be overridden in a user-defined facet derived from codecvt }}
+
===Protected member functions===
{{dcl list prot mem vfun | cpp/locale/time_get/do_date_order | obtains preferred ordering of day, month, and year}}
+
{{dsc begin}}
{{dcl list prot mem vfun | cpp/locale/time_get/do_get_time | extracts hours, minutes, and seconds from input stream}}
+
{{dsc inc|cpp/locale/time_get/dsc do_date_order}}
{{dcl list prot mem vfun | cpp/locale/time_get/do_get_date | extracts month, day, and year from input stream}}
+
{{dsc inc|cpp/locale/time_get/dsc do_get_time}}
{{dcl list prot mem vfun | cpp/locale/time_get/do_get_weekday | extracts the name of a day of the week from input stream}}
+
{{dsc inc|cpp/locale/time_get/dsc do_get_date}}
{{dcl list prot mem vfun | cpp/locale/time_get/do_get_monthname | extacts a month name from input stream}}
+
{{dsc inc|cpp/locale/time_get/dsc do_get_weekday}}
{{dcl list prot mem vfun | cpp/locale/time_get/do_get_year | extracts a year from input stream}}
+
{{dsc inc|cpp/locale/time_get/dsc do_get_monthname}}
{{dcl list prot mem vfun | cpp/locale/time_get/do_get | extracts date/time components from input stream, according to the specified format}}
+
{{dsc inc|cpp/locale/time_get/dsc do_get_year}}
{{dcl list end}}
+
{{dsc inc|cpp/locale/time_get/dsc do_get}}
 +
{{dsc end}}
  
{{ inherited | {{small|std::}}time_base |
+
{{inherited|{{small|std::}}time_base|
{{tdcl list begin}}
+
{{dsc begin}}
{{tdcl list hitem | Type | Definition}}
+
{{dsc hitem|Type|Definition}}
{{tdcl list item | {{tt|dateorder}} | date order enumeration type, defining the values {{tt|no_order}}, {{tt|dmy}}, {{tt|mdy}}, {{tt|ymd}}, and {{tt|ydm}}}}
+
{{dsc|{{tt|dateorder}}|date order enumeration type, defining the values {{tt|no_order}}, {{tt|dmy}}, {{tt|mdy}}, {{tt|ymd}}, and {{tt|ydm}}}}
{{tdcl list end}}
+
{{dsc end}}
 
}}
 
}}
 
  
 
===Example===
 
===Example===
 
+
{{example
{{example cpp
+
|1=Note: choose clang to observe the output. libstdc++ does not correctly implement the %b specifier: [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78714 bug 78714].
|  
+
|code=
| code=
+
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
#include <sstream>
 
#include <string>
 
 
#include <locale>
 
#include <locale>
#include <ctime>
+
#include <sstream>
#include <iomanip>
+
 
 
int main()
 
int main()
 
{
 
{
     std::wstring input = L"2011-Februar-18 23:12:34";
+
     std::tm t = {};
    std::tm t;
+
    std::istringstream ss("2011-Februar-18 23:12:34");
    std::wistringstream ss(input);
+
     ss.imbue(std::locale("de_DE.utf-8"));
     ss.imbue(std::locale("de_DE"));
+
   
     ss >> std::get_time(&t, L"%Y-%b-%d %H:%M:%S"); // uses std::time_get<wchar_t>
+
     ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
     std::cout << std::asctime(&t);
+
    if (ss.fail())
 +
        std::cout << "Parse failed\n";
 +
     else
 +
        std::cout << std::put_time(&t, "%c") << '\n';
 
}
 
}
| output=
+
|p=true
 +
|output=
 
Sun Feb 18 23:12:34 2011
 
Sun Feb 18 23:12:34 2011
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list tclass | cpp/locale/time_put | formats contents of {{cpp|struct std::tm}} for output as character sequence}}
+
{{dsc inc|cpp/locale/dsc time_put}}
{{dcl list tfun | cpp/io/manip/get_time | parses a date/time value of specified format| notes={{mark c++11}}}}
+
{{dsc inc|cpp/io/manip/dsc get_time}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 09:31, 6 October 2023

 
 
 
 
Defined in header <locale>
template<

    class CharT,
    class InputIt = std::istreambuf_iterator<CharT>

> class time_get;

Class template std::time_get encapsulates date and time parsing rules. The I/O manipulator std::get_time uses the std::time_get facet of the I/O stream's locale to convert text input to a std::tm object.

cpp/locale/time basecpp/locale/locale/facetstd-time get-inheritance.svg

Inheritance diagram

If a std::time_get specialization is not guaranteed to be provided by the standard library (see below), the behaviors of its member functions (except the constructor and destructor) are not guaranteed as specified.

Contents

[edit] Specializations

The standard library is guaranteed to provide the following specializations (they are required to be implemented by any locale object):

Defined in header <locale>
std::time_get<char> parses narrow string representations of date and time
std::time_get<wchar_t> parses wide string representations of date and time

In addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:

  • CharT is one of char and wchar_t, and
  • InputIt must meet the requirements of LegacyInputIterator.

[edit] Member types

Member type Definition
char_type CharT
iter_type InputIt

[edit] Member functions

constructs a new time_get facet
(public member function)
destructs a time_get facet
(protected member function)
invokes do_date_order
(public member function) [edit]
invokes do_get_time
(public member function) [edit]
invokes do_get_date
(public member function) [edit]
invokes do_get_weekday
(public member function) [edit]
invokes do_get_monthname
(public member function) [edit]
invokes do_get_year
(public member function) [edit]
(C++11)
invokes do_get
(public member function) [edit]

[edit] Member objects

static std::locale::id id
id of the locale
(public member object)

[edit] Protected member functions

obtains preferred ordering of day, month, and year
(virtual protected member function) [edit]
[virtual]
extracts hours, minutes, and seconds from input stream
(virtual protected member function) [edit]
[virtual]
extracts month, day, and year from input stream
(virtual protected member function) [edit]
extracts the name of a day of the week from input stream
(virtual protected member function) [edit]
extracts a month name from input stream
(virtual protected member function) [edit]
[virtual]
extracts a year from input stream
(virtual protected member function) [edit]
[virtual] (C++11)
extracts date/time components from input stream, according to the specified format
(virtual protected member function) [edit]

Inherited from std::time_base

Type Definition
dateorder date order enumeration type, defining the values no_order, dmy, mdy, ymd, and ydm

[edit] Example

Note: choose clang to observe the output. libstdc++ does not correctly implement the %b specifier: bug 78714.

#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";
    else
        std::cout << std::put_time(&t, "%c") << '\n';
}

Possible output:

Sun Feb 18 23:12:34 2011

[edit] See also

formats contents of std::tm for output as character sequence
(class template) [edit]
(C++11)
parses a date/time value of specified format
(function template) [edit]