Our shift pattern is actually called a modified Detroit schedule. The actual shift pattern is CACBCBABA. Check out this link to see an actual printed calendar. http://www.ladder54.com/schedule.htm. If we look at October 1st then follow the pattern of CACBCBABA you will see what I mean, I work the red days, all 110 to 112 days a year at 24 hours a shift. We would need to trap for leap years as well all though I don't think that is much of a problem, I could be wrong though....
While I have never heard of the Modified Detroit style schedule, the problem here is one of pattern calculation. We have a pattern that starts on a given date, October 1, 2001 and repeats going forward and backward with the pattern CACBCBABA. By now, anyone who sees the phrase "repeating pattern" should immediatly think of our good friend, the Modulus operator. Using the modulus operator, we can figure out how many times the pattern can be fully applied to the difference in days between a given day and the base date.
To help demonstrate that, let's create a ColdFusion user defined function that will take a date and return either "shifta", "shiftb", or "shiftc" depending on how the Modified Detroit patterns gets applied:
Launch code in new window » Download code as text file »
The thing you have to be careful of with the modulus operator is that taking the modulus of a negative number can result in a negative number. Therefore, when our given day falls before our base date, we need to apply the modulus in a slightly different manner - starting at the end of the pattern map rather than at the beginning.
Now that we have our ColdFusion user defined function for getting the modified detroit style schedule, let's apply that to the CSS classes of our standard calendar display:
Launch code in new window » Download code as text file »
Here, we are looping over the days in a given month, but we are using our GetDetroitShift() ColdFusion UDF to get both the CSS class for our calendar day as well as the displayed letter (A,B,C). By default, the calendar starts on the current month:
| | | | ||
| | ![]() | | ||
| | | |
And, if we jump over to the base month, October 2001, you will se that 10/01/2001 does indeed start our CACBCBABA pattern:
| | | | ||
| | ![]() | | ||
| | | |
To try out the demo for yourself, click here.
Download Code Snippet ZIP File
Comments (11) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Ben,
Don't forget to trap whitespace. Check the source at the beginning and at the end in your example at http://www.bennadel.com/resources/demo/modified_detroit/index.cfm
Dmitriy
Posted by Dmitriy Goltseker on Jul 10, 2007 at 3:31 PM
Thanks dude. I just added a:
<cfcontent
type="text/html"
reset="true"
/>
... before the content. Trimmed the leading white space nicely.
Posted by Ben Nadel on Jul 10, 2007 at 3:58 PM
This is just awesome, now to only apply this to the number of the day and not the background, this way we can see the events that we want to list on each day.
THANK YOU THANK YOU,
Jim
Posted by Jim Schell on Jul 10, 2007 at 4:23 PM
@Jim,
My pleasure to help. Please let me know if you need any help modifying this.
Posted by Ben Nadel on Jul 10, 2007 at 4:27 PM
@Ben: How about at the end of the document?
Posted by Dmitriy Goltseker on Jul 10, 2007 at 4:45 PM
Actually if we could set this so only the date/number was color coded along with the shift designator, the next thing is what would be the best to way to plug in the events on the appropriate day. I will try to follow your code from the previous calendar example.
So you would have the day color code (number only) white background of the day grid/block and events on each day in its respective block/grid say blue in color, in this case of the staffing calendar would show who is off and on what type of leave are they using, sick, vacation etc., with these events being hyperlinked to a detail page.
Thanks so much for your help
Jim
Posted by Jim Schell on Jul 10, 2007 at 4:48 PM
Hmmm, the end of document white space must be caused by the OnRequestEnd() Application.cfc method.... I will look into it.
Posted by Ben Nadel on Jul 10, 2007 at 4:49 PM
Ben:
I am always amazed at how much energy you put into these posts. Way to go!
You know... you could probably charge for these kinds of things!
LOL
Posted by Ken Auenson, II on Jul 10, 2007 at 5:26 PM
I am amazed as well, what Ben did in a couple of hours, would have taken me me months to figure out if I could even figure it out.
Jim
Ben THANK YOU
Posted by Jim Schell on Jul 10, 2007 at 5:31 PM
Yeah! Three cheers for Ben!
As I started reading this solution, I said, out loud, "man, this guy is so smart!" Of course, I always think that when reading this blog.
Now I'm thinking of 1984, the novel. Something about reading a good book and feeling as though you must have already read it because everything in it is so familiar. When reading Ben's solutions, I'm thinking. . ."yes, yes, that makes perfect sense. . .and so simple too! I would have done the same thing--after hacking for days rather than hours and only if I took the time to rethink and rewrite my thousands of lines of code down to the tens or hundreds that would make it more efficient, readable and reusable."
hip hip hooray!
Posted by macbuoy on Jul 10, 2007 at 5:59 PM
Oh you guys! :) Thanks for the kind words!
Posted by Ben Nadel on Jul 11, 2007 at 8:48 AM