Support formatting of negative time.

This commit is contained in:
Bartosz Taudul 2017-09-24 16:45:08 +02:00
parent 36ffaec1df
commit 29d5b8d4e9
2 changed files with 14 additions and 7 deletions

View File

@ -523,7 +523,7 @@ Vector<Event*>& View::GetParentVector( const Event& ev )
} }
} }
const char* View::TimeToString( uint64_t ns ) const const char* View::TimeToString( int64_t ns ) const
{ {
enum { Pool = 4 }; enum { Pool = 4 };
static char bufpool[Pool][64]; static char bufpool[Pool][64];
@ -531,27 +531,34 @@ const char* View::TimeToString( uint64_t ns ) const
char* buf = bufpool[bufsel]; char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool; bufsel = ( bufsel + 1 ) % Pool;
const char* sign = "";
if( ns < 0 )
{
sign = "-";
ns = -ns;
}
if( ns < 1000 ) if( ns < 1000 )
{ {
sprintf( buf, "%" PRIu64 " ns", ns ); sprintf( buf, "%s%" PRIu64 " ns", sign, ns );
} }
else if( ns < 1000ull * 1000 ) else if( ns < 1000ull * 1000 )
{ {
sprintf( buf, "%.2f us", ns / 1000. ); sprintf( buf, "%s%.2f us", sign, ns / 1000. );
} }
else if( ns < 1000ull * 1000 * 1000 ) else if( ns < 1000ull * 1000 * 1000 )
{ {
sprintf( buf, "%.2f ms", ns / ( 1000. * 1000. ) ); sprintf( buf, "%s%.2f ms", sign, ns / ( 1000. * 1000. ) );
} }
else if( ns < 1000ull * 1000 * 1000 * 60 ) else if( ns < 1000ull * 1000 * 1000 * 60 )
{ {
sprintf( buf, "%.2f s", ns / ( 1000. * 1000. * 1000. ) ); sprintf( buf, "%s%.2f s", sign, ns / ( 1000. * 1000. * 1000. ) );
} }
else else
{ {
const auto m = ns / ( 1000ull * 1000 * 1000 * 60 ); const auto m = ns / ( 1000ull * 1000 * 1000 * 60 );
const auto s = ns - m * ( 1000ull * 1000 * 1000 * 60 ); const auto s = ns - m * ( 1000ull * 1000 * 1000 * 60 );
sprintf( buf, "%" PRIu64 ":%04.1f", m, s / ( 1000. * 1000. * 1000. ) ); sprintf( buf, "%s%" PRIu64 ":%04.1f", sign, m, s / ( 1000. * 1000. * 1000. ) );
} }
return buf; return buf;
} }

View File

@ -67,7 +67,7 @@ private:
uint64_t GetLastTime() const; uint64_t GetLastTime() const;
int64_t GetZoneEnd( const Event& ev ) const; int64_t GetZoneEnd( const Event& ev ) const;
Vector<Event*>& GetParentVector( const Event& ev ); Vector<Event*>& GetParentVector( const Event& ev );
const char* TimeToString( uint64_t ns ) const; const char* TimeToString( int64_t ns ) const;
const char* GetString( uint64_t ptr ) const; const char* GetString( uint64_t ptr ) const;
const char* GetThreadString( uint64_t id ) const; const char* GetThreadString( uint64_t id ) const;