{"id":61,"date":"2007-12-20T10:39:36","date_gmt":"2007-12-20T10:39:36","guid":{"rendered":"http:\/\/www.myfatblog.co.uk\/?p=61"},"modified":"2007-12-20T10:39:36","modified_gmt":"2007-12-20T10:39:36","slug":"adding-business-hours","status":"publish","type":"post","link":"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/","title":{"rendered":"Adding business hours&#8230;."},"content":{"rendered":"<p>Part of our workflows have some time critical tasks. In this particular instance, there is a measurable KPI of 12 business hours to respond to an event.<\/p>\n<p>The DateTime construct is very useful for date manipulation, but does not appear to provide an easy way to understand Business Hours. So how to go about tracking this?<\/p>\n<p>The simplest way that I managed to come up with, was to create two methods, One that checks if a given DateTime exists on a business day, the other that checks whether the time falls within business hours, named onBusinessDay() and inBusinessHours() respecitively.<\/p>\n<p><code>        static bool onBusinessDay(DateTime timeToTest)<br \/>\n        {<br \/>\n            ArrayList mBusinessDays = new ArrayList();<br \/>\n            mBusinessDays.Add(\"Monday\");<br \/>\n            mBusinessDays.Add(\"Tuesday\");<br \/>\n            mBusinessDays.Add(\"Wednesday\");<br \/>\n            mBusinessDays.Add(\"Thursday\");<br \/>\n            mBusinessDays.Add(\"Friday\");<\/p>\n<p>            if (mBusinessDays.Contains(timeToTest.DayOfWeek.ToString()))<br \/>\n            {<br \/>\n                return true;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                return false;<br \/>\n            }<\/p>\n<p>        }<\/code><\/p>\n<p>and-<\/p>\n<p><code>        static bool inBusinessHours(DateTime timeToTest)<br \/>\n        {<br \/>\n            DateTime businessStart = DateTime.Parse(timeToTest.ToShortDateString() + \" 09:00:00\");<br \/>\n            DateTime businessEnd = DateTime.Parse(timeToTest.ToShortDateString() + \" 17:30:00\");<br \/>\n            if ((timeToTest.CompareTo(businessStart) >= 0) && (timeToTest.CompareTo(businessEnd) <= 0))\n            {\n                return true;\n            }\n            return false;\n        }<\/code><\/p>\n<p>This returns the value's nicely, but now we have the problem, How do we add the hours and track the business hours accuratley? Remember, DateTime has no concept of business hours.<\/p>\n<p>As part of a learning exercise, I decided to use an Enum to create a Bit Flag set to track the BusinessHours and BusinessDay results. (Remember to declare this outside of your main method.<\/p>\n<p><code>        [Flags]<br \/>\n        enum DateHoursCheck : uint<br \/>\n        {<br \/>\n            inBusinessHours = 0x01, \/\/Bit 0<br \/>\n            onBusinessDay = 0x02, \/\/Bit 1<br \/>\n            notUsed1 = 0x04, \/\/Bit 2<br \/>\n            notUsed2 = 0x08 \/\/Bit 3<br \/>\n        }<\/code><\/p>\n<p>And finally, we get to the program code itself. In order to track the business hours usage, we just create a For loop that increments Int i until it reaches taskHours - 1, The key here, is that we only increment i, if the hour added results in the testDate being inBusinessHours AND on a businessDay. This way we work around out of hours and weekend issues.<\/p>\n<p><code>        static void Main(string[] args)<br \/>\n        {<br \/>\n            DateTime startDate;<br \/>\n            DateTime testDate;<br \/>\n            int taskHours = 12;<\/p>\n<p>            DateHoursCheck dhc = new DateHoursCheck();<\/p>\n<p>            startDate = DateTime.Now;<br \/>\n            testDate = startDate;<\/p>\n<p>            \/\/We're going to surround the logic in a big loop construct.<br \/>\n            \/\/This will set the task time and then test it for business day and business hours.<br \/>\n            \/\/Ifboth are correct, we'll end up with True True, which will end the loop.<\/p>\n<p>            for (int i = 0; i < taskHours-1;)\n            {\n                \/\/Each time the enum is 11, then we'll inc i.\n                \/\/This should mean we check each of the 12 business hours.\n\t\t\n\t\t\/\/Reset the bit flags to 0\n                dhc = 0;\n\n                testDate = testDate.AddHours(1);\n\n                if(inBusinessHours(testDate))\n                {\n                    dhc = dhc | DateHoursCheck.inBusinessHours;\n                }\n                if(onBusinessDay(testDate))\n                {\n                    dhc = dhc | DateHoursCheck.onBusinessDay;\n                }\n\n\n                if (((dhc &#038; DateHoursCheck.onBusinessDay) == DateHoursCheck.onBusinessDay) &#038;&#038;\n                ((dhc &#038; DateHoursCheck.inBusinessHours) == DateHoursCheck.inBusinessHours))\n                {\n                    i++;\n                }\n          }\n\n            Console.WriteLine(\"New Task Time - \" + testDate.ToString());\n            Console.ReadLine();\n        }\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part of our workflows have some time critical tasks. In this particular instance, there is a measurable KPI of 12 business hours to respond to an event. The DateTime construct is very useful for date manipulation, but does not appear to provide an easy way to understand Business Hours. So how to go about tracking &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[13,16,12,10],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Adding business hours.... - Blog of an overweight SharePoint addict<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adding business hours.... - Blog of an overweight SharePoint addict\" \/>\n<meta property=\"og:description\" content=\"Part of our workflows have some time critical tasks. In this particular instance, there is a measurable KPI of 12 business hours to respond to an event. The DateTime construct is very useful for date manipulation, but does not appear to provide an easy way to understand Business Hours. So how to go about tracking &hellip; Continue reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog of an overweight SharePoint addict\" \/>\n<meta property=\"article:published_time\" content=\"2007-12-20T10:39:36+00:00\" \/>\n<meta name=\"author\" content=\"Cimares\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cimares\" \/>\n<meta name=\"twitter:site\" content=\"@cimares\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Cimares\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/\",\"url\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/\",\"name\":\"Adding business hours.... - Blog of an overweight SharePoint addict\",\"isPartOf\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/#website\"},\"datePublished\":\"2007-12-20T10:39:36+00:00\",\"dateModified\":\"2007-12-20T10:39:36+00:00\",\"author\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#website\",\"url\":\"http:\/\/www.myfatblog.co.uk\/\",\"name\":\"Blog of an overweight SharePoint addict\",\"description\":\"The rantings of a (not so) food obsessed IT consultant!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.myfatblog.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6\",\"name\":\"Cimares\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg\",\"contentUrl\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg\",\"caption\":\"Cimares\"},\"sameAs\":[\"http:\/\/www.myfatblog.co.uk\"],\"url\":\"http:\/\/www.myfatblog.co.uk\/index.php\/author\/reginald\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Adding business hours.... - Blog of an overweight SharePoint addict","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/","og_locale":"en_US","og_type":"article","og_title":"Adding business hours.... - Blog of an overweight SharePoint addict","og_description":"Part of our workflows have some time critical tasks. In this particular instance, there is a measurable KPI of 12 business hours to respond to an event. The DateTime construct is very useful for date manipulation, but does not appear to provide an easy way to understand Business Hours. So how to go about tracking &hellip; Continue reading","og_url":"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/","og_site_name":"Blog of an overweight SharePoint addict","article_published_time":"2007-12-20T10:39:36+00:00","author":"Cimares","twitter_card":"summary_large_image","twitter_creator":"@cimares","twitter_site":"@cimares","twitter_misc":{"Written by":"Cimares","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/","url":"http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/","name":"Adding business hours.... - Blog of an overweight SharePoint addict","isPartOf":{"@id":"http:\/\/www.myfatblog.co.uk\/#website"},"datePublished":"2007-12-20T10:39:36+00:00","dateModified":"2007-12-20T10:39:36+00:00","author":{"@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.myfatblog.co.uk\/index.php\/2007\/12\/adding-business-hours\/"]}]},{"@type":"WebSite","@id":"http:\/\/www.myfatblog.co.uk\/#website","url":"http:\/\/www.myfatblog.co.uk\/","name":"Blog of an overweight SharePoint addict","description":"The rantings of a (not so) food obsessed IT consultant!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.myfatblog.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6","name":"Cimares","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/image\/","url":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg","contentUrl":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg","caption":"Cimares"},"sameAs":["http:\/\/www.myfatblog.co.uk"],"url":"http:\/\/www.myfatblog.co.uk\/index.php\/author\/reginald\/"}]}},"_links":{"self":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/61"}],"collection":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":0,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}