Simplify implementation of human_readable_duration()
This commit is contained in:
@@ -35,10 +35,6 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <string>
|
|
||||||
#include <sstream>
|
|
||||||
#include <vector>
|
|
||||||
#include <iomanip>
|
|
||||||
|
|
||||||
#include "terminaloverlay.h"
|
#include "terminaloverlay.h"
|
||||||
|
|
||||||
@@ -176,24 +172,16 @@ NotificationEngine::NotificationEngine()
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
static std::string human_readable_duration(int num_seconds) {
|
static std::string human_readable_duration(int num_seconds) {
|
||||||
static int divisions[3] = {60, 60, 24};
|
char tmp[ 128 ];
|
||||||
std::stringstream buf;
|
if ( num_seconds < 60 ) {
|
||||||
if (num_seconds < divisions[0]) {
|
snprintf( tmp, 128, "%d seconds", num_seconds );
|
||||||
buf << num_seconds << " seconds";
|
} else if ( num_seconds < 3600 ) {
|
||||||
|
snprintf( tmp, 128, "%d:%02d", num_seconds / 60, num_seconds % 60 );
|
||||||
} else {
|
} else {
|
||||||
std::vector<int> components;
|
snprintf( tmp, 128, "%d:%02d:%02d", num_seconds / 3600,
|
||||||
for (unsigned int d = 0; d < sizeof(divisions)/sizeof(divisions[0]) && num_seconds > 0; num_seconds /= divisions[d++]) {
|
(num_seconds / 60) % 60, num_seconds % 60 );
|
||||||
int comp = num_seconds % divisions[d];
|
|
||||||
components.push_back(comp);
|
|
||||||
}
|
}
|
||||||
if (num_seconds > 0) components.push_back(num_seconds);
|
return tmp;
|
||||||
std::vector<int>::const_reverse_iterator iter = components.rbegin();
|
|
||||||
buf << *(iter++);
|
|
||||||
for (; iter != components.rend(); ++iter) {
|
|
||||||
buf << ":" << std::setw(2) << std::setfill('0') << *iter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return buf.str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NotificationEngine::apply( Framebuffer &fb ) const
|
void NotificationEngine::apply( Framebuffer &fb ) const
|
||||||
|
|||||||
Reference in New Issue
Block a user