Skip to content

Commit

Permalink
Generalization of strftime/wcsftime function calls in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Oct 14, 2021
1 parent dbc5c20 commit 4e1d3fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
38 changes: 21 additions & 17 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,21 +110,19 @@ 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
std::time_t time_now = std::time(nullptr);
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));
}
}

Expand Down Expand Up @@ -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",
Expand All @@ -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));
}
}

Expand Down
16 changes: 12 additions & 4 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
}
}

Expand Down

0 comments on commit 4e1d3fa

Please sign in to comment.