qt5base-lts/tests/auto/corelib
Jon Severinsson 53e6cb3ff6 Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).

[1] http://www.tondering.dk/claus/cal/julperiod.php

While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.

Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).

In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.

int main(int, char *[])
{
    int year, month, day;
    qint64 jd;
    for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
        QDate::fromJulianDay(i).getDate(&year, &month, &day);
        jd = QDate(year, month, day).toJulianDay();
        qDebug() << jd << year << month << day;
    }
}

Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-11-02 12:16:49 +01:00
..
animation normalise signal/slot signatures [QtCore tests] 2012-10-22 03:21:01 +02:00
codecs Change copyrights from Nokia to Digia 2012-09-22 19:20:11 +02:00
global QFlags: use unsigned int when the enum's underlying is unsigned 2012-09-28 23:50:10 +02:00
io test: Remove QSKIP from tst_QSettings::dontReorderIniKeysNeedlessly() 2012-10-31 19:56:51 +01:00
itemmodels normalise signal/slot signatures [QtCore tests] 2012-10-22 03:21:01 +02:00
json test: Rename test case name TestQtJson -> tst_QtJson 2012-10-24 01:39:02 +02:00
kernel move QSystemSemaphore autotest from qtscript to qtbase 2012-11-01 15:59:58 +01:00
mimetypes test: Moving tst_QMimeDatabase::inheritsPerformance() test 2012-10-31 19:56:42 +01:00
plugin Change copyrights from Nokia to Digia 2012-09-22 19:20:11 +02:00
statemachine normalise signal/slot signatures [QtCore tests] 2012-10-22 03:21:01 +02:00
thread Fix for leak in QFuture 2012-11-01 16:09:29 +01:00
tools Fix the gregorian date <-> julian day calculations in QDate 2012-11-02 12:16:49 +01:00
xml Remove duplicate words and punctuation from documentation. 2012-09-26 04:03:20 +02:00
corelib.pro Import QMimeType / QMimeDatabase into QtCore. 2012-02-18 22:19:43 +01:00