[Opalvoip-svn] SF.net SVN: opalvoip:[34821] ptlib/trunk
Brought to you by:
csoutheren,
rjongbloed
From: <rjo...@us...> - 2016-05-09 09:47:14
|
Revision: 34821 http://sourceforge.net/p/opalvoip/code/34821 Author: rjongbloed Date: 2016-05-09 09:47:12 +0000 (Mon, 09 May 2016) Log Message: ----------- Enhanced the PTimeInterval printing so can do nanosecond accuracy. Also cleared up some incorrect documentation and code around negative precision (doc said width!) used to indicate formatting options. Modified Paths: -------------- ptlib/trunk/include/ptlib/timeint.h ptlib/trunk/samples/timing/timing.cxx ptlib/trunk/src/ptlib/common/ptime.cxx Modified: ptlib/trunk/include/ptlib/timeint.h =================================================================== --- ptlib/trunk/include/ptlib/timeint.h 2016-05-06 18:32:03 UTC (rev 34820) +++ ptlib/trunk/include/ptlib/timeint.h 2016-05-09 09:47:12 UTC (rev 34821) @@ -150,11 +150,12 @@ }; /** Output the time interval as a string. - The \p width, if negative, can control which components (hours/minutes) - are output. < -6 suppresses hour, < -3 suppresses minutes. + If \p precision is <= 0 and \p format is NormalFormat then the + format used will be IncludeDays, and the positive value + for \p precision is then used as number of decimals. */ PString AsString( - int decimals = 3, ///< DEcimals for milliseconds part + int decimals = 3, ///< Decimals for milliseconds part Formats format = NormalFormat, ///< Output format options int width = 1 ///< Width of output ) const; @@ -448,12 +449,6 @@ ) const; //@} - protected: - virtual int64_t InternalGet() const; - virtual void InternalSet(int64_t t); - - private: - atomic<int64_t> m_nanoseconds; static int64_t const MicroToNano = 1000; static int64_t const MilliToNano = MicroToNano*1000; static int64_t const SecsToNano = MilliToNano*1000; @@ -461,7 +456,14 @@ static int64_t const HoursToNano = MinsToNano*60; static int64_t const DaysToNano = HoursToNano*24; + protected: + virtual int64_t InternalGet() const; + virtual void InternalSet(int64_t t); + private: + atomic<int64_t> m_nanoseconds; + + // Include platform dependent part of class #ifdef _WIN32 #include "msos/ptlib/timeint.h" Modified: ptlib/trunk/samples/timing/timing.cxx =================================================================== --- ptlib/trunk/samples/timing/timing.cxx 2016-05-06 18:32:03 UTC (rev 34820) +++ ptlib/trunk/samples/timing/timing.cxx 2016-05-09 09:47:12 UTC (rev 34821) @@ -49,6 +49,26 @@ { cout << "Timing Test Program\n" << endl; + PTimeInterval nano = PTimeInterval::NanoSeconds(-543210123456789LL); + int w, p; + for (w = 1; w <= 18; w++) + for (p = 0; p <= 9; p++) + cout << "TimeInterval scientific, width " << w << ", precision " << p << ": \"" + << setiosflags(ios::scientific) + << setw(w) << setprecision(p) << nano + << resetiosflags(ios::scientific) << '"' << endl; + nano.SetNanoSeconds(PTimeInterval::DaysToNano*10+ + PTimeInterval::HoursToNano*12+ + PTimeInterval::MinsToNano*34+ + PTimeInterval::SecsToNano*56+ + 123456789); + for (w = 1; w <= 23; w++) + for (p = -9; p <= 9; p++) + cout << "TimeInterval output, width " << w << ", precision " << p << ": \"" + << setw(w) << setprecision(p) << nano << '"' << endl; + + cout << "\n\n"; + PTime now; cout << "Time is now " << now.AsString("h:m:s.u d/M/y") << "\n" "Time is now " << now.AsString("yyyy/MM/dd h:m:s.uuuu") << "\n" @@ -119,19 +139,6 @@ cout << "Actual resolution is " << 1000000/count << "us\n" "Current tick: \"" << newTick << '"' << endl; - oldTick = 123456; - int w, p; - for (w = 1; w <= 10; w++) - for (p = 0; p < 4; p++) - cout << "TimeInterval output, width " << w << ", precision " << p << ": \"" - << setw(w) << setprecision(p) << oldTick << '"' << endl; - for (w = 1; w <= 8; w++) - for (p = 0; p < 4; p++) - cout << "TimeInterval scientific, width " << w << ", precision " << p << ": \"" - << setiosflags(ios::scientific) - << setw(w) << setprecision(p) << oldTick - << resetiosflags(ios::scientific) << '"' << endl; - cout << "\nTesting sleep function" << endl; PTime start_time1; PINDEX loop; Modified: ptlib/trunk/src/ptlib/common/ptime.cxx =================================================================== --- ptlib/trunk/src/ptlib/common/ptime.cxx 2016-05-06 18:32:03 UTC (rev 34820) +++ ptlib/trunk/src/ptlib/common/ptime.cxx 2016-05-09 09:47:12 UTC (rev 34821) @@ -64,17 +64,9 @@ void PTimeInterval::PrintOn(ostream & stream) const { - int precision = (int)stream.precision(); - - Formats fmt = NormalFormat; - if ((stream.flags()&ios::scientific) != 0) - fmt = SecondsOnly; - else if (precision > -4 && precision < 0) { - fmt = IncludeDays; - precision = -precision; - } - - stream << AsString(precision, fmt, (int)stream.width()); + stream << AsString((int)stream.precision(), + (stream.flags()&ios::scientific) != 0 ? SecondsOnly : NormalFormat, + (int)stream.width()); } @@ -103,93 +95,57 @@ str << right << setfill('0'); - if (precision > 3) - precision = 3; - else if (precision < -9) - precision = -9; - else if (precision < -6) - precision = -6; - else if (precision < -3) - precision = -3; + if (precision <= 0 && format == NormalFormat) { + format = IncludeDays; + precision = -precision; + } + if (precision > 9) + precision = 9; - PInt64 ms = GetMilliSeconds(); - if (ms < 0) { + PInt64 ns = GetNanoSeconds(); + if (ns < 0) { str << '-'; - ms = -ms; + ns = -ns; } - if (format == SecondsOnly) { - switch (precision) { - case 1 : - str << ms/1000 << '.' << (int)(ms%1000+50)/100; - break; + if (format == SecondsOnly) + str << ns/SecsToNano; + else { + bool hadPrevious = false; - case 2 : - str << ms/1000 << '.' << setw(2) << (int)(ms%1000+5)/10; - break; - - case 3 : - str << ms/1000 << '.' << setw(3) << (int)(ms%1000); - break; - - default : - str << (ms+500)/1000; - } - - return str; - } - - PBoolean hadPrevious = false; - long tmp; - - if (format == IncludeDays) { - tmp = (long)(ms/86400000); - if (tmp > 0 || width > (precision+10)) { - str << tmp << 'd'; + if (format == IncludeDays && (ns > DaysToNano || width > (precision + 10))) { + str << ns/DaysToNano << 'd'; + ns = ns % DaysToNano; hadPrevious = true; } - tmp = (long)(ms%86400000)/3600000; - } - else - tmp = (long)(ms/3600000); - - if (precision >= -9) { - if (hadPrevious || tmp > 0 || width > (precision+7)) { + if (hadPrevious || ns > HoursToNano || width > (precision + 7)) { if (hadPrevious) str << ':' << setw(2); - str << tmp; + str << ns/HoursToNano; hadPrevious = true; } - } - if (precision >= -6) { - tmp = (long)(ms%3600000)/60000; - if (hadPrevious || tmp > 0 || width > (precision+4)) { + ns = ns % HoursToNano; + if (hadPrevious || ns > MinsToNano || width > (precision + 4)) { if (hadPrevious) str << ':' << setw(2); - str << tmp; + str << ns/MinsToNano; hadPrevious = true; } - } - if (precision >= -3) { if (hadPrevious) str << ':' << setw(2); - str << (long)(ms%60000)/1000; + str << (ns % MinsToNano) / SecsToNano; } - switch (precision) { - case 1 : - str << '.' << (int)(ms%1000)/100; - break; + ns = ns%SecsToNano; - case 2 : - str << '.' << setw(2) << (int)(ms%1000)/10; - break; - - case 3 : - str << '.' << setw(3) << (int)(ms%1000); + if (precision > 0) { + int64_t powerOfTen = SecsToNano; + for (int i = 0; i < precision; ++i) + powerOfTen /= 10; + str << '.' << setw(precision) << (int)(ns%SecsToNano+powerOfTen/2)/powerOfTen; } return str; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |