{"id":35893,"date":"2005-04-15T09:03:52","date_gmt":"2005-04-15T09:03:52","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/04\/15\/tweaking-our-computation-of-the-interval-between-two-moments-in-time\/"},"modified":"2005-04-15T09:03:52","modified_gmt":"2005-04-15T09:03:52","slug":"tweaking-our-computation-of-the-interval-between-two-moments-in-time","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050415-52\/?p=35893","title":{"rendered":"Tweaking our computation of the interval between two moments in time"},"content":{"rendered":"<p>\nWe can take our\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/14\/408106.aspx\">\ncomputation of the interval between two moments in time<\/a>\nand combine it with the trick we developed for\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/13\/407835.aspx\">\nusing the powers of mathematics to simplify multi-level comparisons<\/a>\nto reduce the amount of work we impose upon the time\/date engine.\n<\/p>\n<pre>\nstatic void PrintAge(DateTime bday, DateTime asof)\n{\n \/\/ Accumulate years without going over.\n int years = asof.Year - bday.Year;\n<font COLOR=\"blue\"> if (asof.Month*32 + asof.Day &lt; bday.Month*32 + bday.Day) years--;\n DateTime t = bday.AddYears(years);<\/font>\n \/\/ Accumulate months without going over.\n int months = asof.Month - bday.Month;\n if (asof.Day &lt; bday.Day) months--;\n months = (months + 12) % 12;\n t = t.AddMonths(months);\n \/\/ Days are constant-length, woo-hoo!\n int days = (asof - t).Days;\n SC.WriteLine(\"{0} years, {1} months, {2} days\",\n              years, months, days);\n}\n<\/pre>\n<p>\nObserve that we avoided a call to the <code>AddYears<\/code> method\n(which is presumably rather complicated because years are variable-length)\nby replacing it with a multi-level comparison\nto determine whether the ending month\/day falls later in the year than\nthe starting month\/day.  Since no month has 32 days, a multiplier of\n32 is enough to avoid an overflow of the day into the month field\nof the comparison key.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can take our computation of the interval between two moments in time and combine it with the trick we developed for using the powers of mathematics to simplify multi-level comparisons to reduce the amount of work we impose upon the time\/date engine. static void PrintAge(DateTime bday, DateTime asof) { \/\/ Accumulate years without going [&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-35893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>We can take our computation of the interval between two moments in time and combine it with the trick we developed for using the powers of mathematics to simplify multi-level comparisons to reduce the amount of work we impose upon the time\/date engine. static void PrintAge(DateTime bday, DateTime asof) { \/\/ Accumulate years without going [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35893","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=35893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35893\/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=35893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=35893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=35893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}