Skip to content

Commit f88c020

Browse files
phprusvitaut
authored andcommitted
Generalization of strftime/wcsftime function calls in tests
1 parent 2eeddba commit f88c020

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

test/chrono-test.cc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ auto make_second(int s) -> std::tm {
4242
return time;
4343
}
4444

45+
std::string system_strftime(const std::string& format, const std::tm* timeptr,
46+
size_t maxsize = 1024) {
47+
std::vector<char> output(maxsize);
48+
auto size =
49+
std::strftime(output.data(), output.size(), format.c_str(), timeptr);
50+
return std::string(output.data(), size);
51+
}
52+
4553
TEST(chrono_test, format_tm) {
4654
auto tm = std::tm();
4755
tm.tm_year = 116;
@@ -102,21 +110,19 @@ TEST(chrono_test, format_tm) {
102110
std::time_t t = std::mktime(&tm);
103111
tm = *std::localtime(&t);
104112

105-
char output[256] = {};
106-
std::strftime(output, sizeof(output), iso_week_spec.c_str(), &tm);
107113
auto fmt_spec = std::string("{:").append(iso_week_spec).append("}");
108-
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm));
114+
EXPECT_EQ(system_strftime(iso_week_spec, &tm),
115+
fmt::format(fmt::runtime(fmt_spec), tm));
109116
}
110117

111118
// Every day from 1970-01-01
112119
std::time_t time_now = std::time(nullptr);
113120
for (std::time_t t = 6 * 3600; t < time_now; t += 86400) {
114121
tm = *std::localtime(&t);
115122

116-
char output[256] = {};
117-
std::strftime(output, sizeof(output), iso_week_spec.c_str(), &tm);
118123
auto fmt_spec = std::string("{:").append(iso_week_spec).append("}");
119-
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm));
124+
EXPECT_EQ(system_strftime(iso_week_spec, &tm),
125+
fmt::format(fmt::runtime(fmt_spec), tm));
120126
}
121127
}
122128

@@ -208,22 +214,20 @@ TEST(chrono_test, gmtime) {
208214
EXPECT_TRUE(equal(tm, fmt::gmtime(t)));
209215
}
210216

211-
template <typename TimePoint> auto strftime(TimePoint tp) -> std::string {
217+
template <typename TimePoint> auto strftime_full(TimePoint tp) -> std::string {
212218
auto t = std::chrono::system_clock::to_time_t(tp);
213219
auto tm = *std::localtime(&t);
214-
char output[256] = {};
215-
std::strftime(output, sizeof(output), "%Y-%m-%d %H:%M:%S", &tm);
216-
return output;
220+
return system_strftime("%Y-%m-%d %H:%M:%S", &tm);
217221
}
218222

219223
TEST(chrono_test, time_point) {
220224
auto t1 = std::chrono::system_clock::now();
221-
EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
222-
EXPECT_EQ(strftime(t1), fmt::format("{}", t1));
225+
EXPECT_EQ(strftime_full(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
226+
EXPECT_EQ(strftime_full(t1), fmt::format("{}", t1));
223227
using time_point =
224228
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
225229
auto t2 = time_point(std::chrono::seconds(42));
226-
EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
230+
EXPECT_EQ(strftime_full(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
227231

228232
std::vector<std::string> spec_list = {
229233
"%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", "%EC",
@@ -236,12 +240,12 @@ TEST(chrono_test, time_point) {
236240
for (const auto& spec : spec_list) {
237241
auto t = std::chrono::system_clock::to_time_t(t1);
238242
auto tm = *std::localtime(&t);
239-
char output[256] = {};
240-
std::strftime(output, sizeof(output), spec.c_str(), &tm);
243+
244+
auto sys_output = system_strftime(spec, &tm);
241245

242246
auto fmt_spec = std::string("{:").append(spec).append("}");
243-
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), t1));
244-
EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm));
247+
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
248+
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
245249
}
246250
}
247251

test/xchar-test.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ TEST(xchar_test, chrono) {
267267
EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33");
268268
}
269269

270+
std::wstring system_wcsftime(const std::wstring& format, const std::tm* timeptr,
271+
size_t maxsize = 1024) {
272+
std::vector<wchar_t> output(maxsize);
273+
auto size =
274+
std::wcsftime(output.data(), output.size(), format.c_str(), timeptr);
275+
return std::wstring(output.data(), size);
276+
}
277+
270278
TEST(chrono_test, time_point) {
271279
auto t1 = std::chrono::system_clock::now();
272280

@@ -282,12 +290,12 @@ TEST(chrono_test, time_point) {
282290
for (const auto& spec : spec_list) {
283291
auto t = std::chrono::system_clock::to_time_t(t1);
284292
auto tm = *std::localtime(&t);
285-
wchar_t output[1024] = {};
286-
std::wcsftime(output, sizeof(output) / sizeof(wchar_t), spec.c_str(), &tm);
293+
294+
auto sys_output = system_wcsftime(spec, &tm);
287295

288296
auto fmt_spec = std::wstring(L"{:").append(spec).append(L"}");
289-
EXPECT_EQ(output, fmt::format(fmt_spec, t1));
290-
EXPECT_EQ(output, fmt::format(fmt_spec, tm));
297+
EXPECT_EQ(sys_output, fmt::format(fmt_spec, t1));
298+
EXPECT_EQ(sys_output, fmt::format(fmt_spec, tm));
291299
}
292300
}
293301

0 commit comments

Comments
 (0)