2023-07-30 18:54:42 +02:00
|
|
|
#include "util/FormatTime.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
namespace chatterino {
|
2022-05-29 13:54:42 +02:00
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
namespace {
|
2022-05-29 13:54:42 +02:00
|
|
|
|
|
|
|
void appendDuration(int count, QChar &&suffix, QString &out)
|
2018-08-15 22:46:20 +02:00
|
|
|
{
|
2022-05-29 13:54:42 +02:00
|
|
|
if (!out.isEmpty())
|
|
|
|
{
|
|
|
|
out.append(' ');
|
|
|
|
}
|
|
|
|
out.append(QString::number(count));
|
|
|
|
out.append(suffix);
|
2018-08-15 22:46:20 +02:00
|
|
|
}
|
2022-05-29 13:54:42 +02:00
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
} // namespace
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
QString formatTime(int totalSeconds)
|
|
|
|
{
|
|
|
|
QString res;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
int seconds = totalSeconds % 60;
|
|
|
|
int timeoutMinutes = totalSeconds / 60;
|
|
|
|
int minutes = timeoutMinutes % 60;
|
|
|
|
int timeoutHours = timeoutMinutes / 60;
|
|
|
|
int hours = timeoutHours % 24;
|
|
|
|
int days = timeoutHours / 24;
|
|
|
|
if (days > 0)
|
|
|
|
{
|
|
|
|
appendDuration(days, 'd', res);
|
|
|
|
}
|
|
|
|
if (hours > 0)
|
|
|
|
{
|
|
|
|
appendDuration(hours, 'h', res);
|
|
|
|
}
|
|
|
|
if (minutes > 0)
|
|
|
|
{
|
|
|
|
appendDuration(minutes, 'm', res);
|
|
|
|
}
|
|
|
|
if (seconds > 0)
|
|
|
|
{
|
|
|
|
appendDuration(seconds, 's', res);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-07-18 14:12:11 +02:00
|
|
|
QString formatTime(QString totalSecondsString)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
int totalSeconds(totalSecondsString.toInt(&ok));
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
return formatTime(totalSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "n/a";
|
|
|
|
}
|
|
|
|
|
2023-07-30 18:54:42 +02:00
|
|
|
QString formatTime(std::chrono::seconds totalSeconds)
|
|
|
|
{
|
|
|
|
auto count = totalSeconds.count();
|
|
|
|
|
|
|
|
return formatTime(static_cast<int>(std::clamp(
|
|
|
|
count,
|
|
|
|
static_cast<std::chrono::seconds::rep>(std::numeric_limits<int>::min()),
|
|
|
|
static_cast<std::chrono::seconds::rep>(
|
|
|
|
std::numeric_limits<int>::max()))));
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
} // namespace chatterino
|