2 Formatting library for C++ - time formatting
4 Copyright (c) 2012 - 2016, Victor Zverovich
7 For the license information refer to format.h.
13 #include "fmt/format.h"
17 template <typename ArgFormatter
>
18 void format_arg(BasicFormatter
<char, ArgFormatter
> &f
,
19 const char *&format_str
, const std::tm
&tm
) {
20 if (*format_str
== ':')
22 const char *end
= format_str
;
23 while (*end
&& *end
!= '}')
26 FMT_THROW(FormatError("missing '}' in format string"));
27 internal::MemoryBuffer
<char, internal::INLINE_BUFFER_SIZE
> format
;
28 format
.append(format_str
, end
+ 1);
29 format
[format
.size() - 1] = '\0';
30 Buffer
<char> &buffer
= f
.writer().buffer();
31 std::size_t start
= buffer
.size();
33 std::size_t size
= buffer
.capacity() - start
;
34 std::size_t count
= std::strftime(&buffer
[start
], size
, &format
[0], &tm
);
36 buffer
.resize(start
+ count
);
39 if (size
>= format
.size() * 256) {
40 // If the buffer is 256 times larger than the format string, assume
41 // that `strftime` gives an empty result. There doesn't seem to be a
42 // better way to distinguish the two cases:
43 // https://github.com/fmtlib/fmt/issues/367
46 const std::size_t MIN_GROWTH
= 10;
47 buffer
.reserve(buffer
.capacity() + (size
> MIN_GROWTH
? size
: MIN_GROWTH
));