Version 0.8.0
[marnav.git] / extern / fmt / time.h
blobccdad77fbf86e8c3edbe56e203796caa45818526
1 /*
2 Formatting library for C++ - time formatting
4 Copyright (c) 2012 - 2016, Victor Zverovich
5 All rights reserved.
7 For the license information refer to format.h.
8 */
10 #ifndef FMT_TIME_H_
11 #define FMT_TIME_H_
13 #include "fmt/format.h"
14 #include <ctime>
16 namespace fmt {
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 == ':')
21 ++format_str;
22 const char *end = format_str;
23 while (*end && *end != '}')
24 ++end;
25 if (*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();
32 for (;;) {
33 std::size_t size = buffer.capacity() - start;
34 std::size_t count = std::strftime(&buffer[start], size, &format[0], &tm);
35 if (count != 0) {
36 buffer.resize(start + count);
37 break;
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
44 break;
46 const std::size_t MIN_GROWTH = 10;
47 buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
49 format_str = end + 1;
53 #endif // FMT_TIME_H_