QDateTime到FILETIME

时间:2013-10-31 11:06:50

标签: c++ qt winapi

我需要将QDateTime传递给接受FILETIME的Win32函数。

这是MSDN对FILETIME的定义:

  

包含一个64位值,表示自1601年1月1日(UTC)以来100纳秒间隔的数量。

1 个答案:

答案 0 :(得分:2)

我做了一个功能,我已经测试了它并且它有效:

// Convert a QDateTime to a FILETIME.
FILETIME toWinFileTime(const QDateTime &dateTime)
{
    // Definition of FILETIME from MSDN:
    // Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
    QDateTime origin(QDate(1601, 1, 1), QTime(0, 0, 0, 0), Qt::UTC);
    // Get offset - note we need 100-nanosecond intervals, hence we multiply by
    // 10000.
    qint64 _100nanosecs = 10000 * origin.msecsTo(dateTime);
    // Pack _100nanosecs into the structure.
    FILETIME fileTime;
    fileTime.dwLowDateTime = _100nanosecs;
    fileTime.dwHighDateTime = (_100nanosecs >> 32);
    return fileTime;
}