mirror of
https://github.com/fmtlib/fmt.git
synced 2025-04-01 10:20:59 +00:00
Add support for time points with arbitrary durations (#2208)
This commit is contained in:
parent
dac42f52b2
commit
06b3a1000c
@ -401,9 +401,9 @@ inline size_t strftime(wchar_t* str, size_t count, const wchar_t* format,
|
|||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char, typename Duration>
|
||||||
struct formatter<std::chrono::time_point<std::chrono::system_clock>, Char>
|
struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
|
||||||
: formatter<std::tm, Char> {
|
Char> : formatter<std::tm, Char> {
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(std::chrono::time_point<std::chrono::system_clock> val,
|
auto format(std::chrono::time_point<std::chrono::system_clock> val,
|
||||||
FormatContext& ctx) -> decltype(ctx.out()) {
|
FormatContext& ctx) -> decltype(ctx.out()) {
|
||||||
|
@ -100,24 +100,30 @@ TEST(TimeTest, FormatTM) {
|
|||||||
std::time_t t = std::chrono::system_clock::to_time_t(point);
|
std::time_t t = std::chrono::system_clock::to_time_t(point);
|
||||||
std::tm tm = *std::localtime(&t);
|
std::tm tm = *std::localtime(&t);
|
||||||
char strftime_output[256];
|
char strftime_output[256];
|
||||||
std::strftime(strftime_output, sizeof(strftime_output),
|
std::strftime(strftime_output, sizeof(strftime_output), "%Y-%m-%d %H:%M:%S",
|
||||||
"%Y-%m-%d %H:%M:%S", &tm);
|
&tm);
|
||||||
EXPECT_EQ(strftime_output, fmt::format("{:%Y-%m-%d %H:%M:%S}", tm));
|
EXPECT_EQ(strftime_output, fmt::format("{:%Y-%m-%d %H:%M:%S}", tm));
|
||||||
auto wstrftime_output = std::wstring();
|
auto wstrftime_output = std::wstring();
|
||||||
std::copy(strftime_output, strftime_output + strlen(strftime_output),
|
std::copy(strftime_output, strftime_output + strlen(strftime_output),
|
||||||
std::back_inserter(wstrftime_output));
|
std::back_inserter(wstrftime_output));
|
||||||
EXPECT_EQ(wstrftime_output,
|
EXPECT_EQ(wstrftime_output, fmt::format(L"{:%Y-%m-%d %H:%M:%S}", tm));
|
||||||
fmt::format(L"{:%Y-%m-%d %H:%M:%S}", tm));
|
}
|
||||||
|
|
||||||
|
template <typename TimePoint> std::string strftime(TimePoint tp) {
|
||||||
|
std::time_t t = std::chrono::system_clock::to_time_t(tp);
|
||||||
|
std::tm tm = *std::localtime(&t);
|
||||||
|
char output[256];
|
||||||
|
std::strftime(output, sizeof(output), "%Y-%m-%d %H:%M:%S", &tm);
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(TimeTest, TimePoint) {
|
TEST(TimeTest, TimePoint) {
|
||||||
auto point = std::chrono::system_clock::now();
|
auto t1 = std::chrono::system_clock::now();
|
||||||
std::time_t t = std::chrono::system_clock::to_time_t(point);
|
EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
|
||||||
std::tm tm = *std::localtime(&t);
|
using time_point =
|
||||||
char strftime_output[256];
|
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
|
||||||
std::strftime(strftime_output, sizeof(strftime_output),
|
auto t2 = time_point(std::chrono::seconds(42));
|
||||||
"%Y-%m-%d %H:%M:%S", &tm);
|
EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
|
||||||
EXPECT_EQ(strftime_output, fmt::format("{:%Y-%m-%d %H:%M:%S}", point));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define EXPECT_TIME(spec, time, duration) \
|
#define EXPECT_TIME(spec, time, duration) \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user