1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 1997, 2001 Stephan Kulow <coolo@kde.org>
4 SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
5 SPDX-FileCopyrightText: 1999-2002 Hans Petter Bieker <bieker@kde.org>
6 SPDX-FileCopyrightText: 2002 Lukas Tinkl <lukas@kde.org>
7 SPDX-FileCopyrightText: 2007 Bernhard Loos <nhuh.put@web.de>
8 SPDX-FileCopyrightText: 2009, 2010 John Layt <john@layt.net>
9
10 SPDX-License-Identifier: LGPL-2.0-only
11*/
12
13#include "kjobtrackerformatters_p.h"
14#include <KFormat>
15
16#include <QCoreApplication>
17
18QString KJobTrackerFormatters::byteSize(double size)
19{
20 return KFormat().formatByteSize(size);
21}
22
23QString KJobTrackerFormatters::daysDuration(int n)
24{
25 //~ singular %n day
26 //~ plural %n days
27 return QCoreApplication::translate(context: "KJobTrackerFormatters", key: "%n day(s)", disambiguation: "@item:intext", n);
28}
29
30QString KJobTrackerFormatters::hoursDuration(int n)
31{
32 //~ singular %n hour
33 //~ plural %n hours
34 return QCoreApplication::translate(context: "KJobTrackerFormatters", key: "%n hour(s)", disambiguation: "@item:intext", n);
35}
36
37QString KJobTrackerFormatters::minutesDuration(int n)
38{
39 //~ singular %n minute
40 //~ plural %n minutes
41 return QCoreApplication::translate(context: "KJobTrackerFormatters", key: "%n minute(s)", disambiguation: "@item:intext", n);
42}
43
44QString KJobTrackerFormatters::secondsDuration(int n)
45{
46 //~ singular %n second
47 //~ plural %n seconds
48 return QCoreApplication::translate(context: "KJobTrackerFormatters", key: "%n second(s)", disambiguation: "@item:intext", n);
49}
50
51QString KJobTrackerFormatters::duration(unsigned long mSec)
52{
53 unsigned long ms = mSec;
54 int days = ms / (24 * 3600000);
55 ms = ms % (24 * 3600000);
56 int hours = ms / 3600000;
57 ms = ms % 3600000;
58 int minutes = ms / 60000;
59 ms = ms % 60000;
60 int seconds = qRound(d: ms / 1000.0);
61
62 // Handle correctly problematic case #1 (look at KLocaleTest::prettyFormatDuration()
63 // at klocaletest.cpp)
64 if (seconds == 60) {
65 return duration(mSec: mSec - ms + 60000);
66 }
67
68 if (days && hours) {
69 return QCoreApplication::translate(context: "KJobTrackerFormatters", key: "%1 and %2", disambiguation: "@item:intext days and hours.").arg(args: daysDuration(n: days), args: hoursDuration(n: hours));
70 } else if (days) {
71 return daysDuration(n: days);
72 } else if (hours && minutes) {
73 return QCoreApplication::translate(context: "KJobTrackerFormatters", key: "%1 and %2", disambiguation: "@item:intext hours and minutes.")
74 .arg(args: hoursDuration(n: hours), args: minutesDuration(n: minutes));
75 } else if (hours) {
76 return hoursDuration(n: hours);
77 } else if (minutes && seconds) {
78 return QCoreApplication::translate(context: "KJobTrackerFormatters", key: "%1 and %2", disambiguation: "@item:intext minutes and seconds.")
79 .arg(args: minutesDuration(n: minutes), args: secondsDuration(n: seconds));
80 } else if (minutes) {
81 return minutesDuration(n: minutes);
82 } else {
83 return secondsDuration(n: seconds);
84 }
85}
86

source code of kjobwidgets/src/kjobtrackerformatters.cpp