Skip to content

Instantly share code, notes, and snippets.

@lilyball
Created October 9, 2012 03:25
Show Gist options
  • Select an option

  • Save lilyball/3856398 to your computer and use it in GitHub Desktop.

Select an option

Save lilyball/3856398 to your computer and use it in GitHub Desktop.
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
std::string human_readable_duration(int num_seconds) {
std::stringstream buf;
if (num_seconds < 60) {
buf << num_seconds << " seconds";
} else {
std::vector<int> components;
while (num_seconds > 0) {
int comp = num_seconds % 60;
components.push_back(comp);
num_seconds /= 60;
}
std::vector<int>::const_reverse_iterator iter = components.rbegin();
buf << *(iter++);
for (; iter != components.rend(); ++iter) {
buf << ":" << setw(2) << setfill('0') << *iter;
}
}
return buf.str();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment