fmtlegacy/fmt/time.h

54 lines
1.4 KiB
C
Raw Normal View History

2016-04-25 15:07:27 +00:00
/*
Formatting library for C++ - time formatting
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
2016-06-22 13:33:56 +00:00
For the license information refer to format.h.
2016-04-25 15:07:27 +00:00
*/
#ifndef FMT_TIME_H_
#define FMT_TIME_H_
#include "fmt/format.h"
#include <ctime>
namespace fmt {
2016-10-25 12:55:40 +00:00
2016-11-07 00:11:24 +00:00
void format_value(Writer &w, const std::tm &tm, format_context &ctx) {
const char *&s = ctx.ptr();
if (*s == ':')
++s;
const char *end = s;
2016-04-25 15:07:27 +00:00
while (*end && *end != '}')
++end;
if (*end != '}')
2016-08-25 15:38:07 +00:00
FMT_THROW(format_error("missing '}' in format string"));
2016-04-25 15:07:27 +00:00
internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format;
2016-11-07 00:11:24 +00:00
format.append(s, end + 1);
2016-04-25 15:07:27 +00:00
format[format.size() - 1] = '\0';
2016-10-25 12:55:40 +00:00
Buffer<char> &buffer = w.buffer();
2016-04-25 15:24:14 +00:00
std::size_t start = buffer.size();
for (;;) {
std::size_t size = buffer.capacity() - start;
std::size_t count = std::strftime(&buffer[start], size, &format[0], &tm);
if (count != 0) {
buffer.resize(start + count);
break;
}
if (size >= format.size() * 256) {
// If the buffer is 256 times larger than the format string, assume
// that `strftime` gives an empty result. There doesn't seem to be a
// better way to distinguish the two cases:
// https://github.com/fmtlib/fmt/issues/367
break;
}
2016-05-07 14:03:21 +00:00
const std::size_t MIN_GROWTH = 10;
buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
2016-04-25 15:24:14 +00:00
}
2016-11-07 00:11:24 +00:00
s = end;
2016-04-25 15:07:27 +00:00
}
}
#endif // FMT_TIME_H_