Commit 5d2f6c7c authored by rswindell's avatar rswindell
Browse files

Created duration_to_vstr() - like duration_to_str(), but it appends a space

then a clarifying or modifying word (year[s], month[s], day[s], etc.).
parse_byte_count() and parse_duration() now support a space between the
numeric value (decimal digits) and the modifier. Hopefully this doesn't break
anything, but it makes parse_duration() compatible with the strings generated
with duration_to_vstr(). We might want a verbose version of byte_count_to_str()
in the future, so I figured it made sense to go ahead and add the white-space
skipping/parsing to prase_byte_count() now.
parent d63e0271
......@@ -235,6 +235,7 @@ int64_t DLLCALL parse_byte_count(const char* str, ulong unit)
bytes=strtod(str,&p);
if(p!=NULL) {
SKIP_WHITESPACE(p);
switch(toupper(*p)) {
case 'E':
bytes*=1024;
......@@ -290,6 +291,7 @@ double DLLCALL parse_duration(const char* str)
t=strtod(str,&p);
if(p!=NULL) {
SKIP_WHITESPACE(p);
switch(toupper(*p)) {
case 'Y': t*=365.0*24.0*60.0*60.0; break;
case 'W': t*= 7.0*24.0*60.0*60.0; break;
......@@ -303,7 +305,7 @@ double DLLCALL parse_duration(const char* str)
/* Convert a duration (in seconds) to a string
* with a single letter multiplier/suffix:
* (Y)ears, (W)eeks, (D)ays, (H)ours, and (M)inutes
* (Y)ears, (W)eeks, (D)ays, (H)ours, (M)inutes, or (S)econds
*/
char* DLLCALL duration_to_str(double value, char* str, size_t size)
{
......@@ -323,6 +325,39 @@ char* DLLCALL duration_to_str(double value, char* str, size_t size)
return str;
}
/* Convert a duration (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* DLLCALL duration_to_vstr(double value, char* str, size_t size)
{
if(fmod(value,365.0*24.0*60.0*60.0)==0) {
value /= (365.0*24.0*60.0*60.0);
safe_snprintf(str, size, "%g year%s", value, value > 1 ? "s":"");
}
else if(fmod(value,7.0*24.0*60.0*60.0)==0) {
value /= (7.0*24.0*60.0*60.0);
safe_snprintf(str, size, "%g week%s", value, value > 1 ? "s":"");
}
else if(fmod(value,24.0*60.0*60.0)==0) {
value /= (24.0*60.0*60.0);
safe_snprintf(str, size, "%g day%s", value, value > 1 ? "s":"");
}
else if(fmod(value,60.0*60.0)==0) {
value /= (60.0*60.0);
safe_snprintf(str, size, "%g hour%s", value, value > 1 ? "s":"");
}
else if(fmod(value,60.0)==0) {
value /= 60.0;
safe_snprintf(str, size, "%g minute%s", value, value > 1 ? "s":"");
}
else
safe_snprintf(str, size, "%g second%s", value, value > 1 ? "s":"");
return str;
}
/****************************************************************************/
/* Convert ASCIIZ string to upper case */
/****************************************************************************/
......
......@@ -370,6 +370,7 @@ DLLEXPORT char DLLCALL c_unescape_char(char ch);
DLLEXPORT int64_t DLLCALL parse_byte_count(const char*, ulong unit);
DLLEXPORT double DLLCALL parse_duration(const char*);
DLLEXPORT char* DLLCALL duration_to_str(double value, char* str, size_t size);
DLLEXPORT char* DLLCALL duration_to_vstr(double value, char* str, size_t size);
DLLEXPORT char* DLLCALL byte_count_to_str(int64_t bytes, char* str, size_t size);
/* Microsoft (e.g. DOS/Win32) real-time system clock API (ticks since process started) */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment