Hello Guys
I was trying to create a function to convert seconds and bytes to more "human readable".
If I'm right, the file that shows information in bconsole is src/cats/sql.c, function list_result.
How the function list_result didn't check the name of fields, I guess is necessary to check the name of fields to implement this features, but I don't know where can I start. Any tips?
if ( (strcasecmp(field->name, "VolRetention") == 0 ) ) {
...
}
###########################################################################################
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
char* seconds_to_humanreadable(float seconds, char *buf);
int main (int argc, char *argv[]){
char buf[32];
float seconds;
int i = 1;
while( i < argc){
seconds = atof(argv[i]);
printf("%s\n", seconds_to_humanread( seconds, buf) );
i++;
}
} /* end main */
char* seconds_to_humanreadable(float seconds, char *buf) {
if (seconds < 60) {
sprintf(buf, "%.2f seconds", seconds);
}
else if (seconds >= 60 && seconds < 3600){
sprintf(buf, "%.2f minutes", seconds/60);
}
else if (seconds >= 3600 && seconds < 86400){
sprintf(buf, "%.2f hours", seconds/3600);
}
else if( seconds >= 86400 && seconds < 2628000){
sprintf(buf, "%.2f days", seconds/86400);
}
else if( seconds >= 2628000 && seconds <= 31536000){
sprintf(buf, "%.2f months", seconds/2628000);
}
else if(seconds > 31536000){
sprintf(buf, "%.2f years", seconds/31536000);
}
else{
sprintf(buf, "0");
}
return buf;
} /*end function */
###########################################################################################
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
char* bytes_to_humanreadable(float bytes, char *buf);
int main (int argc, char *argv[]){
char buf[32];
float bytes;
int i = 1;
while( i < argc){
bytes = atof(argv[i]);
printf("%s\n", bytes_to_humanread( bytes, buf) );
i++;
} /* end while */
} /* end main */
char* bytes_to_humanreadable(float bytes, char *buf) {
int i = 0;
const char* unit[] = {" ", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB"};
while( true ){
if ( bytes < 1024 ){
sprintf(buf, "%.2f %s", bytes, unit[i]);
return buf;
}
bytes = bytes / 1024;
i++;
} /* end while */
} /*end function */
###########################################################################################