Mercurial > minori
comparison dep/fmt/doc/html/syntax.html @ 343:1faa72660932
*: transfer back to cmake from autotools
autotools just made lots of things more complicated than
they should have and many things broke (i.e. translations)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 20 Jun 2024 05:56:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
342:adb79bdde329 | 343:1faa72660932 |
---|---|
1 <!DOCTYPE html> | |
2 | |
3 | |
4 <html lang="en"> | |
5 <head> | |
6 <meta charset="utf-8"> | |
7 <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
8 <meta name="viewport" content="width=device-width, initial-scale=1"> | |
9 | |
10 <meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> | |
11 | |
12 <title>Format String Syntax — fmt 10.2.1 documentation</title> | |
13 | |
14 <link rel="stylesheet" href="_static/basic.css" type="text/css" /> | |
15 <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> | |
16 <link rel="stylesheet" href="_static/breathe.css" type="text/css" /> | |
17 | |
18 <script type="text/javascript"> | |
19 var DOCUMENTATION_OPTIONS = { | |
20 URL_ROOT: './', | |
21 VERSION: '10.2.1', | |
22 COLLAPSE_INDEX: false, | |
23 FILE_SUFFIX: '.html', | |
24 LINK_SUFFIX: '.html', | |
25 SOURCELINK_SUFFIX: '.txt', | |
26 HAS_SOURCE: true, | |
27 SOURCELINK_SUFFIX: '.txt' | |
28 }; | |
29 </script> | |
30 <script src="_static/jquery.js"></script> | |
31 <script src="_static/underscore.js"></script> | |
32 <script src="_static/doctools.js"></script> | |
33 <script src="_static/language_data.js"></script> | |
34 <link rel="index" title="Index" href="genindex.html" /> | |
35 <link rel="search" title="Search" href="search.html" /> | |
36 <meta name="description" content="Small, safe and fast formatting library"> | |
37 <meta name="keywords" content="C++, formatting, printf, string, library"> | |
38 <meta name="author" content="Victor Zverovich"> | |
39 <link rel="stylesheet" href="_static/fmt.css"> | |
40 | |
41 <script async src="https://www.googletagmanager.com/gtag/js?id=UA-20116650-4"></script> | |
42 <script> | |
43 window.dataLayer = window.dataLayer || []; | |
44 function gtag(){dataLayer.push(arguments);} | |
45 gtag('js', new Date()); | |
46 | |
47 gtag('config', 'UA-20116650-4'); | |
48 </script> | |
49 | |
50 </head> | |
51 <body role="document"> | |
52 <nav class="navbar navbar-inverse"> | |
53 <div class="tb-container"> | |
54 <div class="row"> | |
55 <div class="navbar-content"> | |
56 | |
57 <div class="navbar-header"> | |
58 <button type="button" class="navbar-toggle collapsed" | |
59 data-toggle="collapse" data-target=".navbar-collapse"> | |
60 <span class="sr-only">Toggle navigation</span> | |
61 <span class="icon-bar"></span> | |
62 <span class="icon-bar"></span> | |
63 <span class="icon-bar"></span> | |
64 </button> | |
65 <a class="navbar-brand" href="index.html">{fmt}</a> | |
66 </div> | |
67 | |
68 | |
69 <div class="collapse navbar-collapse"> | |
70 <ul class="nav navbar-nav"> | |
71 <li class="dropdown"> | |
72 <a href="#" class="dropdown-toggle" data-toggle="dropdown" | |
73 role="button" aria-expanded="false">10.2.1 | |
74 <span class="caret"></span></a> | |
75 <ul class="dropdown-menu" role="menu"> | |
76 | |
77 <li><a href="https://fmt.dev/10.2.1">10.2.1</a></li> | |
78 | |
79 <li><a href="https://fmt.dev/10.2.0">10.2.0</a></li> | |
80 | |
81 <li><a href="https://fmt.dev/10.1.1">10.1.1</a></li> | |
82 | |
83 </ul> | |
84 </li> | |
85 | |
86 | |
87 <li><a href="contents.html">Contents</a></li> | |
88 | |
89 | |
90 | |
91 <li><a href="usage.html">Usage</a></li> | |
92 | |
93 | |
94 | |
95 <li><a href="api.html">API</a></li> | |
96 | |
97 | |
98 | |
99 <li class="active"><a href="syntax.html">Syntax | |
100 <span class="sr-only">(current)</span></a></li> | |
101 | |
102 | |
103 </ul> | |
104 | |
105 | |
106 <form class="navbar-form navbar-right" role="search" action="search.html" | |
107 method="get"> | |
108 <div class="form-group"> | |
109 <input type="text" name="q" class="form-control" | |
110 placeholder="Search" > | |
111 </div> | |
112 <input type="hidden" name="check_keywords" value="yes" /> | |
113 <input type="hidden" name="area" value="default" /> | |
114 | |
115 </form> | |
116 | |
117 </div> | |
118 </div> | |
119 </div> | |
120 </div> | |
121 </nav> | |
122 | |
123 | |
124 | |
125 <div class="tb-container"> | |
126 <div class="row"> | |
127 | |
128 | |
129 <div class="content"> | |
130 | |
131 <section id="format-string-syntax"> | |
132 <span id="syntax"></span><h1>Format String Syntax<a class="headerlink" href="#format-string-syntax" title="Permalink to this headline">¶</a></h1> | |
133 <p>Formatting functions such as <a class="reference internal" href="api.html#format"><span class="std std-ref">fmt::format()</span></a> and | |
134 <a class="reference internal" href="api.html#print"><span class="std std-ref">fmt::print()</span></a> use the same format string syntax described in this | |
135 section.</p> | |
136 <p>Format strings contain “replacement fields” surrounded by curly braces <code class="docutils literal notranslate"><span class="pre">{}</span></code>. | |
137 Anything that is not contained in braces is considered literal text, which is | |
138 copied unchanged to the output. If you need to include a brace character in the | |
139 literal text, it can be escaped by doubling: <code class="docutils literal notranslate"><span class="pre">{{</span></code> and <code class="docutils literal notranslate"><span class="pre">}}</span></code>.</p> | |
140 <p>The grammar for a replacement field is as follows:</p> | |
141 <pre> | |
142 <strong id="grammar-token-sf-replacement_field"><span id="grammar-token-replacement-field"></span>replacement_field</strong> ::= "{" [<a class="reference internal" href="#grammar-token-sf-arg_id"><code class="xref docutils literal notranslate"><span class="pre">arg_id</span></code></a>] [":" (<a class="reference internal" href="#grammar-token-sf-format_spec"><code class="xref docutils literal notranslate"><span class="pre">format_spec</span></code></a> | <a class="reference internal" href="#grammar-token-sf-chrono_format_spec"><code class="xref docutils literal notranslate"><span class="pre">chrono_format_spec</span></code></a>)] "}" | |
143 <strong id="grammar-token-sf-arg_id"><span id="grammar-token-arg-id"></span>arg_id </strong> ::= <a class="reference internal" href="#grammar-token-sf-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | <a class="reference internal" href="#grammar-token-sf-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> | |
144 <strong id="grammar-token-sf-integer"><span id="grammar-token-integer"></span>integer </strong> ::= <a class="reference internal" href="#grammar-token-sf-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>+ | |
145 <strong id="grammar-token-sf-digit"><span id="grammar-token-digit"></span>digit </strong> ::= "0"..."9" | |
146 <strong id="grammar-token-sf-identifier"><span id="grammar-token-identifier"></span>identifier </strong> ::= <a class="reference internal" href="#grammar-token-sf-id_start"><code class="xref docutils literal notranslate"><span class="pre">id_start</span></code></a> <a class="reference internal" href="#grammar-token-sf-id_continue"><code class="xref docutils literal notranslate"><span class="pre">id_continue</span></code></a>* | |
147 <strong id="grammar-token-sf-id_start"><span id="grammar-token-id-start"></span>id_start </strong> ::= "a"..."z" | "A"..."Z" | "_" | |
148 <strong id="grammar-token-sf-id_continue"><span id="grammar-token-id-continue"></span>id_continue </strong> ::= <a class="reference internal" href="#grammar-token-sf-id_start"><code class="xref docutils literal notranslate"><span class="pre">id_start</span></code></a> | <a class="reference internal" href="#grammar-token-sf-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a> | |
149 </pre> | |
150 <p>In less formal terms, the replacement field can start with an <em>arg_id</em> | |
151 that specifies the argument whose value is to be formatted and inserted into | |
152 the output instead of the replacement field. | |
153 The <em>arg_id</em> is optionally followed by a <em>format_spec</em>, which is preceded by a | |
154 colon <code class="docutils literal notranslate"><span class="pre">':'</span></code>. These specify a non-default format for the replacement value.</p> | |
155 <p>See also the <a class="reference internal" href="#formatspec"><span class="std std-ref">Format Specification Mini-Language</span></a> section.</p> | |
156 <p>If the numerical arg_ids in a format string are 0, 1, 2, … in sequence, | |
157 they can all be omitted (not just some) and the numbers 0, 1, 2, … will be | |
158 automatically inserted in that order.</p> | |
159 <p>Named arguments can be referred to by their names or indices.</p> | |
160 <p>Some simple format string examples:</p> | |
161 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="s">"First, thou shalt count to {0}"</span><span class="w"> </span><span class="c1">// References the first argument</span> | |
162 <span class="s">"Bring me a {}"</span><span class="w"> </span><span class="c1">// Implicitly references the first argument</span> | |
163 <span class="s">"From {} to {}"</span><span class="w"> </span><span class="c1">// Same as "From {0} to {1}"</span> | |
164 </pre></div> | |
165 </div> | |
166 <p>The <em>format_spec</em> field contains a specification of how the value should be | |
167 presented, including such details as field width, alignment, padding, decimal | |
168 precision and so on. Each value type can define its own “formatting | |
169 mini-language” or interpretation of the <em>format_spec</em>.</p> | |
170 <p>Most built-in types support a common formatting mini-language, which is | |
171 described in the next section.</p> | |
172 <p>A <em>format_spec</em> field can also include nested replacement fields in certain | |
173 positions within it. These nested replacement fields can contain only an | |
174 argument id; format specifications are not allowed. This allows the formatting | |
175 of a value to be dynamically specified.</p> | |
176 <p>See the <a class="reference internal" href="#formatexamples"><span class="std std-ref">Format Examples</span></a> section for some examples.</p> | |
177 <section id="format-specification-mini-language"> | |
178 <span id="formatspec"></span><h2>Format Specification Mini-Language<a class="headerlink" href="#format-specification-mini-language" title="Permalink to this headline">¶</a></h2> | |
179 <p>“Format specifications” are used within replacement fields contained within a | |
180 format string to define how individual values are presented (see | |
181 <a class="reference internal" href="#syntax"><span class="std std-ref">Format String Syntax</span></a>). Each formattable type may define how the format | |
182 specification is to be interpreted.</p> | |
183 <p>Most built-in types implement the following options for format specifications, | |
184 although some of the formatting options are only supported by the numeric types.</p> | |
185 <p>The general form of a <em>standard format specifier</em> is:</p> | |
186 <pre> | |
187 <strong id="grammar-token-sf-format_spec"><span id="grammar-token-format-spec"></span>format_spec</strong> ::= [[<a class="reference internal" href="#grammar-token-sf-fill"><code class="xref docutils literal notranslate"><span class="pre">fill</span></code></a>]<a class="reference internal" href="#grammar-token-sf-align"><code class="xref docutils literal notranslate"><span class="pre">align</span></code></a>][<a class="reference internal" href="#grammar-token-sf-sign"><code class="xref docutils literal notranslate"><span class="pre">sign</span></code></a>]["#"]["0"][<a class="reference internal" href="#grammar-token-sf-width"><code class="xref docutils literal notranslate"><span class="pre">width</span></code></a>]["." <a class="reference internal" href="#grammar-token-sf-precision"><code class="xref docutils literal notranslate"><span class="pre">precision</span></code></a>]["L"][<a class="reference internal" href="#grammar-token-sf-type"><code class="xref docutils literal notranslate"><span class="pre">type</span></code></a>] | |
188 <strong id="grammar-token-sf-fill"><span id="grammar-token-fill"></span>fill </strong> ::= <a character other than '{' or '}'> | |
189 <strong id="grammar-token-sf-align"><span id="grammar-token-align"></span>align </strong> ::= "<" | ">" | "^" | |
190 <strong id="grammar-token-sf-sign"><span id="grammar-token-sign"></span>sign </strong> ::= "+" | "-" | " " | |
191 <strong id="grammar-token-sf-width"><span id="grammar-token-width"></span>width </strong> ::= <a class="reference internal" href="#grammar-token-sf-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | "{" [<a class="reference internal" href="#grammar-token-sf-arg_id"><code class="xref docutils literal notranslate"><span class="pre">arg_id</span></code></a>] "}" | |
192 <strong id="grammar-token-sf-precision"><span id="grammar-token-precision"></span>precision </strong> ::= <a class="reference internal" href="#grammar-token-sf-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | "{" [<a class="reference internal" href="#grammar-token-sf-arg_id"><code class="xref docutils literal notranslate"><span class="pre">arg_id</span></code></a>] "}" | |
193 <strong id="grammar-token-sf-type"><span id="grammar-token-type"></span>type </strong> ::= "a" | "A" | "b" | "B" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | | |
194 "o" | "p" | "s" | "x" | "X" | "?" | |
195 </pre> | |
196 <p>The <em>fill</em> character can be any Unicode code point other than <code class="docutils literal notranslate"><span class="pre">'{'</span></code> or | |
197 <code class="docutils literal notranslate"><span class="pre">'}'</span></code>. The presence of a fill character is signaled by the character following | |
198 it, which must be one of the alignment options. If the second character of | |
199 <em>format_spec</em> is not a valid alignment option, then it is assumed that both the | |
200 fill character and the alignment option are absent.</p> | |
201 <p>The meaning of the various alignment options is as follows:</p> | |
202 <table class="docutils align-default"> | |
203 <colgroup> | |
204 <col style="width: 13%" /> | |
205 <col style="width: 87%" /> | |
206 </colgroup> | |
207 <thead> | |
208 <tr class="row-odd"><th class="head"><p>Option</p></th> | |
209 <th class="head"><p>Meaning</p></th> | |
210 </tr> | |
211 </thead> | |
212 <tbody> | |
213 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'<'</span></code></p></td> | |
214 <td><p>Forces the field to be left-aligned within the available | |
215 space (this is the default for most objects).</p></td> | |
216 </tr> | |
217 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'>'</span></code></p></td> | |
218 <td><p>Forces the field to be right-aligned within the | |
219 available space (this is the default for numbers).</p></td> | |
220 </tr> | |
221 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'^'</span></code></p></td> | |
222 <td><p>Forces the field to be centered within the available | |
223 space.</p></td> | |
224 </tr> | |
225 </tbody> | |
226 </table> | |
227 <p>Note that unless a minimum field width is defined, the field width will always | |
228 be the same size as the data to fill it, so that the alignment option has no | |
229 meaning in this case.</p> | |
230 <p>The <em>sign</em> option is only valid for floating point and signed integer types, | |
231 and can be one of the following:</p> | |
232 <table class="docutils align-default"> | |
233 <colgroup> | |
234 <col style="width: 13%" /> | |
235 <col style="width: 87%" /> | |
236 </colgroup> | |
237 <thead> | |
238 <tr class="row-odd"><th class="head"><p>Option</p></th> | |
239 <th class="head"><p>Meaning</p></th> | |
240 </tr> | |
241 </thead> | |
242 <tbody> | |
243 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'+'</span></code></p></td> | |
244 <td><p>indicates that a sign should be used for both | |
245 nonnegative as well as negative numbers.</p></td> | |
246 </tr> | |
247 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'-'</span></code></p></td> | |
248 <td><p>indicates that a sign should be used only for negative | |
249 numbers (this is the default behavior).</p></td> | |
250 </tr> | |
251 <tr class="row-even"><td><p>space</p></td> | |
252 <td><p>indicates that a leading space should be used on | |
253 nonnegative numbers, and a minus sign on negative numbers.</p></td> | |
254 </tr> | |
255 </tbody> | |
256 </table> | |
257 <p>The <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option causes the “alternate form” to be used for the | |
258 conversion. The alternate form is defined differently for different | |
259 types. This option is only valid for integer and floating-point types. | |
260 For integers, when binary, octal, or hexadecimal output is used, this | |
261 option adds the prefix respective <code class="docutils literal notranslate"><span class="pre">"0b"</span></code> (<code class="docutils literal notranslate"><span class="pre">"0B"</span></code>), <code class="docutils literal notranslate"><span class="pre">"0"</span></code>, or | |
262 <code class="docutils literal notranslate"><span class="pre">"0x"</span></code> (<code class="docutils literal notranslate"><span class="pre">"0X"</span></code>) to the output value. Whether the prefix is | |
263 lower-case or upper-case is determined by the case of the type | |
264 specifier, for example, the prefix <code class="docutils literal notranslate"><span class="pre">"0x"</span></code> is used for the type <code class="docutils literal notranslate"><span class="pre">'x'</span></code> | |
265 and <code class="docutils literal notranslate"><span class="pre">"0X"</span></code> is used for <code class="docutils literal notranslate"><span class="pre">'X'</span></code>. For floating-point numbers the | |
266 alternate form causes the result of the conversion to always contain a | |
267 decimal-point character, even if no digits follow it. Normally, a | |
268 decimal-point character appears in the result of these conversions | |
269 only if a digit follows it. In addition, for <code class="docutils literal notranslate"><span class="pre">'g'</span></code> and <code class="docutils literal notranslate"><span class="pre">'G'</span></code> | |
270 conversions, trailing zeros are not removed from the result.</p> | |
271 <p><em>width</em> is a decimal integer defining the minimum field width. If not | |
272 specified, then the field width will be determined by the content.</p> | |
273 <p>Preceding the <em>width</em> field by a zero (<code class="docutils literal notranslate"><span class="pre">'0'</span></code>) character enables sign-aware | |
274 zero-padding for numeric types. It forces the padding to be placed after the | |
275 sign or base (if any) but before the digits. This is used for printing fields in | |
276 the form ‘+000000120’. This option is only valid for numeric types and it has no | |
277 effect on formatting of infinity and NaN.</p> | |
278 <p>The <em>precision</em> is a decimal number indicating how many digits should be | |
279 displayed after the decimal point for a floating-point value formatted with | |
280 <code class="docutils literal notranslate"><span class="pre">'f'</span></code> and <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, or before and after the decimal point for a floating-point | |
281 value formatted with <code class="docutils literal notranslate"><span class="pre">'g'</span></code> or <code class="docutils literal notranslate"><span class="pre">'G'</span></code>. For non-number types the field | |
282 indicates the maximum field size - in other words, how many characters will be | |
283 used from the field content. The <em>precision</em> is not allowed for integer, | |
284 character, Boolean, and pointer values. Note that a C string must be | |
285 null-terminated even if precision is specified.</p> | |
286 <p>The <code class="docutils literal notranslate"><span class="pre">'L'</span></code> option uses the current locale setting to insert the appropriate | |
287 number separator characters. This option is only valid for numeric types.</p> | |
288 <p>Finally, the <em>type</em> determines how the data should be presented.</p> | |
289 <p>The available string presentation types are:</p> | |
290 <table class="docutils align-default"> | |
291 <colgroup> | |
292 <col style="width: 13%" /> | |
293 <col style="width: 87%" /> | |
294 </colgroup> | |
295 <thead> | |
296 <tr class="row-odd"><th class="head"><p>Type</p></th> | |
297 <th class="head"><p>Meaning</p></th> | |
298 </tr> | |
299 </thead> | |
300 <tbody> | |
301 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'s'</span></code></p></td> | |
302 <td><p>String format. This is the default type for strings and | |
303 may be omitted.</p></td> | |
304 </tr> | |
305 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'?'</span></code></p></td> | |
306 <td><p>Debug format. The string is quoted and special | |
307 characters escaped.</p></td> | |
308 </tr> | |
309 <tr class="row-even"><td><p>none</p></td> | |
310 <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'s'</span></code>.</p></td> | |
311 </tr> | |
312 </tbody> | |
313 </table> | |
314 <p>The available character presentation types are:</p> | |
315 <table class="docutils align-default"> | |
316 <colgroup> | |
317 <col style="width: 13%" /> | |
318 <col style="width: 87%" /> | |
319 </colgroup> | |
320 <thead> | |
321 <tr class="row-odd"><th class="head"><p>Type</p></th> | |
322 <th class="head"><p>Meaning</p></th> | |
323 </tr> | |
324 </thead> | |
325 <tbody> | |
326 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td> | |
327 <td><p>Character format. This is the default type for | |
328 characters and may be omitted.</p></td> | |
329 </tr> | |
330 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'?'</span></code></p></td> | |
331 <td><p>Debug format. The character is quoted and special | |
332 characters escaped.</p></td> | |
333 </tr> | |
334 <tr class="row-even"><td><p>none</p></td> | |
335 <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'c'</span></code>.</p></td> | |
336 </tr> | |
337 </tbody> | |
338 </table> | |
339 <p>The available integer presentation types are:</p> | |
340 <table class="docutils align-default"> | |
341 <colgroup> | |
342 <col style="width: 13%" /> | |
343 <col style="width: 87%" /> | |
344 </colgroup> | |
345 <thead> | |
346 <tr class="row-odd"><th class="head"><p>Type</p></th> | |
347 <th class="head"><p>Meaning</p></th> | |
348 </tr> | |
349 </thead> | |
350 <tbody> | |
351 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td> | |
352 <td><p>Binary format. Outputs the number in base 2. Using the | |
353 <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">"0b"</span></code> | |
354 to the output value.</p></td> | |
355 </tr> | |
356 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td> | |
357 <td><p>Binary format. Outputs the number in base 2. Using the | |
358 <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">"0B"</span></code> | |
359 to the output value.</p></td> | |
360 </tr> | |
361 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td> | |
362 <td><p>Character format. Outputs the number as a character.</p></td> | |
363 </tr> | |
364 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td> | |
365 <td><p>Decimal integer. Outputs the number in base 10.</p></td> | |
366 </tr> | |
367 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'o'</span></code></p></td> | |
368 <td><p>Octal format. Outputs the number in base 8.</p></td> | |
369 </tr> | |
370 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'x'</span></code></p></td> | |
371 <td><p>Hex format. Outputs the number in base 16, using | |
372 lower-case letters for the digits above 9. Using the | |
373 <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">"0x"</span></code> | |
374 to the output value.</p></td> | |
375 </tr> | |
376 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'X'</span></code></p></td> | |
377 <td><p>Hex format. Outputs the number in base 16, using | |
378 upper-case letters for the digits above 9. Using the | |
379 <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">"0X"</span></code> | |
380 to the output value.</p></td> | |
381 </tr> | |
382 <tr class="row-odd"><td><p>none</p></td> | |
383 <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'d'</span></code>.</p></td> | |
384 </tr> | |
385 </tbody> | |
386 </table> | |
387 <p>Integer presentation types can also be used with character and Boolean values | |
388 with the only exception that <code class="docutils literal notranslate"><span class="pre">'c'</span></code> cannot be used with <code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">bool</span></code>. Boolean values | |
389 are formatted using textual representation, either <code class="docutils literal notranslate"><span class="pre">true</span></code> or <code class="docutils literal notranslate"><span class="pre">false</span></code>, if the | |
390 presentation type is not specified.</p> | |
391 <p>The available presentation types for floating-point values are:</p> | |
392 <table class="docutils align-default"> | |
393 <colgroup> | |
394 <col style="width: 13%" /> | |
395 <col style="width: 87%" /> | |
396 </colgroup> | |
397 <thead> | |
398 <tr class="row-odd"><th class="head"><p>Type</p></th> | |
399 <th class="head"><p>Meaning</p></th> | |
400 </tr> | |
401 </thead> | |
402 <tbody> | |
403 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'a'</span></code></p></td> | |
404 <td><p>Hexadecimal floating point format. Prints the number in | |
405 base 16 with prefix <code class="docutils literal notranslate"><span class="pre">"0x"</span></code> and lower-case letters for | |
406 digits above 9. Uses <code class="docutils literal notranslate"><span class="pre">'p'</span></code> to indicate the exponent.</p></td> | |
407 </tr> | |
408 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'A'</span></code></p></td> | |
409 <td><p>Same as <code class="docutils literal notranslate"><span class="pre">'a'</span></code> except it uses upper-case letters for | |
410 the prefix, digits above 9 and to indicate the exponent.</p></td> | |
411 </tr> | |
412 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'e'</span></code></p></td> | |
413 <td><p>Exponent notation. Prints the number in scientific | |
414 notation using the letter ‘e’ to indicate the exponent.</p></td> | |
415 </tr> | |
416 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'E'</span></code></p></td> | |
417 <td><p>Exponent notation. Same as <code class="docutils literal notranslate"><span class="pre">'e'</span></code> except it uses an | |
418 upper-case <code class="docutils literal notranslate"><span class="pre">'E'</span></code> as the separator character.</p></td> | |
419 </tr> | |
420 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td> | |
421 <td><p>Fixed point. Displays the number as a fixed-point | |
422 number.</p></td> | |
423 </tr> | |
424 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'F'</span></code></p></td> | |
425 <td><p>Fixed point. Same as <code class="docutils literal notranslate"><span class="pre">'f'</span></code>, but converts <code class="docutils literal notranslate"><span class="pre">nan</span></code> to | |
426 <code class="docutils literal notranslate"><span class="pre">NAN</span></code> and <code class="docutils literal notranslate"><span class="pre">inf</span></code> to <code class="docutils literal notranslate"><span class="pre">INF</span></code>.</p></td> | |
427 </tr> | |
428 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'g'</span></code></p></td> | |
429 <td><p>General format. For a given precision <code class="docutils literal notranslate"><span class="pre">p</span> <span class="pre">>=</span> <span class="pre">1</span></code>, | |
430 this rounds the number to <code class="docutils literal notranslate"><span class="pre">p</span></code> significant digits and | |
431 then formats the result in either fixed-point format | |
432 or in scientific notation, depending on its magnitude.</p> | |
433 <p>A precision of <code class="docutils literal notranslate"><span class="pre">0</span></code> is treated as equivalent to a | |
434 precision of <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p> | |
435 </td> | |
436 </tr> | |
437 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'G'</span></code></p></td> | |
438 <td><p>General format. Same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code> except switches to | |
439 <code class="docutils literal notranslate"><span class="pre">'E'</span></code> if the number gets too large. The | |
440 representations of infinity and NaN are uppercased, too.</p></td> | |
441 </tr> | |
442 <tr class="row-even"><td><p>none</p></td> | |
443 <td><p>Similar to <code class="docutils literal notranslate"><span class="pre">'g'</span></code>, except that the default precision is | |
444 as high as needed to represent the particular value.</p></td> | |
445 </tr> | |
446 </tbody> | |
447 </table> | |
448 <p>The available presentation types for pointers are:</p> | |
449 <table class="docutils align-default"> | |
450 <colgroup> | |
451 <col style="width: 13%" /> | |
452 <col style="width: 87%" /> | |
453 </colgroup> | |
454 <thead> | |
455 <tr class="row-odd"><th class="head"><p>Type</p></th> | |
456 <th class="head"><p>Meaning</p></th> | |
457 </tr> | |
458 </thead> | |
459 <tbody> | |
460 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'p'</span></code></p></td> | |
461 <td><p>Pointer format. This is the default type for | |
462 pointers and may be omitted.</p></td> | |
463 </tr> | |
464 <tr class="row-odd"><td><p>none</p></td> | |
465 <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'p'</span></code>.</p></td> | |
466 </tr> | |
467 </tbody> | |
468 </table> | |
469 </section> | |
470 <section id="chrono-format-specifications"> | |
471 <span id="chrono-specs"></span><h2>Chrono Format Specifications<a class="headerlink" href="#chrono-format-specifications" title="Permalink to this headline">¶</a></h2> | |
472 <p>Format specifications for chrono duration and time point types as well as | |
473 <code class="docutils literal notranslate"><span class="pre">std::tm</span></code> have the following syntax:</p> | |
474 <pre> | |
475 <strong id="grammar-token-sf-chrono_format_spec"><span id="grammar-token-chrono-format-spec"></span>chrono_format_spec</strong> ::= [[<a class="reference internal" href="#grammar-token-sf-fill"><code class="xref docutils literal notranslate"><span class="pre">fill</span></code></a>]<a class="reference internal" href="#grammar-token-sf-align"><code class="xref docutils literal notranslate"><span class="pre">align</span></code></a>][<a class="reference internal" href="#grammar-token-sf-width"><code class="xref docutils literal notranslate"><span class="pre">width</span></code></a>]["." <a class="reference internal" href="#grammar-token-sf-precision"><code class="xref docutils literal notranslate"><span class="pre">precision</span></code></a>][<a class="reference internal" href="#grammar-token-sf-chrono_specs"><code class="xref docutils literal notranslate"><span class="pre">chrono_specs</span></code></a>] | |
476 <strong id="grammar-token-sf-chrono_specs"><span id="grammar-token-chrono-specs"></span>chrono_specs </strong> ::= [<a class="reference internal" href="#grammar-token-sf-chrono_specs"><code class="xref docutils literal notranslate"><span class="pre">chrono_specs</span></code></a>] <a class="reference internal" href="#grammar-token-sf-conversion_spec"><code class="xref docutils literal notranslate"><span class="pre">conversion_spec</span></code></a> | <a class="reference internal" href="#grammar-token-sf-chrono_specs"><code class="xref docutils literal notranslate"><span class="pre">chrono_specs</span></code></a> <a class="reference internal" href="#grammar-token-sf-literal_char"><code class="xref docutils literal notranslate"><span class="pre">literal_char</span></code></a> | |
477 <strong id="grammar-token-sf-conversion_spec"><span id="grammar-token-conversion-spec"></span>conversion_spec </strong> ::= "%" [<a class="reference internal" href="#grammar-token-sf-modifier"><code class="xref docutils literal notranslate"><span class="pre">modifier</span></code></a>] <a class="reference internal" href="#grammar-token-sf-chrono_type"><code class="xref docutils literal notranslate"><span class="pre">chrono_type</span></code></a> | |
478 <strong id="grammar-token-sf-literal_char"><span id="grammar-token-literal-char"></span>literal_char </strong> ::= <a character other than '{', '}' or '%'> | |
479 <strong id="grammar-token-sf-modifier"><span id="grammar-token-modifier"></span>modifier </strong> ::= "E" | "O" | |
480 <strong id="grammar-token-sf-chrono_type"><span id="grammar-token-chrono-type"></span>chrono_type </strong> ::= "a" | "A" | "b" | "B" | "c" | "C" | "d" | "D" | "e" | "F" | | |
481 "g" | "G" | "h" | "H" | "I" | "j" | "m" | "M" | "n" | "p" | | |
482 "q" | "Q" | "r" | "R" | "S" | "t" | "T" | "u" | "U" | "V" | | |
483 "w" | "W" | "x" | "X" | "y" | "Y" | "z" | "Z" | "%" | |
484 </pre> | |
485 <p>Literal chars are copied unchanged to the output. Precision is valid only for | |
486 <code class="docutils literal notranslate"><span class="pre">std::chrono::duration</span></code> types with a floating-point representation type.</p> | |
487 <p>The available presentation types (<em>chrono_type</em>) are:</p> | |
488 <table class="docutils align-default"> | |
489 <colgroup> | |
490 <col style="width: 12%" /> | |
491 <col style="width: 88%" /> | |
492 </colgroup> | |
493 <thead> | |
494 <tr class="row-odd"><th class="head"><p>Type</p></th> | |
495 <th class="head"><p>Meaning</p></th> | |
496 </tr> | |
497 </thead> | |
498 <tbody> | |
499 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'a'</span></code></p></td> | |
500 <td><p>The abbreviated weekday name, e.g. “Sat”. If the value does not | |
501 contain a valid weekday, an exception of type <code class="docutils literal notranslate"><span class="pre">format_error</span></code> is | |
502 thrown.</p></td> | |
503 </tr> | |
504 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'A'</span></code></p></td> | |
505 <td><p>The full weekday name, e.g. “Saturday”. If the value does not | |
506 contain a valid weekday, an exception of type <code class="docutils literal notranslate"><span class="pre">format_error</span></code> is | |
507 thrown.</p></td> | |
508 </tr> | |
509 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td> | |
510 <td><p>The abbreviated month name, e.g. “Nov”. If the value does not | |
511 contain a valid month, an exception of type <code class="docutils literal notranslate"><span class="pre">format_error</span></code> is | |
512 thrown.</p></td> | |
513 </tr> | |
514 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td> | |
515 <td><p>The full month name, e.g. “November”. If the value does not | |
516 contain a valid month, an exception of type <code class="docutils literal notranslate"><span class="pre">format_error</span></code> is | |
517 thrown.</p></td> | |
518 </tr> | |
519 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td> | |
520 <td><p>The date and time representation, e.g. “Sat Nov 12 22:04:00 1955”. | |
521 The modified command <code class="docutils literal notranslate"><span class="pre">%Ec</span></code> produces the locale’s alternate date | |
522 and time representation.</p></td> | |
523 </tr> | |
524 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'C'</span></code></p></td> | |
525 <td><p>The year divided by 100 using floored division, e.g. “55”. If the | |
526 result is a single decimal digit, it is prefixed with 0. | |
527 The modified command <code class="docutils literal notranslate"><span class="pre">%EC</span></code> produces the locale’s alternative | |
528 representation of the century.</p></td> | |
529 </tr> | |
530 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td> | |
531 <td><p>The day of month as a decimal number. If the result is a single | |
532 decimal digit, it is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%Od</span></code> | |
533 produces the locale’s alternative representation.</p></td> | |
534 </tr> | |
535 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'D'</span></code></p></td> | |
536 <td><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">%m/%d/%y</span></code>, e.g. “11/12/55”.</p></td> | |
537 </tr> | |
538 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'e'</span></code></p></td> | |
539 <td><p>The day of month as a decimal number. If the result is a single | |
540 decimal digit, it is prefixed with a space. The modified command | |
541 <code class="docutils literal notranslate"><span class="pre">%Oe</span></code> produces the locale’s alternative representation.</p></td> | |
542 </tr> | |
543 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'F'</span></code></p></td> | |
544 <td><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">%Y-%m-%d</span></code>, e.g. “1955-11-12”.</p></td> | |
545 </tr> | |
546 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'g'</span></code></p></td> | |
547 <td><p>The last two decimal digits of the ISO week-based year. If the | |
548 result is a single digit it is prefixed by 0.</p></td> | |
549 </tr> | |
550 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'G'</span></code></p></td> | |
551 <td><p>The ISO week-based year as a decimal number. If the result is less | |
552 than four digits it is left-padded with 0 to four digits.</p></td> | |
553 </tr> | |
554 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'h'</span></code></p></td> | |
555 <td><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">%b</span></code>, e.g. “Nov”.</p></td> | |
556 </tr> | |
557 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'H'</span></code></p></td> | |
558 <td><p>The hour (24-hour clock) as a decimal number. If the result is a | |
559 single digit, it is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%OH</span></code> | |
560 produces the locale’s alternative representation.</p></td> | |
561 </tr> | |
562 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'I'</span></code></p></td> | |
563 <td><p>The hour (12-hour clock) as a decimal number. If the result is a | |
564 single digit, it is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%OI</span></code> | |
565 produces the locale’s alternative representation.</p></td> | |
566 </tr> | |
567 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'j'</span></code></p></td> | |
568 <td><p>If the type being formatted is a specialization of duration, the | |
569 decimal number of days without padding. Otherwise, the day of the | |
570 year as a decimal number. Jan 1 is 001. If the result is less than | |
571 three digits, it is left-padded with 0 to three digits.</p></td> | |
572 </tr> | |
573 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'m'</span></code></p></td> | |
574 <td><p>The month as a decimal number. Jan is 01. If the result is a | |
575 single digit, it is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%Om</span></code> | |
576 produces the locale’s alternative representation.</p></td> | |
577 </tr> | |
578 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'M'</span></code></p></td> | |
579 <td><p>The minute as a decimal number. If the result is a single digit, | |
580 it is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%OM</span></code> produces the | |
581 locale’s alternative representation.</p></td> | |
582 </tr> | |
583 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code></p></td> | |
584 <td><p>A new-line character.</p></td> | |
585 </tr> | |
586 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'p'</span></code></p></td> | |
587 <td><p>The AM/PM designations associated with a 12-hour clock.</p></td> | |
588 </tr> | |
589 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'q'</span></code></p></td> | |
590 <td><p>The duration’s unit suffix.</p></td> | |
591 </tr> | |
592 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'Q'</span></code></p></td> | |
593 <td><p>The duration’s numeric value (as if extracted via <code class="docutils literal notranslate"><span class="pre">.count()</span></code>).</p></td> | |
594 </tr> | |
595 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r'</span></code></p></td> | |
596 <td><p>The 12-hour clock time, e.g. “10:04:00 PM”.</p></td> | |
597 </tr> | |
598 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'R'</span></code></p></td> | |
599 <td><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">%H:%M</span></code>, e.g. “22:04”.</p></td> | |
600 </tr> | |
601 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'S'</span></code></p></td> | |
602 <td><p>Seconds as a decimal number. If the number of seconds is less than | |
603 10, the result is prefixed with 0. If the precision of the input | |
604 cannot be exactly represented with seconds, then the format is a | |
605 decimal floating-point number with a fixed format and a precision | |
606 matching that of the precision of the input (or to a microseconds | |
607 precision if the conversion to floating-point decimal seconds | |
608 cannot be made within 18 fractional digits). The character for the | |
609 decimal point is localized according to the locale. The modified | |
610 command <code class="docutils literal notranslate"><span class="pre">%OS</span></code> produces the locale’s alternative representation.</p></td> | |
611 </tr> | |
612 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'t'</span></code></p></td> | |
613 <td><p>A horizontal-tab character.</p></td> | |
614 </tr> | |
615 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'T'</span></code></p></td> | |
616 <td><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">%H:%M:%S</span></code>.</p></td> | |
617 </tr> | |
618 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code></p></td> | |
619 <td><p>The ISO weekday as a decimal number (1-7), where Monday is 1. The | |
620 modified command <code class="docutils literal notranslate"><span class="pre">%Ou</span></code> produces the locale’s alternative | |
621 representation.</p></td> | |
622 </tr> | |
623 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'U'</span></code></p></td> | |
624 <td><p>The week number of the year as a decimal number. The first Sunday | |
625 of the year is the first day of week 01. Days of the same year | |
626 prior to that are in week 00. If the result is a single digit, it | |
627 is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%OU</span></code> produces the | |
628 locale’s alternative representation.</p></td> | |
629 </tr> | |
630 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'V'</span></code></p></td> | |
631 <td><p>The ISO week-based week number as a decimal number. If the result | |
632 is a single digit, it is prefixed with 0. The modified command | |
633 <code class="docutils literal notranslate"><span class="pre">%OV</span></code> produces the locale’s alternative representation.</p></td> | |
634 </tr> | |
635 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'w'</span></code></p></td> | |
636 <td><p>The weekday as a decimal number (0-6), where Sunday is 0. | |
637 The modified command <code class="docutils literal notranslate"><span class="pre">%Ow</span></code> produces the locale’s alternative | |
638 representation.</p></td> | |
639 </tr> | |
640 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'W'</span></code></p></td> | |
641 <td><p>The week number of the year as a decimal number. The first Monday | |
642 of the year is the first day of week 01. Days of the same year | |
643 prior to that are in week 00. If the result is a single digit, it | |
644 is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%OW</span></code> produces the | |
645 locale’s alternative representation.</p></td> | |
646 </tr> | |
647 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'x'</span></code></p></td> | |
648 <td><p>The date representation, e.g. “11/12/55”. The modified command | |
649 <code class="docutils literal notranslate"><span class="pre">%Ex</span></code> produces the locale’s alternate date representation.</p></td> | |
650 </tr> | |
651 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'X'</span></code></p></td> | |
652 <td><p>The time representation, e.g. “10:04:00”. The modified command | |
653 <code class="docutils literal notranslate"><span class="pre">%EX</span></code> produces the locale’s alternate time representation.</p></td> | |
654 </tr> | |
655 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'y'</span></code></p></td> | |
656 <td><p>The last two decimal digits of the year. If the result is a single | |
657 digit it is prefixed by 0. The modified command <code class="docutils literal notranslate"><span class="pre">%Oy</span></code> produces | |
658 the locale’s alternative representation. The modified command | |
659 <code class="docutils literal notranslate"><span class="pre">%Ey</span></code> produces the locale’s alternative representation of offset | |
660 from <code class="docutils literal notranslate"><span class="pre">%EC</span></code> (year only).</p></td> | |
661 </tr> | |
662 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'Y'</span></code></p></td> | |
663 <td><p>The year as a decimal number. If the result is less than four | |
664 digits it is left-padded with 0 to four digits. The modified | |
665 command <code class="docutils literal notranslate"><span class="pre">%EY</span></code> produces the locale’s alternative full year | |
666 representation.</p></td> | |
667 </tr> | |
668 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'z'</span></code></p></td> | |
669 <td><p>The offset from UTC in the ISO 8601:2004 format. For example -0430 | |
670 refers to 4 hours 30 minutes behind UTC. If the offset is zero, | |
671 +0000 is used. The modified commands <code class="docutils literal notranslate"><span class="pre">%Ez</span></code> and <code class="docutils literal notranslate"><span class="pre">%Oz</span></code> insert a | |
672 <code class="docutils literal notranslate"><span class="pre">:</span></code> between the hours and minutes: -04:30. If the offset | |
673 information is not available, an exception of type | |
674 <code class="docutils literal notranslate"><span class="pre">format_error</span></code> is thrown.</p></td> | |
675 </tr> | |
676 <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'Z'</span></code></p></td> | |
677 <td><p>The time zone abbreviation. If the time zone abbreviation is not | |
678 available, an exception of type <code class="docutils literal notranslate"><span class="pre">format_error</span></code> is thrown.</p></td> | |
679 </tr> | |
680 <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'%'</span></code></p></td> | |
681 <td><p>A % character.</p></td> | |
682 </tr> | |
683 </tbody> | |
684 </table> | |
685 <p>Specifiers that have a calendaric component such as <code class="docutils literal notranslate"><span class="pre">'d'</span></code> (the day of month) | |
686 are valid only for <code class="docutils literal notranslate"><span class="pre">std::tm</span></code> and time points but not durations.</p> | |
687 </section> | |
688 <section id="range-format-specifications"> | |
689 <h2>Range Format Specifications<a class="headerlink" href="#range-format-specifications" title="Permalink to this headline">¶</a></h2> | |
690 <p>Format specifications for range types have the following syntax:</p> | |
691 <pre> | |
692 <strong id="grammar-token-sf-range_format_spec"><span id="grammar-token-range-format-spec"></span>range_format_spec</strong> ::= [":" [<code class="xref docutils literal notranslate"><span class="pre">underlying_spec</span></code>]] | |
693 </pre> | |
694 <p>The <code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">underlying_spec</span></code> is parsed based on the formatter of the range’s | |
695 reference type.</p> | |
696 <p>By default, a range of characters or strings is printed escaped and quoted. But | |
697 if any <code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">underlying_spec</span></code> is provided (even if it is empty), then the characters | |
698 or strings are printed according to the provided specification.</p> | |
699 <p>Examples:</p> | |
700 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="p">{</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">20</span><span class="p">,</span><span class="w"> </span><span class="mi">30</span><span class="p">});</span> | |
701 <span class="c1">// Result: [10, 20, 30]</span> | |
702 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{::#x}"</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="p">{</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">20</span><span class="p">,</span><span class="w"> </span><span class="mi">30</span><span class="p">});</span> | |
703 <span class="c1">// Result: [0xa, 0x14, 0x1e]</span> | |
704 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="p">{</span><span class="sc">'h'</span><span class="p">,</span><span class="w"> </span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="sc">'l'</span><span class="p">,</span><span class="w"> </span><span class="sc">'l'</span><span class="p">,</span><span class="w"> </span><span class="sc">'o'</span><span class="p">});</span> | |
705 <span class="c1">// Result: ['h', 'e', 'l', 'l', 'o']</span> | |
706 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{::}"</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="p">{</span><span class="sc">'h'</span><span class="p">,</span><span class="w"> </span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="sc">'l'</span><span class="p">,</span><span class="w"> </span><span class="sc">'l'</span><span class="p">,</span><span class="w"> </span><span class="sc">'o'</span><span class="p">});</span> | |
707 <span class="c1">// Result: [h, e, l, l, o]</span> | |
708 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{::d}"</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="p">{</span><span class="sc">'h'</span><span class="p">,</span><span class="w"> </span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="sc">'l'</span><span class="p">,</span><span class="w"> </span><span class="sc">'l'</span><span class="p">,</span><span class="w"> </span><span class="sc">'o'</span><span class="p">});</span> | |
709 <span class="c1">// Result: [104, 101, 108, 108, 111]</span> | |
710 </pre></div> | |
711 </div> | |
712 </section> | |
713 <section id="format-examples"> | |
714 <span id="formatexamples"></span><h2>Format Examples<a class="headerlink" href="#format-examples" title="Permalink to this headline">¶</a></h2> | |
715 <p>This section contains examples of the format syntax and comparison with | |
716 the printf formatting.</p> | |
717 <p>In most of the cases the syntax is similar to the printf formatting, with the | |
718 addition of the <code class="docutils literal notranslate"><span class="pre">{}</span></code> and with <code class="docutils literal notranslate"><span class="pre">:</span></code> used instead of <code class="docutils literal notranslate"><span class="pre">%</span></code>. | |
719 For example, <code class="docutils literal notranslate"><span class="pre">"%03.2f"</span></code> can be translated to <code class="docutils literal notranslate"><span class="pre">"{:03.2f}"</span></code>.</p> | |
720 <p>The new format syntax also supports new and different options, shown in the | |
721 following examples.</p> | |
722 <p>Accessing arguments by position:</p> | |
723 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{0}, {1}, {2}"</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'b'</span><span class="p">,</span><span class="w"> </span><span class="sc">'c'</span><span class="p">);</span> | |
724 <span class="c1">// Result: "a, b, c"</span> | |
725 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{}, {}, {}"</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'b'</span><span class="p">,</span><span class="w"> </span><span class="sc">'c'</span><span class="p">);</span> | |
726 <span class="c1">// Result: "a, b, c"</span> | |
727 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{2}, {1}, {0}"</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'b'</span><span class="p">,</span><span class="w"> </span><span class="sc">'c'</span><span class="p">);</span> | |
728 <span class="c1">// Result: "c, b, a"</span> | |
729 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{0}{1}{0}"</span><span class="p">,</span><span class="w"> </span><span class="s">"abra"</span><span class="p">,</span><span class="w"> </span><span class="s">"cad"</span><span class="p">);</span><span class="w"> </span><span class="c1">// arguments' indices can be repeated</span> | |
730 <span class="c1">// Result: "abracadabra"</span> | |
731 </pre></div> | |
732 </div> | |
733 <p>Aligning the text and specifying a width:</p> | |
734 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:<30}"</span><span class="p">,</span><span class="w"> </span><span class="s">"left aligned"</span><span class="p">);</span> | |
735 <span class="c1">// Result: "left aligned "</span> | |
736 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:>30}"</span><span class="p">,</span><span class="w"> </span><span class="s">"right aligned"</span><span class="p">);</span> | |
737 <span class="c1">// Result: " right aligned"</span> | |
738 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:^30}"</span><span class="p">,</span><span class="w"> </span><span class="s">"centered"</span><span class="p">);</span> | |
739 <span class="c1">// Result: " centered "</span> | |
740 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:*^30}"</span><span class="p">,</span><span class="w"> </span><span class="s">"centered"</span><span class="p">);</span><span class="w"> </span><span class="c1">// use '*' as a fill char</span> | |
741 <span class="c1">// Result: "***********centered***********"</span> | |
742 </pre></div> | |
743 </div> | |
744 <p>Dynamic width:</p> | |
745 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:<{}}"</span><span class="p">,</span><span class="w"> </span><span class="s">"left aligned"</span><span class="p">,</span><span class="w"> </span><span class="mi">30</span><span class="p">);</span> | |
746 <span class="c1">// Result: "left aligned "</span> | |
747 </pre></div> | |
748 </div> | |
749 <p>Dynamic precision:</p> | |
750 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:.{}f}"</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span> | |
751 <span class="c1">// Result: "3.1"</span> | |
752 </pre></div> | |
753 </div> | |
754 <p>Replacing <code class="docutils literal notranslate"><span class="pre">%+f</span></code>, <code class="docutils literal notranslate"><span class="pre">%-f</span></code>, and <code class="docutils literal notranslate"><span class="pre">%</span> <span class="pre">f</span></code> and specifying a sign:</p> | |
755 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:+f}; {:+f}"</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mf">-3.14</span><span class="p">);</span><span class="w"> </span><span class="c1">// show it always</span> | |
756 <span class="c1">// Result: "+3.140000; -3.140000"</span> | |
757 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{: f}; {: f}"</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mf">-3.14</span><span class="p">);</span><span class="w"> </span><span class="c1">// show a space for positive numbers</span> | |
758 <span class="c1">// Result: " 3.140000; -3.140000"</span> | |
759 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:-f}; {:-f}"</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mf">-3.14</span><span class="p">);</span><span class="w"> </span><span class="c1">// show only the minus -- same as '{:f}; {:f}'</span> | |
760 <span class="c1">// Result: "3.140000; -3.140000"</span> | |
761 </pre></div> | |
762 </div> | |
763 <p>Replacing <code class="docutils literal notranslate"><span class="pre">%x</span></code> and <code class="docutils literal notranslate"><span class="pre">%o</span></code> and converting the value to different bases:</p> | |
764 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}"</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">);</span> | |
765 <span class="c1">// Result: "int: 42; hex: 2a; oct: 52; bin: 101010"</span> | |
766 <span class="c1">// with 0x or 0 or 0b as prefix:</span> | |
767 <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}"</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">);</span> | |
768 <span class="c1">// Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010"</span> | |
769 </pre></div> | |
770 </div> | |
771 <p>Padded hex byte with prefix and always prints both hex characters:</p> | |
772 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"{:#04x}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span> | |
773 <span class="c1">// Result: "0x00"</span> | |
774 </pre></div> | |
775 </div> | |
776 <p>Box drawing using Unicode fill:</p> | |
777 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span> | |
778 <span class="w"> </span><span class="s">"┌{0:─^{2}}┐</span><span class="se">\n</span><span class="s">"</span> | |
779 <span class="w"> </span><span class="s">"│{1: ^{2}}│</span><span class="se">\n</span><span class="s">"</span> | |
780 <span class="w"> </span><span class="s">"└{0:─^{2}}┘</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="s">""</span><span class="p">,</span><span class="w"> </span><span class="s">"Hello, world!"</span><span class="p">,</span><span class="w"> </span><span class="mi">20</span><span class="p">);</span> | |
781 </pre></div> | |
782 </div> | |
783 <p>prints:</p> | |
784 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span>┌────────────────────┐ | |
785 │ Hello, world! │ | |
786 └────────────────────┘ | |
787 </pre></div> | |
788 </div> | |
789 <p>Using type-specific formatting:</p> | |
790 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf"><fmt/chrono.h></span> | |
791 | |
792 <span class="k">auto</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tm</span><span class="p">();</span> | |
793 <span class="n">t</span><span class="p">.</span><span class="n">tm_year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2010</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1900</span><span class="p">;</span> | |
794 <span class="n">t</span><span class="p">.</span><span class="n">tm_mon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span> | |
795 <span class="n">t</span><span class="p">.</span><span class="n">tm_mday</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span> | |
796 <span class="n">t</span><span class="p">.</span><span class="n">tm_hour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">12</span><span class="p">;</span> | |
797 <span class="n">t</span><span class="p">.</span><span class="n">tm_min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">15</span><span class="p">;</span> | |
798 <span class="n">t</span><span class="p">.</span><span class="n">tm_sec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">58</span><span class="p">;</span> | |
799 <span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">"{:%Y-%m-%d %H:%M:%S}"</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">);</span> | |
800 <span class="c1">// Prints: 2010-08-04 12:15:58</span> | |
801 </pre></div> | |
802 </div> | |
803 <p>Using the comma as a thousands separator:</p> | |
804 <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf"><fmt/format.h></span> | |
805 | |
806 <span class="k">auto</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">locale</span><span class="p">(</span><span class="s">"en_US.UTF-8"</span><span class="p">),</span><span class="w"> </span><span class="s">"{:L}"</span><span class="p">,</span><span class="w"> </span><span class="mi">1234567890</span><span class="p">);</span> | |
807 <span class="c1">// s == "1,234,567,890"</span> | |
808 </pre></div> | |
809 </div> | |
810 </section> | |
811 </section> | |
812 | |
813 | |
814 </div> | |
815 </div> | |
816 </div> | |
817 | |
818 | |
819 | |
820 <div class="footer" role="contentinfo"> | |
821 © Copyright 2012-present, Victor Zverovich. | |
822 Created using <a href="http://sphinx-doc.org/">Sphinx</a> 3.3.0. | |
823 </div> | |
824 | |
825 <script src="_static/bootstrap.min.js"></script> | |
826 | |
827 </body> | |
828 </html> |