template< class CharT, class Streamable> std::basic_string<CharT> format(const CharT* fmt, const Streamable& s); | (1) | (since C++20) |
template<class CharT, class Streamable> std::basic_string<CharT> format(const std::locale& loc, const CharT* fmt, const Streamable& s); | (2) | (since C++20) |
template<class CharT, class Traits, class Alloc, class Streamable> std::basic_string<CharT, Traits, Alloc> format(const std::basic_string<CharT, Traits, Alloc>& fmt, const Streamable& s); | (3) | (since C++20) |
template<class CharT, class Traits, class Alloc, class Streamable> std::basic_string<CharT, Traits, Alloc> format(const std::locale& loc, const std::basic_string<CharT, Traits, Alloc>& fmt, const Streamable& s); | (4) | (since C++20) |
Formats the streamable object s
using the format string fmt
and the locale loc
(if provided).
These function templates behave as if they.
std::basic_ostringstream<CharT, Traits, Alloc>
os
(where Traits
is std::char_traits<CharT>
and Alloc
is std::allocator<CharT>
for (1,2)); os.exceptions(std::ios_base::failbit|std::ios_base::badbit)
; os.imbue(loc)
(for overloads (2,4) only); to_stream(os, fmt, s)
(for (1,2)) or to_stream(os, fmt.c_str(), s)
(for (3,4)), where to_stream
is called unqualified to enable argument-dependent lookup; os.str()
. to_stream(std::declval<std::basic_ostream<CharT>&>(), fmt, s)
is a valid expression.to_stream(std::declval<std::basic_ostringstream<CharT, Traits, Alloc>&>(), fmt.c_str(), s)
is a valid expression.The format string consists of zero or more conversion specifiers and ordinary characters. All ordinary characters, excluding the terminating null character, are written into the output stream without modification. 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 an E
or O
modifier character is inserted after the %
character. Each conversion specifier is replaced by appropriate characters in the output as described below.
A character sequence in the format string that begins with a %
but does not match one of the format specifiers below is interpreted as ordinary characters.
The following format specifiers are available:
Conversion specifier | Explanation | |
---|---|---|
%% | Writes a literal % character. |
|
%n | Writes a newline character. | |
%t | Writes a horizontal tab character. | |
Year | ||
%C %EC | Writes the year divided by 100 using floored division. If the result is a single decimal digit, it is prefixed with 0. The modified command |
|
%y | Writes the last two decimal digits of the year. If the result is a single digit it is prefixed by 0. | |
%Y | Writes the year as a decimal number. If the result is less than four digits it is left-padded with 0 to four digits. | |
Month | ||
%b %h | Writes the locale's abbreviated month name. | |
%B | Writes the locale's full month name. | |
%m %Om | Writes the month as a decimal number (January is 01 ). If the result is a single digit, it is prefixed with 0. The modified command |
|
Day | ||
%d %Od | Writes the day of month as a decimal number. If the result is a single decimal digit, it is prefixed with 0. The modified command |
|
%e %Oe | Writes the day of month as a decimal number. If the result is a single decimal digit, it is prefixed with a space. The modified command |
|
Day of the week | ||
%a | Writes the locale's abbreviated weekday name. | |
%A | Writes the locale's full weekday name. | |
%u %Ou | Writes the ISO weekday as a decimal number (1-7), where Monday is 1 . The modified command |
|
%w %Ow | Writes the weekday as a decimal number (0-6), where Sunday is 0 . The modified command |
|
ISO 8601 week-based year | ||
In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
|
||
%g | Writes the last two decimal digits of the ISO 8601 week-based year. If the result is a single digit it is prefixed by 0. | |
%G | Writes the ISO 8601 week-based year as a decimal number. If the result is less than four digits it is left-padded with 0 to four digits. | |
%V %OV | Writes the ISO 8601 week of the year as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command |
|
Week/day of the year | ||
%j | Writes the day of the year as a decimal number (January 1 is 001 ). If the result is less than three digits, it is left-padded with 0 to three digits. |
|
%U %OU | Writes 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. If the result is a single digit, it is prefixed with 0. The modified command |
|
%W %OW | Writes 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. If the result is a single digit, it is prefixed with 0. The modified command |
|
Date | ||
%D | Equivalent to "%m/%d/%y" . |
|
%F | Equivalent to "%Y-%m-%d" . |
|
%x %Ex | Writes the locale's date representation. The modified command |
|
Time of day | ||
%H %OH | Writes the hour (24-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command |
|
%I %OI | Writes the hour (12-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command |
|
%M %OM | Writes the minute as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command |
|
%S %OS | Writes the second as a decimal number. If the number of seconds is less than 10, the result is prefixed with 0. If the precision of the input cannot be exactly represented with seconds, then the format is a decimal floating point number with a fixed format and a precision matching that of the precision of the input (or to a microseconds precision if the conversion to floating point decimal seconds cannot be made within 18 fractional digits). The character for the decimal point is localized according to the locale. The modified command |
|
%p | Writes 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 | Writes the locale's 12-hour clock time. | |
%X %EX | Writes the locale's time representation. The modified command |
|
Miscellaneous | ||
%c %Ec | Writes the locale's date and time representation. The modified command |
|
%z %Ez %Oz | Writes the offset from UTC in the ISO 8601 format. For example -0430 refers to 4 hours 30 minutes behind UTC. If the offset is zero, +0000 is used. The modified commands |
|
%Z | Writes the time zone abbreviation. |
(C++20) | outputs a sys_time into a stream according to the provided format (function template) |
(C++20) | outputs a utc_time into a stream according to the provided format (function template) |
(C++20) | outputs a tai_time into a stream according to the provided format (function template) |
(C++20) | outputs a gps_time into a stream according to the provided format (function template) |
(C++20) | outputs a file_time into a stream according to the provided format (function template) |
(C++20) | outputs a local_time into a stream according to the provided format (function template) |
outputs a year into a stream according to the provided format (function template) |
|
outputs a month into a stream according to the provided format (function template) |
|
outputs a day into a stream according to the provided format (function template) |
|
outputs a weekday into a stream according to the provided format (function template) |
|
outputs a month_day into a stream according to the provided format (function template) |
|
outputs a year_month into a stream according to the provided format (function template) |
|
outputs a year_month_day into a stream according to the provided format (function template) |
|
outputs a zoned_time into a stream according to the provided format (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/chrono/format