Fix printing of time

This commit is contained in:
Dominic Hamon 2013-12-19 16:23:25 -08:00
parent e390e4ebc3
commit 9a25f47250
3 changed files with 23 additions and 21 deletions

View File

@ -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())

View File

@ -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<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

View File

@ -1,6 +1,8 @@
#ifndef BENCHMARK_WALLTIME_H_
#define BENCHMARK_WALLTIME_H_
#include <string>
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