c++ - QDateTime 到 FILETIME

标签 c++ qt winapi

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

这是 MSDN 对 FILETIME 的定义:

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

最佳答案

我做了一个函数来做到这一点,我已经测试过并且它有效:

// 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;
}

关于c++ - QDateTime 到 FILETIME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19704817/

相关文章:

C++语言模板题

c++ - 我应该在我的 C++ WIn32 应用程序中重写 operators new/delete

qt - QML 交互式控制台小部件

oop - 这种设计模式的名称?

windows - Windows 到底是什么版本?

c++ - 当完整对象被替换时,基类子对象的"Transparent replacement"?

c++ - 如何初始化 std::vector 数组?

c++ - 用于 HTML 图像标签的 QRegExp

winapi - 在 Windows 7 上禁用 Ctrl+Alt+Del

c++ - 在 Cmd 窗口之间切换焦点,强制焦点(保持程序运行的 1 个瞬间)