Bug 397008

Summary: KFormat::formatDuration returns different results with ms large
Product: [Frameworks and Libraries] frameworks-kcoreaddons Reporter: Safa Alfulaij <safa1996alfulaij>
Component: generalAssignee: Michael Pyne <mpyne>
Status: RESOLVED FIXED    
Severity: normal CC: kde, kdelibs-bugs-null, nate
Priority: NOR Keywords: junior-jobs
Version First Reported In: 5.48.0   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In: 5.49.0
Sentry Crash Report:

Description Safa Alfulaij 2018-07-31 09:47:43 UTC
When you use KFormat::formatDuration with a very large duration, and you pass an option (ex. ShowMilliseconds), the returned value is different than if it was without the option.

Python example (same issue in C++ and QMl):

from PyKF5 import KCoreAddons

INT = 9999999999
mformat = KCoreAddons.KFormat()
print("KFormat.ShowMilliseconds: {}".format(mformat.formatDuration(INT, KCoreAddons.KFormat.ShowMilliseconds)))
print("No options: {}".format(mformat.formatDuration(INT)))

>> KFormat.ShowMilliseconds: 2777:46:39.999
>> No options: 391:41:05
Comment 1 Christoph Feck 2018-07-31 14:55:52 UTC
Currently uses qRound, which returns int, but either should use qRound64 (and double instead of qreal), or just use int arithmetic for rounding, e.g. ((x + 50) / 100) * 100 will round to multiple of 100.

https://cgit.kde.org/kcoreaddons.git/tree/src/lib/util/kformatprivate.cpp#n332
Comment 2 Christoph Feck 2018-08-02 21:43:24 UTC
https://phabricator.kde.org/D14565
Comment 3 Sune Vuorela 2018-08-03 16:01:45 UTC
Git commit ea2779611800e72b516f400abf18aa8ef4abfaec by Sune Vuorela.
Committed on 03/08/2018 at 16:01.
Pushed by sune into branch 'master'.

Fix overflow in rounding code

Summary:
qRound works with int.

Test Plan: add unit tests

Reviewers: cfeck, safaalfulaij

Reviewed By: cfeck, safaalfulaij

Subscribers: kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D14565

M  +11   -0    autotests/kformattest.cpp
M  +2    -2    src/lib/util/kformatprivate.cpp

https://commits.kde.org/kcoreaddons/ea2779611800e72b516f400abf18aa8ef4abfaec