2018-01-06 17:09:50 +00:00
|
|
|
// Formatting library for C++ - time formatting
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - 2016, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
2016-04-25 15:07:27 +00:00
|
|
|
|
|
|
|
#ifndef FMT_TIME_H_
|
|
|
|
#define FMT_TIME_H_
|
|
|
|
|
2018-01-20 18:28:10 +00:00
|
|
|
#include "format.h"
|
2016-04-25 15:07:27 +00:00
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
namespace fmt {
|
2016-10-25 12:55:40 +00:00
|
|
|
|
2016-10-09 21:57:52 +00:00
|
|
|
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<>(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 != 0; }
|
|
|
|
|
|
|
|
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 != 0) tm_ = *tm;
|
|
|
|
return tm != 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
LocalTime lt(time);
|
|
|
|
if (lt.run())
|
|
|
|
return lt.tm_;
|
|
|
|
// Too big time values may be unsupported.
|
|
|
|
FMT_THROW(format_error("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 != 0; }
|
|
|
|
|
|
|
|
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 != 0) tm_ = *tm;
|
|
|
|
return tm != 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
GMTime gt(time);
|
|
|
|
if (gt.run())
|
|
|
|
return gt.tm_;
|
|
|
|
// Too big time values may be unsupported.
|
|
|
|
FMT_THROW(format_error("time_t value out of range"));
|
|
|
|
return std::tm();
|
|
|
|
}
|
|
|
|
|
2017-08-13 20:09:02 +00:00
|
|
|
template <>
|
|
|
|
struct formatter<std::tm> {
|
2017-09-16 23:50:40 +00:00
|
|
|
template <typename ParseContext>
|
|
|
|
auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
|
|
|
|
auto it = internal::null_terminating_iterator<char>(ctx);
|
2017-08-13 20:09:02 +00:00
|
|
|
if (*it == ':')
|
|
|
|
++it;
|
|
|
|
auto end = it;
|
|
|
|
while (*end && *end != '}')
|
|
|
|
++end;
|
|
|
|
tm_format.reserve(end - it + 1);
|
|
|
|
using internal::pointer_from;
|
|
|
|
tm_format.append(pointer_from(it), pointer_from(end));
|
|
|
|
tm_format.push_back('\0');
|
|
|
|
return pointer_from(end);
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:00:27 +00:00
|
|
|
auto format(const std::tm &tm, context &ctx) -> decltype(ctx.begin()) {
|
2018-01-15 16:22:31 +00:00
|
|
|
internal::buffer &buf = internal::get_container(ctx.begin());
|
2017-08-13 20:09:02 +00:00
|
|
|
std::size_t start = buf.size();
|
|
|
|
for (;;) {
|
|
|
|
std::size_t size = buf.capacity() - start;
|
|
|
|
std::size_t count = std::strftime(&buf[start], size, &tm_format[0], &tm);
|
|
|
|
if (count != 0) {
|
|
|
|
buf.resize(start + count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (size >= tm_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;
|
|
|
|
buf.reserve(buf.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
|
2016-08-05 14:27:56 +00:00
|
|
|
}
|
2018-01-14 19:00:27 +00:00
|
|
|
return ctx.begin();
|
2016-04-25 15:24:14 +00:00
|
|
|
}
|
2017-08-13 20:09:02 +00:00
|
|
|
|
|
|
|
memory_buffer tm_format;
|
|
|
|
};
|
2016-04-25 15:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // FMT_TIME_H_
|