Add support for hours display to timeToString (#3270)

* Add hours support to timeToString

* Update timeToString declaration
This commit is contained in:
Alayan-stk-2 2018-05-29 02:17:52 +02:00 committed by auriamg
parent 670a40cb05
commit c4768b72ae
2 changed files with 25 additions and 13 deletions

View File

@ -504,7 +504,7 @@ namespace StringUtils
* \param time Time in seconds. * \param time Time in seconds.
* \param precision The number of seconds decimals - 3 to display ms, default 2 * \param precision The number of seconds decimals - 3 to display ms, default 2
*/ */
std::string timeToString(float time, unsigned int precision, bool display_minutes_if_zero) std::string timeToString(float time, unsigned int precision, bool display_minutes_if_zero, bool display_hours)
{ {
//Time more detailed than ms are mostly meaningless //Time more detailed than ms are mostly meaningless
if (precision > 3) if (precision > 3)
@ -546,12 +546,15 @@ namespace StringUtils
else else
final_append = ""; final_append = "";
// concatenate the strings with + // concatenate the strings with +
if (display_minutes_if_zero) if (display_hours)
return (std::string("00:00:00") + final_append);
else if (display_minutes_if_zero)
return (std::string("00:00") + final_append); return (std::string("00:00") + final_append);
else else
return (std::string("00") + final_append); return (std::string("00") + final_append);
} }
else if(int_time >= 100*60*precision_power) // up to 99:59.999 else if((int_time >= 60*60*precision_power && !display_hours) ||
int_time >= 100*60*60*precision_power)
{ {
std::string final_append; std::string final_append;
if (precision == 3) if (precision == 3)
@ -563,7 +566,10 @@ namespace StringUtils
else else
final_append = ""; final_append = "";
// concatenate the strings with + // concatenate the strings with +
return (std::string("99:59") + final_append); if (display_hours)
return (std::string("99:59:59") + final_append);
else
return (std::string("59:59") + final_append);
} }
// Principle of the computation in pseudo-code // Principle of the computation in pseudo-code
@ -575,11 +581,15 @@ namespace StringUtils
int_time = int_time/precision_power; int_time = int_time/precision_power;
int sec = int_time % 60; int sec = int_time % 60;
int_time = int_time/60; int_time = int_time/60;
if (int_time >= 100) int_time = 99; int min = int_time % 60;
int min = int_time; int_time = int_time/60;
int hours = int_time;
// Convert the times to string and add the missing zeroes if any // Convert the times to string and add the missing zeroes if any
std::string s_hours = toString(hours);
if (hours < 10)
s_hours = std::string("0") + s_hours;
std::string s_min = toString(min); std::string s_min = toString(min);
if (min < 10) if (min < 10)
s_min = std::string("0") + s_min; s_min = std::string("0") + s_min;
@ -602,14 +612,15 @@ namespace StringUtils
if(negative_time) if(negative_time)
s_neg = "-"; s_neg = "-";
std::string s_min_and_sec = s_sec; std::string s_hours_min_and_sec = s_sec;
if (display_minutes_if_zero || min > 0) if (display_minutes_if_zero || min > 0 || display_hours)
s_min_and_sec = s_min + std::string(":") + s_sec; s_hours_min_and_sec = s_min + std::string(":") + s_sec;
if (display_hours)
s_hours_min_and_sec = s_hours + std::string(":") + s_hours_min_and_sec;
if (precision == 0) if (precision == 0)
return (s_neg + s_min_and_sec); return (s_neg + s_hours_min_and_sec);
else else
return (s_neg + s_min_and_sec + std::string(".") + s_subsec); return (s_neg + s_hours_min_and_sec + std::string(".") + s_subsec);
} // timeToString } // timeToString
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -46,7 +46,8 @@ namespace StringUtils
bool notEmpty(const irr::core::stringw& input); bool notEmpty(const irr::core::stringw& input);
std::string ticksTimeToString(int time); std::string ticksTimeToString(int time);
std::string timeToString(float time, unsigned int precision=2, bool display_minutes_if_zero = true); std::string timeToString(float time, unsigned int precision=2,
bool display_minutes_if_zero = true, bool display_hours = false);
irr::core::stringw loadingDots(float interval = 0.5f, int max_dots = 3); irr::core::stringw loadingDots(float interval = 0.5f, int max_dots = 3);
irr::core::stringw loadingDots(const wchar_t *s); irr::core::stringw loadingDots(const wchar_t *s);
std::string toUpperCase(const std::string&); std::string toUpperCase(const std::string&);