{"id":45161,"date":"2015-07-13T07:00:00","date_gmt":"2015-07-13T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20150713-00\/?p=45161\/"},"modified":"2019-03-13T12:17:16","modified_gmt":"2019-03-13T19:17:16","slug":"20150713-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150713-00\/?p=45161","title":{"rendered":"Generating different types of timestamps from quite a long way away"},"content":{"rendered":"<p>Today&#8217;s Little Program does the reverse of <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2015\/07\/06\/10624290.aspx\">what we had last time<\/a>. It takes a point in time and then generates timestamps in various formats. <\/p>\n<pre>\nusing System;\n\nclass Program\n{\n static void TryFormat(string format, Func&lt;long&gt; func)\n {\n  try {\n   long l = func();\n   if ((ulong)l &gt; 0x00000000FFFFFFFF) {\n       Console.WriteLine(\"{0} 0x{1:X16}\", format, l);\n   } else {\n       Console.WriteLine(\"{0} 0x{1:X08}\", format, l);\n   }\n  } catch (ArgumentException) {\n   Console.WriteLine(\"{0} - invalid\", format);\n  }\n }\n<\/pre>\n<p>Like last time, the <code>Try&shy;Format<\/code> method executes the passed-in function inside a try\/catch block. If the function executes successfully, then we print the result. There is a tiny bit of cleverness where we choose the output format depending on the number of bits in the result. <\/p>\n<pre>\n static long DosDateTimeFromDateTime(DateTime value)\n {\n  int result = ((value.Year - 1980) &lt;&lt; 25) |\n               (value.Month &lt;&lt; 21) |\n               (value.Day &lt;&lt; 16) |\n               (value.Hour &lt;&lt; 11) |\n               (value.Minute &lt;&lt; 5) |\n               (value.Second &gt;&gt; 1);\n  return (uint)result;\n }\n<\/pre>\n<p>The <code>Dos&shy;Date&shy;Time&shy;From&shy;Date&shy;Time<\/code> converts the <code>Date&shy;Time<\/code> into a 32-bit date\/time stamp in MS-DOS format. This is not quite correct because MS-DOS format date\/time stamps are in local time, but we are not converting the incoming <code>Date&shy;Time<\/code> to local time. It&#8217;s up to you to understand what&#8217;s going on. <\/p>\n<pre>\n public static void Main(string[] args)\n {\n  int[] parts = new int[7];\n  for (int i = 0; i &lt; 7; i++) {\n   parts[i] = args.Length &gt; i ? int.Parse(args[i]) : 0;\n  }\n\n  DateTime value = new DateTime(parts[0], parts[1], parts[2],\n                                parts[3], parts[4], parts[5],\n                                parts[6], DateTimeKind.Utc);\n\n  Console.WriteLine(\"Timestamp {0} UTC\", value);\n\n  TryFormat(\"Unix time\",\n    () =&gt; <a HREF=\"http:\/\/blogs.msdn.com\/b\/brada\/archive\/2003\/07\/30\/50205.aspx\">value.ToFileTimeUtc() \/ 10000000 - 11644473600<\/a>);\n  TryFormat(\"UTC FILETIME\",\n    () =&gt; value.ToFileTimeUtc());\n  TryFormat(\"Binary DateTime\",\n    () =&gt; value.ToBinary());\n  TryFormat(\"MS-DOS Date\/Time\",\n    () =&gt; DosDateTimeFromDateTime(value));\n  TryFormat(\"OLE Date\/Time\",\n    () =&gt; <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2015\/06\/22\/10623021.aspx\">BitConverter.DoubleToInt64Bits<\/a>(value.ToOADate()));\n }\n}\n<\/pre>\n<p>The parameters on the command line are the year, month, day, hour, minute, second, and millisecond; any omitted parameters are taken as zero. We create a UTC <code>Date&shy;Time<\/code> from it, and then try to convert that <code>Date&shy;Time<\/code> into the other formats. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Transforming in reverse.<\/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-45161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Transforming in reverse.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/45161","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=45161"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/45161\/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=45161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=45161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=45161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}