{"id":2013,"date":"2014-01-22T07:00:00","date_gmt":"2014-01-22T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/01\/22\/what-clock-do-msg-time-and-getmessagetime-use\/"},"modified":"2014-01-22T07:00:00","modified_gmt":"2014-01-22T07:00:00","slug":"what-clock-do-msg-time-and-getmessagetime-use","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140122-00\/?p=2013","title":{"rendered":"What clock do MSG.time and GetMessageTime use?"},"content":{"rendered":"<p>\nThe <code>MSG<\/code> structure has a field called <code>time<\/code>\nwhich is a <code>DWORD<\/code>.\nThere is also a function\n<code>Get&shy;Message&shy;Time<\/code> which returns a <code>LONG<\/code>.\nBoth are documented as returning the time the message was generated,\nbut the types are different.\nAre these time units comparable?\n<\/p>\n<p>\nYes, they are the same thing.\nThey all use the 32-bit timer provided by the\n<code>Get&shy;Tick&shy;Count<\/code> function.\nSorry about the inconsistency in signed\/unsigned-ness.\nFeel free to cast between them; they are fundamentally the same thing.\n<\/p>\n<p>\nWhether you prefer the signed or unsigned version depends on what\nyou intend to do with the calculation,\nspecifically, how you want to treat the case where the events\noccurred out of the expected order.\n<\/p>\n<p>\nIf you want to see whether some amount of time has elapsed,\nthen you probably want to cast the return value of\n<code>Get&shy;Message&shy;Time<\/code> to <code>DWORD<\/code>:\n<\/p>\n<pre>\nif ((DWORD)GetMessageTime() - dwStartTime &lt; VALUE) {\n ...\n}\n<\/pre>\n<p>\nThe unsigned test checks that the message time is\n<i>on or after<\/i>\nthe start time, but not more than <code>VALUE<\/code> milliseconds\nafter it.\nIf the message time comes <i>before<\/i> the start time,\nthen the test fails.\n<\/p>\n<p>\nOn the other hand, if you want to see which of two events occurred\nearlier,\nthen you want to use a signed subtraction:\n<\/p>\n<pre>\nif (GetMessageTime() - (LONG)dwStartTime &lt; VALUE) {\n ...\n}\n<\/pre>\n<p>\nThe signed test checks that the message time is\n<i>no later than<\/i> <code>VALUE<\/code> milliseconds after\nthe start time.\n<\/p>\n<p>\nNote that language lawyers may note that the above calculation\nrelies on two&#8217;s-complement overflow,\nand signed arithmetic overflow is specifically called out\nby the C language as <i>undefined behavior<\/i>.\nTherefore, a stricter language-compliant\nversion of the above test would be\n<\/p>\n<pre>\nif ((LONG)((DWORD)GetMessageTime() - dwStartTime) &lt; VALUE) {\n ...\n}\n<\/pre>\n<p>\nThe above version still relies on twos-complement arithmetic,\nbut the Win32 ABI requires twos-complement arithmetic, so we&#8217;re\nsafe there at least.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The MSG structure has a field called time which is a DWORD. There is also a function Get&shy;Message&shy;Time which returns a LONG. Both are documented as returning the time the message was generated, but the types are different. Are these time units comparable? Yes, they are the same thing. They all use the 32-bit timer [&hellip;]<\/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-2013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The MSG structure has a field called time which is a DWORD. There is also a function Get&shy;Message&shy;Time which returns a LONG. Both are documented as returning the time the message was generated, but the types are different. Are these time units comparable? Yes, they are the same thing. They all use the 32-bit timer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2013","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=2013"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2013\/revisions"}],"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=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}