From 9a25f47250e51df6b4f3fe6579ff547b6daa9284 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Thu, 19 Dec 2013 16:23:25 -0800 Subject: [PATCH] Fix printing of time --- src/benchmark.cc | 3 +-- src/walltime.cc | 29 +++++++++++++++-------------- src/walltime.h | 12 +++++++----- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/benchmark.cc b/src/benchmark.cc index 3394044a..0d93eb1e 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -341,10 +341,9 @@ bool ConsoleReporter::ReportContext(const BenchmarkContextData& context) { << ((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 1c95fd55..835b973e 100644 --- a/src/walltime.cc +++ b/src/walltime.cc @@ -119,21 +119,22 @@ WallTime Now() { 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((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((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 660e2ba6..7d34ddfe 100644 --- a/src/walltime.h +++ b/src/walltime.h @@ -1,6 +1,8 @@ #ifndef BENCHMARK_WALLTIME_H_ #define BENCHMARK_WALLTIME_H_ +#include + namespace benchmark { typedef double WallTime; @@ -10,11 +12,11 @@ WallTime Now(); // 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