KDateTime::currentUtcDateTime()

Peter Kümmel syntheticpp at gmx.net
Sun Jul 30 14:39:39 BST 2006


David Jarvie wrote:
> I am confused as to why you mention the 
> constructor at all.

I was wrong with the ctor.

But I wonder why on windows the date is set but not under unix.
When you wanna use setTime_t only then the windows code is superfluous.
But when the date should be set on Windows and Unix then it seems
to me that the unix code is incomplete.

The Qt code looks like this:

QDateTime QDateTime::currentDateTime()
{
#if defined(Q_OS_WIN)
    QDate d;
    QTime t;
    SYSTEMTIME st;
    memset(&st, 0, sizeof(SYSTEMTIME));
    GetLocalTime(&st);
    d.jd = QDate::gregorianToJulian(st.wYear, st.wMonth, st.wDay);
    t.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond
            + st.wMilliseconds;
    return QDateTime(d, t);
#else
    QDateTime dt;
    QTime t;
    dt.setDate(QDate::currentDate());
    t = QTime::currentTime();
    if (t.ds() < MSECS_PER_MIN)                // midnight or right after?
        dt.setDate(QDate::currentDate());          // fetch date again
    dt.setTime(t);
    return dt;
#endif
}

and the kde code like this:

KDateTime KDateTime::currentUtcDateTime()
{
#ifdef Q_OS_WIN
    SYSTEMTIME st;
    memset(&st, 0, sizeof(SYSTEMTIME));
    GetSystemTime(&st);
    return KDateTime(QDate(st.wYear, st.wMonth, st.wDay),
                     QTime(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds),
                     Spec(UTC));
#else
    time_t t;
    ::time(&t);
    KDateTime result;
    result.setTime_t(static_cast<uint>(t));
    return result;
#endif
}


You've reproduced the Windows code but not the unix one:
Should there not also be a setDate(QDate::currentDate());
in the kde code?

(Initially I thought the date is set in the KDateTime unix-ctor
but not in the windows-ctor there my ctor suggestion).

Peter




More information about the kde-core-devel mailing list