{"id":2763,"date":"2013-11-01T07:00:00","date_gmt":"2013-11-01T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/11\/01\/on-the-various-ways-of-getting-the-current-time-and-date-in-win32\/"},"modified":"2013-11-01T07:00:00","modified_gmt":"2013-11-01T07:00:00","slug":"on-the-various-ways-of-getting-the-current-time-and-date-in-win32","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131101-00\/?p=2763","title":{"rendered":"On the various ways of getting the current time and date in Win32"},"content":{"rendered":"<p>\nThere are a number of functions in Win32\nthat obtain the current date and time.\nHere&#8217;s how they fit together:\n<\/p>\n<p>\nThe starting point is\n<code>Get&shy;System&shy;Time&shy;As&shy;File&shy;Time<\/code>.\nThis returns the current time in UTC in the form of a\n<code>FILE&shy;TIME<\/code> structure.\nThis also happens to be the time format used internally by the system,\nso this value can be retrieved with a minimum of fuss.\n<\/p>\n<p>\nYou can also call\n<code>Get&shy;System&shy;Time<\/code> which returns the current UTC time\nin the form of a\n<code>SYSTEM&shy;TIME<\/code> structure.\nTo do this, the operating system takes the\ncurrent <code>FILE&shy;TIME<\/code> and then calls\nthe moral equivalent of\n<code>File&shy;Time&shy;To&shy;System&shy;Time<\/code>,\nwhich\ndoes a boatload of gnarly math\nto decompose the\n<code>FILE&shy;TIME<\/code>\ninto year, month, day, hour, minute, second, and millisecond.\n<\/p>\n<p>\nMeanwhile, you can also get the current local time by taking\nthe\n<code>FILE&shy;TIME<\/code> returned by\n<code>Get&shy;System&shy;Time&shy;As&shy;File&shy;Time<\/code>,\nthen passing it to\n<code>File&shy;Time&shy;To&shy;Local&shy;File&shy;Time<\/code>.\n<\/p>\n<p>\nAnd finally, there&#8217;s\n<code>Get&shy;Local&shy;Time<\/code>,\nwhich does the same thing\nas\n<code>Get&shy;System&shy;Time<\/code>,\nbut it starts with the local file time.\n<\/p>\n<p>\nIn equations:\n<\/p>\n<table BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"0\" STYLE=\"border-collapse: collapse\">\n<tr>\n<th STYLE=\"border: solid 1px black\">Format<\/th>\n<th STYLE=\"border: solid 1px black\">Time zone<\/th>\n<th STYLE=\"border: solid 1px black\">Function<\/th>\n<th STYLE=\"border: solid 1px black\" COLSPAN=\"2\">Algorithm<\/th>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">FILE&shy;TIME<\/td>\n<td STYLE=\"border: solid 1px black\">UTC<\/td>\n<td STYLE=\"border: solid 1px black\">Get&shy;System&shy;Time&shy;As&shy;File&shy;Time<\/td>\n<td STYLE=\"border: solid 1px black\" COLSPAN=\"2\">(Native format)<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">FILE&shy;TIME<\/td>\n<td STYLE=\"border: solid 1px black\">Local<\/td>\n<td STYLE=\"border: solid 1px black\">(None)<\/td>\n<td STYLE=\"border: solid 1px black\" COLSPAN=\"2\">Get&shy;System&shy;Time&shy;As&shy;File&shy;Time\n    + File&shy;Time&shy;To&shy;Local&shy;File&shy;Time<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">SYSTEM&shy;TIME<\/td>\n<td STYLE=\"border: solid 1px black\">UTC<\/td>\n<td STYLE=\"border: solid 1px black\">Get&shy;System&shy;Time&nbsp;<\/td>\n<td STYLE=\"border: solid 1px black;border-right: none\">Get&shy;System&shy;Time&shy;As&shy;File&shy;Time<\/td>\n<td STYLE=\"border: solid 1px black;border-left: none\">+ File&shy;Time&shy;To&shy;System&shy;Time<\/span><\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">SYSTEM&shy;TIME<\/td>\n<td STYLE=\"border: solid 1px black\">Local<\/td>\n<td STYLE=\"border: solid 1px black\">Get&shy;Local&shy;Time&nbsp;<\/td>\n<td STYLE=\"border: solid 1px black;border-right: none\">Get&shy;System&shy;Time&shy;As&shy;File&shy;Time\n      + File&shy;Time&shy;To&shy;Local&shy;File&shy;Time<\/td>\n<td STYLE=\"border: solid 1px black;border-left: none\">+ File&shy;Time&shy;To&shy;System&shy;Time<\/span><\/td>\n<\/tr>\n<\/table>\n<p>\nI happen to be a fan of commutative diagrams.\n(Though since there are no closed loops, there is nothing to commute.)\n<\/p>\n<div STYLE=\"height: 300px;width: 600px;font-size: 15px;text-align: center\">\n<div STYLE=\"width: 0px;height: 0px;overflow: hidden\">\n  A 2-by-2 grid of boxes.\n  The top row is labeled FILE&shy;TIME; the bottom row is labeled\n  SYSTEM&shy;TIME.\n  The first column is labeled UTC; the second column is labeled Local.\n  The upper left box\n  is labeled Get&shy;System&shy;Time&shy;As&shy;File&shy;Time.\n  There is an outgoing arrow to the right\n  labeled\n  File&shy;Time&shy;To&shy;Local&shy;File&shy;Time leading to the box in the\n  second column labeled None.\n  There is an outgoing arrow downward labeled\n  File&shy;Time&shy;To&shy;System&shy;Time\n  leading to the box in the second row, first column,\n  labeled Get&shy;System&shy;Time.\n  From the box in the upper right corner labeled None,\n  there is an outgoing arrow\n  downward labeled\n  File&shy;Time&shy;To&shy;System&shy;Time\n  leading to the box in the second row, second column,\n  labeled Get&shy;Local&shy;Time.\n<\/div>\n<div STYLE=\"height: 30px;width: 130px\">UTC<\/div>\n<div STYLE=\"height: 30px;width: 130px\">Local<\/div>\n<div STYLE=\"height: 30px;width: 100px;text-align: right\">File&shy;Time<\/div>\n<div STYLE=\"padding-top: 10px;width: 130px;height: 45px;border: solid black 1px\">\n   Get&shy;System&shy;Time&shy;As&shy;File&shy;Time<\/div>\n<div STYLE=\"text-align: center;width: 150px;height: 45px;font-size: 12px\">\n   File&shy;Time&shy;To&shy;Local&shy;File&shy;Time<\/div>\n<div STYLE=\"padding-top: 10px;width: 130px;height: 45px;border: solid black 1px\">(None)<\/div>\n<div STYLE=\"text-align: left;width: 100px;height: 45px;font-size: 12px\">\n   File&shy;Time&shy;To&shy;System&shy;Time<\/div>\n<div STYLE=\"text-align: right;width: 100px;height: 45px;font-size: 12px\">\n   File&shy;Time&shy;To&shy;System&shy;Time<\/div>\n<div STYLE=\"height: 30px;width: 100px;text-align: right\">SYSTEM&shy;TIME<\/div>\n<div STYLE=\"padding-top: 10px;width: 130px;height: 45px;border: solid black 1px\">\n   Get&shy;System&shy;Time<\/div>\n<div STYLE=\"padding-top: 10px;width: 130px;height: 45px;border: solid black 1px\">\n   Get&shy;Local&shy;Time<\/div>\n<div>\n<div STYLE=\"width: 150px;text-align: center\">&rarr;<\/div>\n<div>&darr;<\/div>\n<div>&darr;<\/div>\n<\/p><\/div>\n<\/div>\n<p>\nTo complete the commutative diagram,\nthere would be an arrow connecting the bottom two boxes called\n<code>System&shy;Time&shy;To&shy;Local&shy;Time<\/code>,\nbut there is no such function.\n<\/p>\n<p>\nToday&#8217;s article was inspired by some code I ran across which did this:\n<\/p>\n<pre>\nSYSTEMTIME stNow;\nFILETIME ftNow;\nGetSystemTime(&amp;stNow);\nSystemTimeToFileTime(&amp;stNow, &amp;ftNow);\n<\/pre>\n<p>\nThat code unwittingly takes an excursion from\n<code>Get&shy;System&shy;Time&shy;As&shy;File&shy;Time<\/code>\nthrough\n<code>File&shy;Time&shy;To&shy;System&shy;Time<\/code> to\n<code>Get&shy;System&shy;Time<\/code>,\nthen back through\n<code>System&shy;Time&shy;To&shy;File&shy;Time&amp;shy<\/code> to\nreturn to\n<code>Get&shy;System&shy;Time&shy;As&shy;File&shy;Time<\/code>,\njust so that it can end up where it started,\nbut with a lot of extra math (and loss of resolution).\n<\/p>\n<p>\n<b>Exercise<\/b>: How would you implement the\n<code>System&shy;Time&shy;To&shy;Local&shy;Time<\/code>\nfunction?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a number of functions in Win32 that obtain the current date and time. Here&#8217;s how they fit together: The starting point is Get&shy;System&shy;Time&shy;As&shy;File&shy;Time. This returns the current time in UTC in the form of a FILE&shy;TIME structure. This also happens to be the time format used internally by the system, so this value [&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,108],"class_list":["post-2763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code","tag-time"],"acf":[],"blog_post_summary":"<p>There are a number of functions in Win32 that obtain the current date and time. Here&#8217;s how they fit together: The starting point is Get&shy;System&shy;Time&shy;As&shy;File&shy;Time. This returns the current time in UTC in the form of a FILE&shy;TIME structure. This also happens to be the time format used internally by the system, so this value [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2763","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=2763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2763\/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=2763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}