summaryrefslogtreecommitdiff
path: root/lib/spdlog/fmt/bundled/time.h
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2018-07-11 07:33:08 +0200
committerJörg Frings-Fürst <debian@jff-webhosting.net>2018-07-11 07:33:08 +0200
commit621f4acf1406e51eaf5d9429c46625e6f5fde98e (patch)
tree0fe1fd92a0502602f54b2bf12a8c247ef575d00f /lib/spdlog/fmt/bundled/time.h
parent25d4e8d5ee396a852568b7ae1c15971a9da409db (diff)
parent5aa4b715375b173e9c24363f977883786b05cd6d (diff)
Merge branch 'release/debian/2.0.0-1'debian/2.0.0-1
Diffstat (limited to 'lib/spdlog/fmt/bundled/time.h')
-rw-r--r--lib/spdlog/fmt/bundled/time.h188
1 files changed, 188 insertions, 0 deletions
diff --git a/lib/spdlog/fmt/bundled/time.h b/lib/spdlog/fmt/bundled/time.h
new file mode 100644
index 0000000..1d4210c
--- /dev/null
+++ b/lib/spdlog/fmt/bundled/time.h
@@ -0,0 +1,188 @@
+/*
+ Formatting library for C++ - time formatting
+
+ Copyright (c) 2012 - 2016, Victor Zverovich
+ All rights reserved.
+
+ For the license information refer to format.h.
+ */
+
+#ifndef FMT_TIME_H_
+#define FMT_TIME_H_
+
+#include "format.h"
+#include <ctime>
+
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable : 4702) // unreachable code
+#pragma warning(disable : 4996) // "deprecated" functions
+#endif
+
+namespace fmt {
+template<typename ArgFormatter>
+void format_arg(BasicFormatter<char, ArgFormatter> &f, const char *&format_str, const std::tm &tm)
+{
+ if (*format_str == ':')
+ ++format_str;
+ const char *end = format_str;
+ while (*end && *end != '}')
+ ++end;
+ if (*end != '}')
+ FMT_THROW(FormatError("missing '}' in format string"));
+ internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format;
+ format.append(format_str, end + 1);
+ format[format.size() - 1] = '\0';
+ Buffer<char> &buffer = f.writer().buffer();
+ 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;
+ }
+ const std::size_t MIN_GROWTH = 10;
+ buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
+ }
+ format_str = end + 1;
+}
+
+namespace internal {
+inline Null<> localtime_r(...)
+{
+ return Null<>();
+}
+inline Null<> localtime_s(...)
+{
+ return Null<>();
+}
+inline Null<> gmtime_r(...)
+{
+ return Null<>();
+}
+inline Null<> gmtime_s(...)
+{
+ return Null<>();
+}
+} // namespace internal
+
+// Thread-safe replacement for std::localtime
+inline std::tm localtime(std::time_t time)
+{
+ struct LocalTime
+ {
+ std::time_t time_;
+ std::tm tm_;
+
+ LocalTime(std::time_t t)
+ : time_(t)
+ {
+ }
+
+ bool run()
+ {
+ using namespace fmt::internal;
+ return handle(localtime_r(&time_, &tm_));
+ }
+
+ bool handle(std::tm *tm)
+ {
+ return tm != FMT_NULL;
+ }
+
+ bool handle(internal::Null<>)
+ {
+ using namespace fmt::internal;
+ return fallback(localtime_s(&tm_, &time_));
+ }
+
+ bool fallback(int res)
+ {
+ return res == 0;
+ }
+
+ bool fallback(internal::Null<>)
+ {
+ using namespace fmt::internal;
+ std::tm *tm = std::localtime(&time_);
+ if (tm)
+ tm_ = *tm;
+ return tm != FMT_NULL;
+ }
+ };
+ LocalTime lt(time);
+ if (lt.run())
+ return lt.tm_;
+ // Too big time values may be unsupported.
+ FMT_THROW(fmt::FormatError("time_t value out of range"));
+ return std::tm();
+}
+
+// Thread-safe replacement for std::gmtime
+inline std::tm gmtime(std::time_t time)
+{
+ struct GMTime
+ {
+ std::time_t time_;
+ std::tm tm_;
+
+ GMTime(std::time_t t)
+ : time_(t)
+ {
+ }
+
+ bool run()
+ {
+ using namespace fmt::internal;
+ return handle(gmtime_r(&time_, &tm_));
+ }
+
+ bool handle(std::tm *tm)
+ {
+ return tm != FMT_NULL;
+ }
+
+ bool handle(internal::Null<>)
+ {
+ using namespace fmt::internal;
+ return fallback(gmtime_s(&tm_, &time_));
+ }
+
+ bool fallback(int res)
+ {
+ return res == 0;
+ }
+
+ bool fallback(internal::Null<>)
+ {
+ std::tm *tm = std::gmtime(&time_);
+ if (tm != FMT_NULL)
+ tm_ = *tm;
+ return tm != FMT_NULL;
+ }
+ };
+ GMTime gt(time);
+ if (gt.run())
+ return gt.tm_;
+ // Too big time values may be unsupported.
+ FMT_THROW(fmt::FormatError("time_t value out of range"));
+ return std::tm();
+}
+} // namespace fmt
+
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
+#endif // FMT_TIME_H_