mirror of
https://github.com/fmtlib/fmt.git
synced 2025-02-05 00:40:12 +00:00
Generalization of strftime/wcsftime function calls in tests
This commit is contained in:
parent
2eeddba756
commit
f88c020fc0
@ -42,6 +42,14 @@ auto make_second(int s) -> std::tm {
|
||||
return time;
|
||||
}
|
||||
|
||||
std::string system_strftime(const std::string& format, const std::tm* timeptr,
|
||||
size_t maxsize = 1024) {
|
||||
std::vector<char> output(maxsize);
|
||||
auto size =
|
||||
std::strftime(output.data(), output.size(), format.c_str(), timeptr);
|
||||
return std::string(output.data(), size);
|
||||
}
|
||||
|
||||
TEST(chrono_test, format_tm) {
|
||||
auto tm = std::tm();
|
||||
tm.tm_year = 116;
|
||||
@ -102,10 +110,9 @@ TEST(chrono_test, format_tm) {
|
||||
std::time_t t = std::mktime(&tm);
|
||||
tm = *std::localtime(&t);
|
||||
|
||||
char output[256] = {};
|
||||
std::strftime(output, sizeof(output), iso_week_spec.c_str(), &tm);
|
||||
auto fmt_spec = std::string("{:").append(iso_week_spec).append("}");
|
||||
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm));
|
||||
EXPECT_EQ(system_strftime(iso_week_spec, &tm),
|
||||
fmt::format(fmt::runtime(fmt_spec), tm));
|
||||
}
|
||||
|
||||
// Every day from 1970-01-01
|
||||
@ -113,10 +120,9 @@ TEST(chrono_test, format_tm) {
|
||||
for (std::time_t t = 6 * 3600; t < time_now; t += 86400) {
|
||||
tm = *std::localtime(&t);
|
||||
|
||||
char output[256] = {};
|
||||
std::strftime(output, sizeof(output), iso_week_spec.c_str(), &tm);
|
||||
auto fmt_spec = std::string("{:").append(iso_week_spec).append("}");
|
||||
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm));
|
||||
EXPECT_EQ(system_strftime(iso_week_spec, &tm),
|
||||
fmt::format(fmt::runtime(fmt_spec), tm));
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,22 +214,20 @@ TEST(chrono_test, gmtime) {
|
||||
EXPECT_TRUE(equal(tm, fmt::gmtime(t)));
|
||||
}
|
||||
|
||||
template <typename TimePoint> auto strftime(TimePoint tp) -> std::string {
|
||||
template <typename TimePoint> auto strftime_full(TimePoint tp) -> std::string {
|
||||
auto t = std::chrono::system_clock::to_time_t(tp);
|
||||
auto tm = *std::localtime(&t);
|
||||
char output[256] = {};
|
||||
std::strftime(output, sizeof(output), "%Y-%m-%d %H:%M:%S", &tm);
|
||||
return output;
|
||||
return system_strftime("%Y-%m-%d %H:%M:%S", &tm);
|
||||
}
|
||||
|
||||
TEST(chrono_test, time_point) {
|
||||
auto t1 = std::chrono::system_clock::now();
|
||||
EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
|
||||
EXPECT_EQ(strftime(t1), fmt::format("{}", t1));
|
||||
EXPECT_EQ(strftime_full(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
|
||||
EXPECT_EQ(strftime_full(t1), fmt::format("{}", t1));
|
||||
using time_point =
|
||||
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
|
||||
auto t2 = time_point(std::chrono::seconds(42));
|
||||
EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
|
||||
EXPECT_EQ(strftime_full(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
|
||||
|
||||
std::vector<std::string> spec_list = {
|
||||
"%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", "%EC",
|
||||
@ -236,12 +240,12 @@ TEST(chrono_test, time_point) {
|
||||
for (const auto& spec : spec_list) {
|
||||
auto t = std::chrono::system_clock::to_time_t(t1);
|
||||
auto tm = *std::localtime(&t);
|
||||
char output[256] = {};
|
||||
std::strftime(output, sizeof(output), spec.c_str(), &tm);
|
||||
|
||||
auto sys_output = system_strftime(spec, &tm);
|
||||
|
||||
auto fmt_spec = std::string("{:").append(spec).append("}");
|
||||
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), t1));
|
||||
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm));
|
||||
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
|
||||
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -267,6 +267,14 @@ TEST(xchar_test, chrono) {
|
||||
EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33");
|
||||
}
|
||||
|
||||
std::wstring system_wcsftime(const std::wstring& format, const std::tm* timeptr,
|
||||
size_t maxsize = 1024) {
|
||||
std::vector<wchar_t> output(maxsize);
|
||||
auto size =
|
||||
std::wcsftime(output.data(), output.size(), format.c_str(), timeptr);
|
||||
return std::wstring(output.data(), size);
|
||||
}
|
||||
|
||||
TEST(chrono_test, time_point) {
|
||||
auto t1 = std::chrono::system_clock::now();
|
||||
|
||||
@ -282,12 +290,12 @@ TEST(chrono_test, time_point) {
|
||||
for (const auto& spec : spec_list) {
|
||||
auto t = std::chrono::system_clock::to_time_t(t1);
|
||||
auto tm = *std::localtime(&t);
|
||||
wchar_t output[1024] = {};
|
||||
std::wcsftime(output, sizeof(output) / sizeof(wchar_t), spec.c_str(), &tm);
|
||||
|
||||
auto sys_output = system_wcsftime(spec, &tm);
|
||||
|
||||
auto fmt_spec = std::wstring(L"{:").append(spec).append(L"}");
|
||||
EXPECT_EQ(output, fmt::format(fmt_spec, t1));
|
||||
EXPECT_EQ(output, fmt::format(fmt_spec, tm));
|
||||
EXPECT_EQ(sys_output, fmt::format(fmt_spec, t1));
|
||||
EXPECT_EQ(sys_output, fmt::format(fmt_spec, tm));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user