@@ -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+
4553TEST (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
219223TEST (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
0 commit comments