File indexing completed on 2024-02-18 03:58:09

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1997, 2001 Stephan Kulow <coolo@kde.org>
0004     SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
0005     SPDX-FileCopyrightText: 1999-2002 Hans Petter Bieker <bieker@kde.org>
0006     SPDX-FileCopyrightText: 2002 Lukas Tinkl <lukas@kde.org>
0007     SPDX-FileCopyrightText: 2007 Bernhard Loos <nhuh.put@web.de>
0008     SPDX-FileCopyrightText: 2009, 2010 John Layt <john@layt.net>
0009 
0010     SPDX-License-Identifier: LGPL-2.0-only
0011 */
0012 
0013 #include "kjobtrackerformatters_p.h"
0014 #include <KFormat>
0015 
0016 #include <QCoreApplication>
0017 
0018 QString KJobTrackerFormatters::byteSize(double size)
0019 {
0020     return KFormat().formatByteSize(size);
0021 }
0022 
0023 QString KJobTrackerFormatters::daysDuration(int n)
0024 {
0025     //~ singular %n day
0026     //~ plural %n days
0027     return QCoreApplication::translate("KJobTrackerFormatters", "%n day(s)", "@item:intext", n);
0028 }
0029 
0030 QString KJobTrackerFormatters::hoursDuration(int n)
0031 {
0032     //~ singular %n hour
0033     //~ plural %n hours
0034     return QCoreApplication::translate("KJobTrackerFormatters", "%n hour(s)", "@item:intext", n);
0035 }
0036 
0037 QString KJobTrackerFormatters::minutesDuration(int n)
0038 {
0039     //~ singular %n minute
0040     //~ plural %n minutes
0041     return QCoreApplication::translate("KJobTrackerFormatters", "%n minute(s)", "@item:intext", n);
0042 }
0043 
0044 QString KJobTrackerFormatters::secondsDuration(int n)
0045 {
0046     //~ singular %n second
0047     //~ plural %n seconds
0048     return QCoreApplication::translate("KJobTrackerFormatters", "%n second(s)", "@item:intext", n);
0049 }
0050 
0051 QString KJobTrackerFormatters::duration(unsigned long mSec)
0052 {
0053     unsigned long ms = mSec;
0054     int days = ms / (24 * 3600000);
0055     ms = ms % (24 * 3600000);
0056     int hours = ms / 3600000;
0057     ms = ms % 3600000;
0058     int minutes = ms / 60000;
0059     ms = ms % 60000;
0060     int seconds = qRound(ms / 1000.0);
0061 
0062     // Handle correctly problematic case #1 (look at KLocaleTest::prettyFormatDuration()
0063     // at klocaletest.cpp)
0064     if (seconds == 60) {
0065         return duration(mSec - ms + 60000);
0066     }
0067 
0068     if (days && hours) {
0069         return QCoreApplication::translate("KJobTrackerFormatters", "%1 and %2", "@item:intext days and hours.").arg(daysDuration(days), hoursDuration(hours));
0070     } else if (days) {
0071         return daysDuration(days);
0072     } else if (hours && minutes) {
0073         return QCoreApplication::translate("KJobTrackerFormatters", "%1 and %2", "@item:intext hours and minutes.")
0074             .arg(hoursDuration(hours), minutesDuration(minutes));
0075     } else if (hours) {
0076         return hoursDuration(hours);
0077     } else if (minutes && seconds) {
0078         return QCoreApplication::translate("KJobTrackerFormatters", "%1 and %2", "@item:intext minutes and seconds.")
0079             .arg(minutesDuration(minutes), secondsDuration(seconds));
0080     } else if (minutes) {
0081         return minutesDuration(minutes);
0082     } else {
0083         return secondsDuration(seconds);
0084     }
0085 }