string: Add a function for creating a detailed time string
I use this to display the total running time of queues in the gui. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
ed43fd3689
commit
cc6f4c9293
|
@ -5,14 +5,11 @@
|
|||
#include <core/callback.h>
|
||||
#include <core/queue.h>
|
||||
#include <core/random.h>
|
||||
#include <core/string.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
#define O_MINUTES (60)
|
||||
#define O_HOURS (60 * O_MINUTES)
|
||||
#define O_DAYS (24 * O_HOURS)
|
||||
|
||||
|
||||
Queue :: Queue(unsigned int flags)
|
||||
: _cur(-1), _flags(flags), _length(0)
|
||||
|
@ -201,27 +198,9 @@ unsigned int Queue :: size()
|
|||
return _tracks.size();
|
||||
}
|
||||
|
||||
const std::string Queue :: length_str()
|
||||
unsigned int Queue :: length()
|
||||
{
|
||||
std::stringstream ss;
|
||||
unsigned int factor[4] = { O_DAYS, O_HOURS, O_MINUTES, 1 };
|
||||
std::string fields[4] = { "day", "hour", "minute", "second" };
|
||||
unsigned int len = _length;
|
||||
|
||||
for (unsigned int i = 0; i < 4; i++) {
|
||||
unsigned int dur = len / factor[i];
|
||||
len -= dur * factor[i];
|
||||
|
||||
if (dur > 0) {
|
||||
ss << dur << " " << fields[i];
|
||||
if (dur > 1)
|
||||
ss << "s";
|
||||
if (len > 0)
|
||||
ss << ", ";
|
||||
}
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
return _length;
|
||||
}
|
||||
|
||||
class SortTracks {
|
||||
|
|
|
@ -5,6 +5,10 @@
|
|||
#include <core/string.h>
|
||||
#include <sstream>
|
||||
|
||||
#define O_MINUTES (60)
|
||||
#define O_HOURS (60 * O_MINUTES)
|
||||
#define O_DAYS (24 * O_HOURS)
|
||||
|
||||
const std::string string :: utos(unsigned int u)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
@ -24,3 +28,35 @@ const std::string string :: sec2str(unsigned int sec)
|
|||
ss << seconds;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
const std::string _time_detail(unsigned int value, unsigned int remaining,
|
||||
const std::string &field)
|
||||
{
|
||||
std::string res;
|
||||
|
||||
if (value > 0) {
|
||||
res += string :: utos(value) + " " + field;
|
||||
if (value > 1)
|
||||
res += "s";
|
||||
if (remaining > 0)
|
||||
res += ", ";
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
const std::string string :: sec2str_detailed(unsigned int sec)
|
||||
{
|
||||
std::string res;
|
||||
unsigned int factor[3] = { O_DAYS, O_HOURS, O_MINUTES };
|
||||
std::string field[3] = { "day", "hour", "minute" };
|
||||
unsigned int val;
|
||||
|
||||
for (unsigned int i = 0; i < 3; i++) {
|
||||
val = sec / factor[i];
|
||||
sec %= factor[i];
|
||||
res += _time_detail(val, sec, field[i]);
|
||||
}
|
||||
|
||||
res += _time_detail(sec, 0, "second");
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -110,7 +110,7 @@ bool Tab :: tab_is_cur()
|
|||
void Tab :: tab_runtime_changed()
|
||||
{
|
||||
if (tab_is_cur())
|
||||
gui :: get_widget<Gtk::Label>("o_queue_time")->set_text(tab_pq->length_str());
|
||||
gui :: get_widget<Gtk::Label>("o_queue_time")->set_text(string :: sec2str_detailed(tab_pq->length()));
|
||||
}
|
||||
|
||||
void Tab :: tab_display_sorting()
|
||||
|
|
|
@ -168,9 +168,9 @@ public:
|
|||
/**
|
||||
* Find the runtime of the queue.
|
||||
*
|
||||
* @return The runtime of the queue, as a string.
|
||||
* @return The runtime of the queue.
|
||||
*/
|
||||
const std::string length_str();
|
||||
unsigned int length();
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,10 +22,18 @@ namespace string
|
|||
|
||||
/**
|
||||
* Convert from seconds to a time string.
|
||||
* @param sec Number of seconds.
|
||||
* @param sec Number of seconds.
|
||||
* @return A string in mm:ss format.
|
||||
*/
|
||||
const std::string sec2str(unsigned int);
|
||||
|
||||
/**
|
||||
* Convert from seconds to a detailed time string.
|
||||
* @param sec Number of seconds.
|
||||
* @return A string listing the length in terms of days,
|
||||
* months, hours, minutes, and seconds.
|
||||
*/
|
||||
const std::string sec2str_detailed(unsigned int);
|
||||
}
|
||||
|
||||
#endif /* OCARINA_CORE_STRING_H */
|
||||
|
|
|
@ -22,7 +22,6 @@ public:
|
|||
TestQueue(unsigned int f) : Queue(f) {}
|
||||
unsigned int get_cur() { return _cur; }
|
||||
unsigned int get_flags() { return _flags; }
|
||||
unsigned int get_length() { return _length; }
|
||||
std::vector <sort_info> get_sorder() { return _sort_order; };
|
||||
};
|
||||
|
||||
|
@ -37,7 +36,7 @@ void test_default()
|
|||
|
||||
test_equal(q.get_cur(), (unsigned)-1);
|
||||
test_equal(q.get_flags(), (unsigned)0);
|
||||
test_equal(q.get_length(), (unsigned)0);
|
||||
test_equal(q.length(), (unsigned)0);
|
||||
test_equal(q.get_sorder().size(), (size_t)0);
|
||||
test_equal(q.next(), (Track *)NULL);
|
||||
}
|
||||
|
@ -49,7 +48,7 @@ void test_constructor()
|
|||
|
||||
test_equal(q.get_cur(), (unsigned)-1);
|
||||
test_equal(q.get_flags(), flags);
|
||||
test_equal(q.get_length(), (unsigned)0);
|
||||
test_equal(q.length(), (unsigned)0);
|
||||
test_equal(q.get_sorder().size(), (size_t)0);
|
||||
test_equal(q.next(), (Track *)NULL);
|
||||
}
|
||||
|
@ -132,46 +131,40 @@ void test_add_remove()
|
|||
get_callbacks()->on_queue_track_add = test_add_cb;
|
||||
get_callbacks()->on_queue_track_del = test_del_cb;
|
||||
|
||||
test_equal(q.get_length(), expected);
|
||||
test_equal(q.length_str(), (std::string)"");
|
||||
test_equal(q.length(), expected);
|
||||
test_equal(q.size(), (unsigned)0);
|
||||
|
||||
|
||||
/* Add tracks */
|
||||
test_for_each(0, 24, 1, _test_add_loop);
|
||||
test_equal(q.get_length(), expected);
|
||||
test_equal(q.length_str(), (std::string)"1 hour, 26 minutes");
|
||||
test_equal(q.length(), expected);
|
||||
test_equal(q.size(), (unsigned)24);
|
||||
|
||||
|
||||
/* Add everything again */
|
||||
test_for_each(24, 48, 1, _test_add_loop);
|
||||
test_equal(q.get_length(), expected);
|
||||
test_equal(q.length_str(), (std::string)"2 hours, 52 minutes");
|
||||
test_equal(q.length(), expected);
|
||||
test_equal(q.size(), (unsigned)48);
|
||||
|
||||
|
||||
/* Test removing multiple tracks at once */
|
||||
count_del = 0;
|
||||
test_for_each(0, 12, 1, _test_del_loop);
|
||||
test_equal(q.get_length(), expected);
|
||||
test_equal(q.length_str(), (std::string)"1 hour, 50 minutes");
|
||||
test_equal(q.length(), expected);
|
||||
test_equal(q.size(), (unsigned)24);
|
||||
|
||||
|
||||
/* Test removing tracks one at a time */
|
||||
count_del = 0;
|
||||
test_for_each(0, 12, 1, _test_del_middle_loop);
|
||||
test_equal(q.get_length(), expected);
|
||||
test_equal(q.length_str(), (std::string)"55 minutes");
|
||||
test_equal(q.length(), expected);
|
||||
test_equal(q.size(), (unsigned)12);
|
||||
|
||||
|
||||
/* Remove remaining tracks */
|
||||
count_del = 0;
|
||||
test_for_each(0, 12, 1, _test_del_front_loop);
|
||||
test_equal(q.get_length(), (unsigned)0);
|
||||
test_equal(q.length_str(), (std::string)"");
|
||||
test_equal(q.length(), (unsigned)0);
|
||||
test_equal(q.size(), (unsigned)0);
|
||||
}
|
||||
|
||||
|
@ -254,7 +247,7 @@ void test_next()
|
|||
|
||||
test_for_each(0, 24, 1, _test_next_loop);
|
||||
test_equal(q.size(), (unsigned)0);
|
||||
test_equal(q.length_str(), (std::string)"");
|
||||
test_equal(q.length(), 0);
|
||||
test_equal(q.next(), TRACK_NULL);
|
||||
|
||||
|
||||
|
@ -264,7 +257,7 @@ void test_next()
|
|||
|
||||
test_for_each(0, 24, 1, _test_next_rand_loop);
|
||||
test_equal(q.size(), (unsigned)0);
|
||||
test_equal(q.length_str(), (std::string)"");
|
||||
test_equal(q.length(), 0);
|
||||
test_equal(q.next(), TRACK_NULL);
|
||||
|
||||
|
||||
|
@ -380,7 +373,7 @@ void test_saving()
|
|||
|
||||
test_equal(r.has_flag(Q_RANDOM), q.has_flag(Q_RANDOM));
|
||||
test_equal(r.size(), q.size());
|
||||
test_equal(r.length_str(), q.length_str());
|
||||
test_equal(r.length(), q.length());
|
||||
test_for_each(0, 24, 1, _test_saving_loop);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,8 +29,32 @@ void test_sec2str()
|
|||
test_equal(string :: sec2str(675), "11:15");
|
||||
}
|
||||
|
||||
void test_sec2str_detailed()
|
||||
{
|
||||
|
||||
unsigned int minute = 60;
|
||||
unsigned int hour = 3600;
|
||||
unsigned int day = 86400;
|
||||
|
||||
test_equal(string :: sec2str_detailed(day), "1 day");
|
||||
test_equal(string :: sec2str_detailed(2 * day), "2 days");
|
||||
|
||||
test_equal(string :: sec2str_detailed(day + hour), "1 day, 1 hour");
|
||||
test_equal(string :: sec2str_detailed(hour), "1 hour");
|
||||
test_equal(string :: sec2str_detailed(2 * hour), "2 hours");
|
||||
|
||||
test_equal(string :: sec2str_detailed(hour + minute), "1 hour, 1 minute");
|
||||
test_equal(string :: sec2str_detailed(minute), "1 minute");
|
||||
test_equal(string :: sec2str_detailed(2 * minute), "2 minutes");
|
||||
|
||||
test_equal(string :: sec2str_detailed(minute + 1), "1 minute, 1 second");
|
||||
test_equal(string :: sec2str_detailed(1), "1 second");
|
||||
test_equal(string :: sec2str_detailed(2), "2 seconds");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
test :: run("Unsigned to String Test", test_utos);
|
||||
test :: run("Seconds to String Test", test_sec2str);
|
||||
test :: run("Seconds to String (Detailed) Test", test_sec2str_detailed);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue