Skip to content
Snippets Groups Projects
Commit 66c589fa authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Create duration_estimate_to_[v]str() functions

Sometimes you want to see "1.5 minutes" (or "1.5M") intead of "90 seconds"
(or "90S")

I noticed some left over questionable use of ulong here in byte_count related
functions, so fixed that.
parent 3ac5570e
No related branches found
No related tags found
No related merge requests found
Pipeline #5010 passed
......@@ -251,7 +251,7 @@ char* c_escape_str(const char* src, char* dst, size_t maxlen, BOOL ctrl_only)
*
* Moved from ini_file.c/parseBytes()
*/
int64_t parse_byte_count(const char* str, ulong unit)
int64_t parse_byte_count(const char* str, uint64_t unit)
{
char* p=NULL;
double bytes;
......@@ -319,7 +319,7 @@ char* byte_count_to_str(uint64_t bytes, char* str, size_t size)
with a single decimal place and a single letter multiplier/suffix.
'unit' is the smallest divisor used.
*/
char* byte_estimate_to_str(uint64_t bytes, char* str, size_t size, ulong unit, int precision)
char* byte_estimate_to_str(uint64_t bytes, char* str, size_t size, uint64_t unit, int precision)
{
if(bytes >= one_pebibyte)
safe_snprintf(str, size, "%1.*fP", precision, bytes/one_pebibyte);
......@@ -420,6 +420,59 @@ char* duration_to_vstr(double value, char* str, size_t size)
return str;
}
/* Convert a duration estimate (in seconds) to a string
* with a single letter multiplier/suffix:
* (Y)ears, (W)eeks, (D)ays, (H)ours, (M)inutes, or (S)econds
*/
char* duration_estimate_to_str(double value, char* str, size_t size, double unit, int precision)
{
if(value >= one_year)
safe_snprintf(str, size, "%1.*gY", precision, value/one_year);
else if(value >= one_week || unit == one_week)
safe_snprintf(str, size, "%1.*gW", precision, value/one_week);
else if(value >= one_day || unit == one_day)
safe_snprintf(str, size, "%1.*gD", precision, value/one_day);
else if(value >= one_hour || unit == one_hour)
safe_snprintf(str, size, "%1.*gH", precision, value/one_hour);
else if(value >= one_minute || unit == one_minute)
safe_snprintf(str, size, "%1.*gM", precision, value/one_minute);
else
safe_snprintf(str, size, "%gS",value);
return str;
}
/* Convert a duration estimate (in seconds) to a verbose string
* with a word clarifier / modifier:
* year[s], week[s], day[s], hour[s], minute[s] or second[s]
*/
char* duration_estimate_to_vstr(double value, char* str, size_t size, double unit, int precision)
{
if(value >= one_year) {
value /= one_year;
safe_snprintf(str, size, "%1.*g year%s", precision, value, value == 1 ? "":"s");
}
else if(value >= one_week || unit == one_week) {
value /= one_week;
safe_snprintf(str, size, "%1.*g week%s", precision, value, value == 1 ? "":"s");
}
else if(value >= one_day || unit == one_day) {
value /= one_day;
safe_snprintf(str, size, "%1.*g day%s", precision, value, value == 1 ? "":"s");
}
else if(value >= one_hour || unit == one_hour) {
value /= one_hour;
safe_snprintf(str, size, "%1.*g hour%s", precision, value, value == 1 ? "":"s");
}
else if(value >= one_minute || unit == one_minute) {
value /= one_minute;
safe_snprintf(str, size, "%1.*g minute%s", precision, value, value == 1 ? "":"s");
}
else
safe_snprintf(str, size, "%g second%s", value, value == 1 ? "":"s");
return str;
}
/****************************************************************************/
/* Convert ASCIIZ string to upper case */
......
......@@ -377,12 +377,14 @@ DLLEXPORT char c_unescape_char_ptr(const char* str, char** endptr);
DLLEXPORT char c_unescape_char(char ch);
/* Power-of-2 byte count string parser (e.g. "100K" returns 102400 if unit is 1) */
DLLEXPORT int64_t parse_byte_count(const char*, ulong unit);
DLLEXPORT int64_t parse_byte_count(const char*, uint64_t unit);
DLLEXPORT double parse_duration(const char*);
DLLEXPORT char* duration_to_str(double value, char* str, size_t size);
DLLEXPORT char* duration_to_vstr(double value, char* str, size_t size);
DLLEXPORT char* duration_estimate_to_str(double value, char* str, size_t size, double unit, int precision);
DLLEXPORT char* duration_estimate_to_vstr(double value, char* str, size_t size, double unit, int precision);
DLLEXPORT char* byte_count_to_str(uint64_t bytes, char* str, size_t size);
DLLEXPORT char* byte_estimate_to_str(uint64_t bytes, char* str, size_t size, ulong unit, int precision);
DLLEXPORT char* byte_estimate_to_str(uint64_t bytes, char* str, size_t size, uint64_t unit, int precision);
/* Microsoft (e.g. DOS/Win32) real-time system clock API (ticks since process started) */
typedef clock_t msclock_t;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment