{"id":112726,"date":"2026-09-23T07:00:00","date_gmt":"2026-09-23T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112726"},"modified":"2026-09-23T19:12:36","modified_gmt":"2026-09-24T02:12:36","slug":"20260923-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260923-00\/?p=112726\/","title":{"rendered":"When working with Win32 <CODE>FILETIME<\/CODE>, don&#8217;t forget that you have <CODE>std::chrono<\/CODE> now"},"content":{"rendered":"<p>Some time ago, I was looking at a pull request, and saw that the code was manually calculating the &#8220;number of minutes since the last change&#8221; by doing annoying math.<\/p>\n<pre>static const DWORD c_TicksPerSecond = 10000000;\r\nstatic const DWORD c_SecondsPerMinute = 60;\r\n\r\nuint64_t FileTimeToULongLong(FILETIME time)\r\n{\r\n    ULARGE_INTEGER value;\r\n\r\n    value.LowPart = time.dwLowDateTime;\r\n    value.HighPart = time.dwHighDateTime;\r\n\r\n    return value.QuadPart;\r\n}\r\nDWORD GetMinutesSinceLastChange()\r\n{\r\n    FILETIME now;\r\n    GetSystemTimeAsFileTime(&amp;now);\r\n\r\n    uint64_t now64 = FileTimeToULongLong(now);\r\n    uint64_t last64 = FileTimeToULongLong(m_lastChange);\r\n\r\n    if (now64 &lt; last64) {\r\n        return 0;\r\n    }\r\n\r\n    uint64_t diff = last64 - now64;\r\n    return static_cast&lt;DWORD&gt;(diff \/\r\n        (static_cast&lt;uint64_t&gt;(c_dwSecondsPerMinute) *\r\n         static_cast&lt;uint64_t&gt;(c_TicksPerSecond)));\r\n}\r\n<\/pre>\n<p>Okay, first of all, we have helpers in the Windows Implementation Library (wil) to save you a lot of typing and calculating weird constants.<\/p>\n<pre>DWORD GetMinutesSinceLastChange()\r\n{\r\n    FILETIME now;\r\n    GetSystemTimeAsFileTime(&amp;now);\r\n\r\n    int64_t now64 = wil::filetime::to_int64(now);\r\n    int64_t last64 = wil::filetime::to_int64(m_lastChange);\r\n\r\n    if (now64 &lt; last64) {\r\n        return 0;\r\n    }\r\n\r\n    int64_t diff = last64 - now64;\r\n    return static_cast&lt;DWORD&gt;(diff \/ wil::filetime_duration::one_minute);\r\n}\r\n<\/pre>\n<p>But even better: You have <code>std::chrono<\/code> now. C++\/WinRT has already written the weird constants for you, and the C++ standard library has all the convenient helper functions.<\/p>\n<pre>DWORD GetMinutesSinceLastChange()\r\n{\r\n    auto last = winrt::clock::from_FILETIME(m_lastChange);\r\n    auto now = (std::max)(winrt::clock::now(), last);\r\n    return (now - last) \/ 1min;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let the library do the math for you.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-112726","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Let the library do the math for you.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=112726"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112726\/revisions"}],"predecessor-version":[{"id":112727,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112726\/revisions\/112727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=112726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}