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

  • Buried in cloud files? We can help with Spring cleaning!

    Whether you use Dropbox, Drive, G-Suite, OneDrive, Gmail, Slack, Notion, or all of the above, Dokkio will organize your files for you. Try Dokkio (from the makers of PBworks) for free today.

  • Dokkio (from the makers of PBworks) was #2 on Product Hunt! Check out what people are saying by clicking here.



Page history last edited by Kenneth Finnegan 14 years 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.



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);


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



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.



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.



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.