Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

no leading zero's when outputting time

Status
Not open for further replies.

AndyHollywood

Programmer
Oct 7, 2001
30
GB
I am outputting time in this format:

KeyDOWN:16,Thu Jan 03 16:33:51.284 2002

but on certain lines where it outputs the seconds.milliseconds the leading zero..if there is one is sometimes removed! e.g.

it will output: 16:33:51.23
when it should really be: 16:33:51.023

this makes it difficult for me later ....why would it be doing this?

This is the timing code that i am using:
//Global
/*Timer Elements*/
struct _timeb timebuffer;
char *timeline;


//In the function

//Timer Elements
_ftime( &timebuffer );
timeline = ctime( & ( timebuffer.time ) );




fprintf(pKB, "KeyUP:%d,%.19s.%hu %s",wParam,timeline, timebuffer.millitm, &timeline[20] );

am i doing something wrong?!?!? or is there another way?

cheers in advance for any insite into this!

Andy
 
The only advice I can give is this: Don't copy the examples out of the MSDN library and expect them to work. This IS microsoft, remember??
 
Would adding a "03" to the format string work (pad with zeros, minimum width three positions) work?

fprintf(pKB, "KeyUP:%d,%.19s.%03hu %s",wParam,timeline, timebuffer.millitm, &timeline[20] );
 
Actually the syntax is %.3hu, the original guy on this thread always wanted three places for the milliseconds, at least the way I read it. I've never used the "h" specifier before, but I believe it means a "half word" or only 16 bits. Can anyone verify this??
tmoody
 
Here's the syntax for Format Specification Fields:

%[flags] [width] [.precision] [{h | l | I64 | L}]type

According to MSDN, %hu means "short unsigned int". My example was correct, based on the following from MSDN:

Width
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).

Precision
The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table R.5). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value.
 
FYI...the "h" modifier signifies to display a short integer.

;-)

Rob
"Programming is like art...It makes me feel like chopping my ear off."

- Currently down
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top