Caprooja
Caprooja

Reputation: 895

Display time string given system tick count

Given:

How can I create a fixed-width time stamp string in C? I can be flexible in this format, but would like to support a maximum of 100 minutes and keep millisecond resolution. One example format would be "99:59:999" meaning it has been 99 mins and 59.999 seconds since cold start.

printf allows me to write decimals (minute, second, ms) with a minimum width, but not a maximum width. I am looking for simple solutions.

Upvotes: 1

Views: 248

Answers (1)

ChuckCottrill
ChuckCottrill

Reputation: 4444

You want to print a fixed width, leading zeroes fractional part for the microseconds. Since there are 6 digits in microseconds, you want,

sprintf(timestamp,"%d:%02d.%06d",ticks/60,ticks%60,mseconds);

And add struct/pointer flavor to taste...

Upvotes: 1

Related Questions