{"id":106706,"date":"2022-06-02T07:00:00","date_gmt":"2022-06-02T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106706"},"modified":"2025-07-11T11:35:08","modified_gmt":"2025-07-11T18:35:08","slug":"20220602-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220602-00\/?p=106706","title":{"rendered":"Converting between Windows <CODE>FILETIME<\/CODE> and Unix <CODE>time_t<\/CODE> without having to type the magic number 116444736000000000"},"content":{"rendered":"<p>Windows tracks time in 100ns units <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20090306-00\/?p=18913\"> since January 1, 1601<\/a>. Unix tracks time in 1s units <a href=\"http:\/\/en.cppreference.com\/w\/c\/chrono\/time_t\"> since January 1, 1970<\/a>. Is there an easy way to convert between them?<\/p>\n<p>The Windows documentation offers <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/sysinfo\/converting-a-time-t-value-to-a-file-time\"> a helper function<\/a> to perform the conversion from <code>time_t<\/code> to <code>FILETIME<\/code>: It converts the units from seconds to 100ns by multiplying against the magic number 10000000, and then adds the second magic number 116444736000000000.<\/p>\n<p>Is there a way to do the conversion without having to hard-code these magic numbers? Maybe somebody else has written a conversion that we can use?<\/p>\n<p>Well, here&#8217;s one place: C++\/WinRT.<\/p>\n<p>The <code>winrt::clock<\/code> class represents the Windows Runtime <code>DateTime<\/code> clock, and also provides a number of helpers to convert to and from other formats. The Windows Runtime <code>DateTime<\/code> has the same internal format as a <code>FILETIME<\/code>, so you can treat them as basically the same thing, just in different wrapping. And since C++\/WinRT represents the Windows Runtime <code>DateTime<\/code> as a C++ <code>std::<wbr \/>chrono::<wbr \/>time_point<\/code> object, you have all of the C++ standard library facilities available.<\/p>\n<pre>\/\/ from Unix time to FILETIME\r\nauto datetime = winrt::clock::from_time_t(unix_time_seconds);\r\nFILETIME filetime = winrt::clock::to_file_time(datetime);\r\n\r\n\/\/ or combined into one line\r\nFILETIME filetime = winrt::clock::to_file_time(\r\n               winrt::clock::from_time_t(unix_time_seconds));\r\n<\/pre>\n<p>And you can just run everything in reverse to go the other way.<\/p>\n<pre>\/\/ from FILETIME to Unix time\r\nauto datetime = winrt::clock::from_file_time(filetime);\r\ntime_t unix_time_seconds = winrt::clock::to_time_t(datetime);\r\n\r\n\/\/ or combined into one line\r\ntime_t unix_time_seconds = winrt::clock::to_time_t(\r\n                     winrt::clock::from_file_time(filetime));\r\n<\/pre>\n<p>Of course, once you reach the <code>std::<wbr \/>chrono::<wbr \/>time_point<\/code>, you can stop and enjoy the scenery before moving onward to your final destination.<\/p>\n<pre>auto datetime = winrt::clock::from_file_time(filetime);\r\n\r\n\/\/ move forward 3 minutes\r\ndatetime += 3min;\r\n\r\ntime_t unix_time_seconds = winrt::clock::to_time_t(datetime);\r\n<\/pre>\n<p>Unix time is represented in the C++ standard library as a <code>std::<wbr \/>chrono::<wbr \/>system_clock<\/code>, so you can convert your Unix timestamps into a <code>sys_time&lt;Duration&gt;<\/code> (or use one of the pre-made types like <code>sys_seconds<\/code>), and then do your work in the world of C++ <code>std::<wbr \/>chrono<\/code> before converting at the last moment to a Windows <code>FILETIME<\/code>.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse; text-align: center;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td><code>time_t<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u21f5<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px currentcolor;\"><code>sys_time<\/code> = <code>time_point&lt;system_clock&gt;<\/code><br \/>\n\u21f5<br \/>\n<code>DateTime<\/code> = <code>time_point&lt;winrt::clock&gt;<\/code><\/td>\n<td style=\"padding-left: 1em;\"><code>std::chrono::time_point<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u21f5<\/td>\n<\/tr>\n<tr>\n<td><code>FILETIME<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To get in and out of the box through the top:<\/p>\n<pre>\/\/ time_t to sys_seconds\r\nauto n_seconds = std::chrono::sys_seconds(std::chrono::seconds(N));\r\n\r\n\/\/ sys_seconds to time_t\r\nauto unix_ticks = seconds.time_since_epoch().count();\r\n<\/pre>\n<p>To convert between <code>sys_time<\/code> and <code>winrt::clock<\/code>:<\/p>\n<pre>auto winrt = winrt::clock::from_sys(sys);\r\nauto sys = winrt::clock::to_sys(winrt);\r\n<\/pre>\n<p>And to get in and out through the bottom, use the <code>to_file_time<\/code> and <code>from_file_time<\/code> methods, as noted earlier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not exactly a number that you can rattle off the top of your head.<\/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-106706","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s not exactly a number that you can rattle off the top of your head.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106706","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=106706"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106706\/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=106706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}