Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDateTime to FILETIME

Tags:

c++

qt

winapi

I need to pass a QDateTime to a Win32 function that accepts FILETIME.

This is MSDN's definition of FILETIME:

Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

like image 433
sashoalm Avatar asked Mar 03 '26 11:03

sashoalm


1 Answers

I made a function to do that, which I've tested and it works:

// 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;
}
like image 157
sashoalm Avatar answered Mar 06 '26 02:03

sashoalm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!