Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/time put byname"

From cppreference.com
< cpp‎ | locale
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (Example: fix locale.)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/title | time_put_byname}}
+
{{cpp/title|time_put_byname}}
 
{{cpp/locale/navbar}}
 
{{cpp/locale/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | locale}}
+
{{dcl header|locale}}
{{ddcl list item | 1=
+
{{dcl|1=
template< class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
+
template<
class time_put_byname : public std::time_put<CharT, OutputIterator>;
+
    class CharT,
 +
    class OutputIt = std::ostreambuf_iterator<CharT>
 +
> class time_put_byname : public std::time_put<CharT, OutputIt>;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
{{c|std::time_put_byname}} is a {{c|std::time_put}} facet which encapsulates time and date formatting rules of the locale specified at its construction.
+
{{tt|std::time_put_byname}} is a {{lc|std::time_put}} facet which encapsulates time and date formatting rules of the locale specified at its construction.
  
Two specializations are provided by the standard library
+
===Specializations===
 
+
The standard library is guaranteed to provide every specialization that satisfies the following type requirements:
{{dcl list begin}}
+
* {{tt|CharT}} is one of {{c/core|char}} and {{c/core|wchar_t}}, and
{{dcl list header | locale }}
+
* {{tt|OutputIt}} must meet the requirements of {{named req|OutputIterator}}.
{{dcl list item | {{c|std::time_put_byname<char, OutputIterator>}} | narrow/multibyte time formatting }}
+
{{dcl list item | {{c|std::time_put_byname<wchar_t>, OutputIterator}} | wide string time formatting }}
+
{{dcl list end}}
+
  
 
===Member types===
 
===Member types===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dcl list item | {{tt|char_type}} | {{tt|CharT}}}}
+
{{dsc|{{tt|char_type}}|{{tt|CharT}}}}
{{dcl list item | {{tt|iter_type}} | {{tt|OutputIterator}}}}
+
{{dsc|{{tt|iter_type}}|{{tt|OutputIt}}}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/locale/time_put_byname/time_put_byname | constructs a new time_put_byname facet }}
+
{{dsc inc|cpp/locale/byname/dsc constructor|time_put_byname}}
{{dcl list prot mem dtor | cpp/locale/time_put_byname/~time_put_byname | destructs a time_put_byname facet }}
+
{{dsc inc|cpp/locale/byname/dsc destructor|time_put_byname}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
{{include|cpp/locale/byname/constructor|time_put_byname}}
 +
{{include|cpp/locale/byname/destructor|time_put_byname}}
  
 
{{cpp/locale/time_put/inherit}}
 
{{cpp/locale/time_put/inherit}}
Line 36: Line 38:
 
===Example===
 
===Example===
 
{{example
 
{{example
| This example prints current time using the "C" locale with the time_put facet replaced by various time_put_byname facets
+
|Prints current time using the "C" locale with the {{tt|time_put}} facet replaced by various {{tt|std::time_put_byname}} facets. The result shown was obtained using the clang compiler.
| code=
+
|code=
#include <iostream>
+
#include <codecvt>
 
#include <ctime>
 
#include <ctime>
 
#include <iomanip>
 
#include <iomanip>
#include <codecvt>
+
#include <iostream>
 
   
 
   
 
int main()
 
int main()
 
{
 
{
     std::time_t t = std::time(NULL);
+
     std::time_t t = std::time(nullptr);
 
     std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv(std::cout.rdbuf());
 
     std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv(std::cout.rdbuf());
 
     std::wostream out(&conv);
 
     std::wostream out(&conv);
 
+
   
 
     out.imbue(std::locale(out.getloc(),
 
     out.imbue(std::locale(out.getloc(),
                           new std::time_put_byname<wchar_t>("ja_JP")));
+
                           new std::time_put_byname<wchar_t>("ja_JP.utf8")));
 
     out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
 
     out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
 
+
   
 
     out.imbue(std::locale(out.getloc(),
 
     out.imbue(std::locale(out.getloc(),
                        new std::time_put_byname<wchar_t>("ru_RU.utf8")));
+
                          new std::time_put_byname<wchar_t>("ru_RU.utf8")));
     out << std::put_time(std::localtime(&t), L"%A %c") << '\n';  
+
     out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
 
+
   
 
     out.imbue(std::locale(out.getloc(),
 
     out.imbue(std::locale(out.getloc(),
                        new std::time_put_byname<wchar_t>("sv_SE.utf8")));
+
                          new std::time_put_byname<wchar_t>("sv_SE.utf8")));
     out << std::put_time(std::localtime(&t), L"%A %c") << '\n';  
+
     out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
木曜日 2012年08月09日 21時41分02秒
+
木曜日 2023年10月05日 19時44分51秒
Четверг Чт. 09 авг. 2012 21:41:02
+
Четверг Чт 05 окт 2023 19:44:51
torsdag tor  9 aug 2012 21:41:02
+
torsdag tor  5 okt 2023 19:44:51
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/locale/dcl list time_put}}
+
{{dsc inc|cpp/locale/dsc time_put}}
{{dcl list end}}
+
{{dsc end}}
  
[[de:cpp/locale/time put byname]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/locale/time put byname]]
+
[[fr:cpp/locale/time put byname]]
+
[[it:cpp/locale/time put byname]]
+
[[ja:cpp/locale/time put byname]]
+
[[pt:cpp/locale/time put byname]]
+
[[ru:cpp/locale/time put byname]]
+
[[zh:cpp/locale/time put byname]]
+

Latest revision as of 11:46, 5 October 2023

 
 
 
Defined in header <locale>
template<

    class CharT,
    class OutputIt = std::ostreambuf_iterator<CharT>

> class time_put_byname : public std::time_put<CharT, OutputIt>;

std::time_put_byname is a std::time_put facet which encapsulates time and date formatting rules of the locale specified at its construction.

Contents

[edit] Specializations

The standard library is guaranteed to provide every specialization that satisfies the following type requirements:

  • CharT is one of char and wchar_t, and
  • OutputIt must meet the requirements of LegacyOutputIterator.

[edit] Member types

Member type Definition
char_type CharT
iter_type OutputIt

[edit] Member functions

(constructor)
constructs a new time_put_byname facet
(public member function) [edit]
(destructor)
destroys a time_put_byname facet
(protected member function) [edit]

std::time_put_byname::time_put_byname

explicit time_put_byname( const char* name, std::size_t refs = 0 );
explicit time_put_byname( const std::string& name, std::size_t refs = 0 );
(since C++11)

Constructs a new std::time_put_byname facet for a locale with name.

refs is used for resource management: if refs == 0, the implementation destroys the facet, when the last std::locale object holding it is destroyed. Otherwise, the object is not destroyed.

Parameters

name - the name of the locale
refs - the number of references that link to the facet

std::time_put_byname::~time_put_byname

protected:
~time_put_byname();

Destroys the facet.

Inherited from std::time_put

Member objects

Member name Type
id (static) std::locale::id

Member functions

invokes do_put
(public member function of std::time_put<CharT,OutputIt>) [edit]

Protected member functions

[virtual]
formats date/time and writes to output stream
(virtual protected member function of std::time_put<CharT,OutputIt>) [edit]

[edit] Example

Prints current time using the "C" locale with the time_put facet replaced by various std::time_put_byname facets. The result shown was obtained using the clang compiler.

#include <codecvt>
#include <ctime>
#include <iomanip>
#include <iostream>
 
int main()
{
    std::time_t t = std::time(nullptr);
    std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv(std::cout.rdbuf());
    std::wostream out(&conv);
 
    out.imbue(std::locale(out.getloc(),
                          new std::time_put_byname<wchar_t>("ja_JP.utf8")));
    out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
 
    out.imbue(std::locale(out.getloc(),
                          new std::time_put_byname<wchar_t>("ru_RU.utf8")));
    out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
 
    out.imbue(std::locale(out.getloc(),
                          new std::time_put_byname<wchar_t>("sv_SE.utf8")));
    out << std::put_time(std::localtime(&t), L"%A %c") << '\n';
}

Possible output:

木曜日 2023年10月05日 19時44分51秒
Четверг Чт 05 окт 2023 19:44:51
torsdag tor  5 okt 2023 19:44:51

[edit] See also

formats contents of std::tm for output as character sequence
(class template) [edit]