mirror-chatterino2/src/util/FormatTime.cpp

54 lines
1.1 KiB
C++
Raw Normal View History

2018-08-07 01:35:24 +02:00
#include "FormatTime.hpp"
namespace chatterino {
namespace {
2018-08-15 22:46:20 +02:00
void appendDuration(int count, QChar &&order, QString &outString)
{
outString.append(QString::number(count));
outString.append(order);
}
2018-08-07 01:35:24 +02:00
} // namespace
QString formatTime(int totalSeconds)
{
QString res;
int seconds = totalSeconds % 60;
int timeoutMinutes = totalSeconds / 60;
int minutes = timeoutMinutes % 60;
int timeoutHours = timeoutMinutes / 60;
int hours = timeoutHours % 24;
int days = timeoutHours / 24;
2018-10-21 13:43:02 +02:00
if (days > 0)
{
2018-08-07 01:35:24 +02:00
appendDuration(days, 'd', res);
}
2018-10-21 13:43:02 +02:00
if (hours > 0)
{
if (!res.isEmpty())
{
2018-08-07 01:35:24 +02:00
res.append(" ");
}
appendDuration(hours, 'h', res);
}
2018-10-21 13:43:02 +02:00
if (minutes > 0)
{
if (!res.isEmpty())
{
2018-08-07 01:35:24 +02:00
res.append(" ");
}
appendDuration(minutes, 'm', res);
}
2018-10-21 13:43:02 +02:00
if (seconds > 0)
{
if (!res.isEmpty())
{
2018-08-07 01:35:24 +02:00
res.append(" ");
}
appendDuration(seconds, 's', res);
}
return res;
}
} // namespace chatterino