Fix printing of time
diff --git a/src/benchmark.cc b/src/benchmark.cc
index 3394044..0d93eb1 100644
--- a/src/benchmark.cc
+++ b/src/benchmark.cc
@@ -341,10 +341,9 @@
<< ((context.num_cpus > 1) ? "s" : "") << "\n";
int remainder_ms;
- char time_buf[32];
std::cout << walltime::Print(walltime::Now(), "%Y/%m/%d-%H:%M:%S",
true, // use local timezone
- time_buf, &remainder_ms) << "\n";
+ &remainder_ms) << "\n";
// Show details of CPU model, caches, TLBs etc.
// if (!context.cpu_info.empty())
diff --git a/src/walltime.cc b/src/walltime.cc
index 1c95fd5..835b973 100644
--- a/src/walltime.cc
+++ b/src/walltime.cc
@@ -119,21 +119,22 @@
return now;
}
-const char* Print(WallTime time, const char *format, bool local,
- char* storage, int *remainder_us) {
- struct tm split;
- double subsecond;
- if (!SplitTimezone(time, local, &split, &subsecond)) {
- snprintf(storage, sizeof(storage), "Invalid time: %f", time);
- } else {
- if (remainder_us != NULL) {
- *remainder_us = static_cast<int>((subsecond * 1000000) + 0.5);
- if (*remainder_us > 999999) *remainder_us = 999999;
- if (*remainder_us < 0) *remainder_us = 0;
- }
- strftime(storage, sizeof(storage), format, &split);
+std::string Print(WallTime time, const char *format, bool local,
+ int *remainder_us) {
+ char storage[32];
+ struct tm split;
+ double subsecond;
+ if (!SplitTimezone(time, local, &split, &subsecond)) {
+ snprintf(storage, sizeof(storage), "Invalid time: %f", time);
+ } else {
+ if (remainder_us != NULL) {
+ *remainder_us = static_cast<int>((subsecond * 1000000) + 0.5);
+ if (*remainder_us > 999999) *remainder_us = 999999;
+ if (*remainder_us < 0) *remainder_us = 0;
}
- return storage;
+ strftime(storage, sizeof(storage), format, &split);
+ }
+ return std::string(storage);
}
} // end namespace walltime
} // end namespace benchmark
diff --git a/src/walltime.h b/src/walltime.h
index 660e2ba..7d34ddf 100644
--- a/src/walltime.h
+++ b/src/walltime.h
@@ -1,6 +1,8 @@
#ifndef BENCHMARK_WALLTIME_H_
#define BENCHMARK_WALLTIME_H_
+#include <string>
+
namespace benchmark {
typedef double WallTime;
@@ -10,11 +12,11 @@
// GIVEN: walltime, generic format string (as understood by strftime),
// a boolean flag specifying if the time is local or UTC (true=local).
-// RETURNS: the formatted string. ALSO RETURNS: the storage printbuffer
-// passed and the remaining number of microseconds (never printed in
-// the string since strftime does not understand it)
-const char* Print(WallTime time, const char *format, bool local,
- char* storage, int *remainder_us);
+// RETURNS: the formatted string. ALSO RETURNS: the remaining number of
+// microseconds (never printed in the string since strftime does not understand
+// it)
+std::string Print(WallTime time, const char *format, bool local,
+ int *remainder_us);
} // end namespace walltime
} // end namespace benchmark