| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

DisplayTime

Page history last edited by Kenneth Finnegan 15 years, 11 months ago

You could use this code snippet to nicely display time so it's easier to read than one really big number of seconds, without loosing the accuracy by rounding it down to minutes or hours.

 

Code:

char buf[10];

int secs;

 

 if (secs < 100)

     sprintf (buf, "%ds", secs);

else if (secs < 100 * 60)

     sprintf (buf, "%dm%ds", secs / 60, secs % 60);

else if (secs < 48 * 3600)

     sprintf (buf, "%dh%dm", secs / 3600, (secs / 60) % 60);

else if (secs < 100 * 86400)

     sprintf (buf, "%dd%dh", secs / 86400, (secs / 3600) % 60);

else

     sprintf (buf, "%dd", secs / 86400);

 

Usage:

Put the code in a function like seconds_to_human(int secs, char *buf) and call it with the number of seconds you want to display and a buffer to store it in.

 


Extensions:

Check out the original wget code to see how they use it.  It's very clever because they use a static char[] to cache the string so it doesn't need to constantly be regenerated.

 


Sources:

http://www.gnu.org/software/wget/  ./src/progress.c :: static const char * eta_to_human_short (int secs, bool condensed)

Comments (0)

You don't have permission to comment on this page.