Added PrintSec in Utilities

Collection of methods for printable stuff
This commit is contained in:
Valentino Orlandi 2023-01-27 02:12:52 +01:00
parent 1380d79bc1
commit 6e8aa9749f
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
2 changed files with 247 additions and 0 deletions

View File

@ -0,0 +1,161 @@
#include "printables.h"
#include "modules/shared.h"
PrintSec::PrintSec()
{
}
const QString PrintSec::printableSize( const unsigned bytes )
{
std::string size_str, size_sfx=" B";
float size = (float)bytes;
if (size > 1024) {
size /= 1024;
size_sfx = " KiB";
if (size > 1024) {
size /= 1024;
size_sfx = " MiB";
}
}
// cut decimals depending on how big the floor is
size_str = std::to_string( size );
size_t cut_index = size_str.find('.')+1;
if ( cut_index == 0 ) { // catches the overflow of +1 of the above
cut_index = size_str.find(',')+1;
}
size_t n_decimals = 3;
if ( size >= 100 ) {
n_decimals --;
if ( size >= 1000 ) {
n_decimals --;
if ( size >= 10000 ) {
n_decimals --;
cut_index --; // no decimals, no "dot"
}
}
}
if ( cut_index >= 1 ) {
cut_index += n_decimals;
if ( cut_index >= size_str.size() ) {
cut_index = size_str.size()-1;
}
}
return QString::fromStdString( size_str.substr(0,cut_index) + size_sfx );
}
const QString PrintSec::printableSpeed( const unsigned bytes, const unsigned secs_ )
{
std::string speed_str, speed_sfx=" B/s";
unsigned secs = secs_;
if ( secs == 0 ) {
secs ++;
}
float speed = (float)bytes / (float)secs;
if (speed > 1024) {
speed /= 1024;
speed_sfx = " KiB/s";
if (speed > 1024) {
speed /= 1024;
speed_sfx = " MiB/s";
}
}
// cut decimals depending on how big the floor is
speed_str = std::to_string( speed );
size_t cut_index = speed_str.find('.')+1;
if ( cut_index == 0 ) { // catches the overflow of +1 of the above
cut_index = speed_str.find(',')+1;
}
size_t n_decimals = 3;
if ( speed >= 100 ) {
n_decimals --;
if ( speed >= 1000 ) {
n_decimals --;
if ( speed >= 10000 ) {
n_decimals --;
cut_index --; // no decimals, no "dot"
}
}
}
if ( cut_index >= 1 ) {
cut_index += n_decimals;
if ( cut_index >= speed_str.size() ) {
cut_index = speed_str.size()-1;
}
}
return QString::fromStdString( speed_str.substr(0,cut_index) + speed_sfx );
}
const QString PrintSec::printableTime( const unsigned seconds )
{
const unsigned mins = seconds / 60;
const unsigned secs = seconds - (mins*60);
return QString("%1:%2").arg(
(mins<10)
? QString("0%1").arg( mins )
: QString::number( mins ),
(secs<10)
? QString("0%1").arg( secs )
: QString::number( secs )
);
}
const QString PrintSec::printableTime( const int hour, const int minute, const int second )
{
return QString("%1:%2:%3").arg(
(hour<10)
? QString("0%1").arg( hour )
: QString::number( hour ),
(minute<10)
? QString("0%1").arg( minute )
: QString::number( minute ),
(second<10)
? QString("0%1").arg( second )
: QString::number( second )
);
}
const QString PrintSec::printableDate( const QString& year, const int month, const QString& day )
{
return QString("%1-%2-%3").arg(
year,
(month<10)
? QString("0%1").arg( month )
: QString::number( month ),
(day.size()<2)
? QString("0%1").arg( day )
: day
);
}
const QString PrintSec::printableDate( const int year, const int month, const int day )
{
return QString("%1-%2-%3").arg(
QString::number( year ),
(month<10)
? QString("0%1").arg( month )
: QString::number( month ),
(day<10)
? QString("0%1").arg( day )
: QString::number( day )
);
}
const QString PrintSec::printableBool( const int value )
{
if ( value == 0 ) {
return TR::tr( BOOLS__FALSE.c_str() );
} else {
return TR::tr( BOOLS__TRUE.c_str() );
}
}

View File

@ -0,0 +1,86 @@
#ifndef PRINTSEC_H
#define PRINTSEC_H
#include <QString>
class PrintSec
{
public:
explicit PrintSec();
//! Formats the size including the suffix, for display purposes
/*!
\param bytes The size in bytes
\return The string to be displayed
*/
static const QString printableSize( const unsigned bytes );
//! Formats the speed including the suffix, for display purposes
/*!
\param bytes The size in bytes
\param seconds The number of seconds
\return The string to be displayed
*/
static const QString printableSpeed(
const unsigned bytes, const unsigned seconds
);
//! Formats the time, for display purposes
/*!
\param seconds The number of seconds
\return The string to be displayed
*/
static const QString printableTime( const unsigned seconds );
//! Returns a string of the given time in the format HH:MM:SS
/*!
\param hour The hour
\param minut The minute
\param second The second
\return The printable time
*/
static const QString printableTime(
const int hour, const int minute, const int second
);
//! Returns a string of the given date in the format YYYY-MM-DD
/*!
\overload const QString printableDate(const int& year, const int& month, const int& day)
\param year The year
\param month The month
\param day The day
\return The printable date
*/
static const QString printableDate(
const QString& year, const int month, const QString& day
);
//! Returns a string of the given date in the format YYYY-MM-DD
/*!
\param year The year (4 digits number)
\param month The month
\param day The day
\return The printable date
*/
static const QString printableDate(
const int year, const int month, const int day
);
//! Returns a string corresponding to the given value
/*!
Used to convert numeric booleans [0,1] to string [FALSE,TRUE]
\param value The value to convert
\return The corresponding printable string
*/
static const QString printableBool( const int value );
};
#endif // PRINTSEC_H