libCom: Fix epicsTime::strftime() roll-over bug

Fractional seconds could round-up to .000 without
incrementing the integer seconds.
We can't actually do the latter, so we prevent the
roll-over and clamp at all 9's instead.
Idea from Eric Norum.
This commit is contained in:
Andrew Johnson
2014-01-29 16:52:22 -06:00
parent bfde24907c
commit b32127c5de
2 changed files with 17 additions and 3 deletions

View File

@@ -554,9 +554,11 @@ size_t epicsTime::strftime (
static_cast < unsigned long > ( 1e1 ),
static_cast < unsigned long > ( 1e0 )
};
// round and convert nanosecs to integer of correct range
// round without overflowing into whole seconds
unsigned long frac = tmns.nSec + div[fracWid] / 2;
frac %= static_cast < unsigned long > ( 1e9 );
if (frac >= nSecPerSec)
frac = nSecPerSec - 1;
// convert nanosecs to integer of correct range
frac /= div[fracWid];
char fracFormat[32];
sprintf ( fracFormat, "%%0%lulu", fracWid );