Permalink
Browse files

added test file

  • Loading branch information...
1 parent f15f8ca commit 832eb47811ebc93297bf5733c7fa42d2e67b85f8 @mplungjan committed Feb 20, 2012
Showing with 125 additions and 0 deletions.
  1. +125 −0 addHolidays.html
View
@@ -0,0 +1,125 @@
+<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
+<script type="text/javascript">
+/* Holiday script using jQuery for calendar
+ (c) 2010-2012 Michel Plungjan javascripts(a)plungjan.name
+ Aim is eventually to turn this into a jQuery Plugin
+*/
+Number.prototype.pad = function(n) {
+ for(var r = this.toString(); r.length < n; r = 0 + r);
+ return r;
+};
+var Sun=0,Mon=1,Tue=2,Wed=3,Thu=4,Fri=5,Sat=6;
+function NthDay(whatOffset,whichDay,whichMonth,whichYear) {
+ // examples - whichDay is Sun=0 to Sat=6:
+ // 3rd Monday in January 2010: (3,1,1,2010); // Martin Luther King day 2010
+ // 3rd Monday In February, current year (3,1,2); // Washington Day
+ // Last Friday In April (-1,5,4); // National Arbor Day
+ var d = new Date();
+ if (whichYear) d.setFullYear(whichYear);
+ if (whatOffset != -1) {
+ d.setMonth(whichMonth-1,1,0,0,0,0); // first of the month, normalised;
+ while (d.getDay()!=whichDay) d.setDate(d.getDate()+1);
+ d.setDate(d.getDate()+((whatOffset-1)*7))
+ }
+ else {
+ d.setMonth(d.getMonth()+1,0,0,0,0); // last of the month, normalised;
+ while (d.getDay()!=whichDay) d.setDate(d.getDate()-1);
+ }
+ return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2);
+}
+/* Easter Calculation by Stephen Chapman http://www.felgall.com March 2007
+ ("Meeus/Jones/Butcher Gregorian algorithm".) */
+ function easter(year) {
+ var a = year % 19;
+ var b = Math.floor(year/100);
+ var c = year % 100;
+ var d = Math.floor(b/4);
+ var e = b % 4;
+ var f = Math.floor((b+8) / 25);
+ var g = Math.floor((b-f+1) / 3);
+ var h = (19*a + b - d - g + 15) % 30;
+ var i = Math.floor(c/4);
+ var j = c % 4;
+ var k = (32 + 2*e + 2*i - h - j) % 7;
+ var m = Math.floor((a + 11*h + 22*k) / 451);
+ var month = Math.floor((h + k - 7*m + 114) / 31);
+ var day = ((h + k - 7*m +114) % 31) + 1;
+ return [3,4][month-3].pad(2)+'/'+day.pad(2);
+}
+var yearMaps = [];
+function getVariableHoliday(dateString,year) {
+ if (!yearMaps[year]) {
+ yearMaps[year] = [];
+ for (var o in variableHolidayMap) {
+ var varHolString = eval('NthDay('+variableHolidayMap[o]+','+year+')');
+ alert(o+':'+varHolString)
+ yearMaps[year][varHolString] = o;
+ }
+ }
+ return yearMaps[year][dateString];
+}
+var fixedHolidayMap = { // "Name of day":"MM/DD"
+"New years Day" :"01/01",
+"Groundhog Day" :"02/02",
+"Valentine's Day" :"02/14",
+"Earth Day" :"04/22",
+"Flag Day" :"06/14",
+"Independence Day" :"07/04",
+"Halloween" :"10/31",
+"Veterans Day" :"11/11",
+"Pearl Harbor Remembrance Day":"12/07",
+"Christmas Day" :"12/25"
+}
+var variableHolidayMap = { // "Name of day":"Nth,DDD,M"
+"Martin Luther King Day":"3,Mon,1", // 3rd Mon in Jan
+"Washington's Birthday" :"3,Mon,2", // 3rd Mon in Feb
+"National Arbor Day" :"-1,Fri,4", // Last Fri of Apr
+"Mother's Day" :"2,Sun,5", // 2nd Sun in May
+"Memorial Day" :"-1,Mon,5", // Last Mon of May
+"Father's Day" :"3,Sun,6", // 3rd Sun in Jun
+"Labor Day" :"1,Mon,9", // 1st Mon in Sep
+"Columbus Day" :"2,Mon,10", // 2nd Mon in Oct
+"Thanksgiving Day" :"4,Thu,11" // 4th Thu in Nov
+};
+var availableTags = ["Easter"]; // the odd one out
+var fixedHoliday = [];
+for (var o in fixedHolidayMap) { // build a holiday by dateString
+ availableTags[availableTags.length] = o;
+ fixedHoliday[fixedHolidayMap[o]]=o;
+}
+
+for (var o in variableHolidayMap) availableTags[availableTags.length] = o;
+
+ $(document).ready(function() {
+ $('#holidayDate').datepicker();
+ $("#tags").autocomplete({
+ source:availableTags,
+ select:function(event,ui) {
+ var res = "";
+ var thisYear = new Date().getFullYear();
+ if (fixedHolidayMap[ui.item.value]) res = fixedHolidayMap[ui.item.value];
+ else if (ui.item.value == "Easter") res = easter(thisYear);
+ else if (variableHolidayMap[ui.item.value]) res = eval('NthDay('+variableHolidayMap[ui.item.value]+','+thisYear+')')
+ if (res) res += '/'+thisYear;
+ $('#holidayDate').val(res);
+ }
+ });
+ $('#holidayDate').change(function() {
+ var dateString = $(this).val().substring(0, 5);
+ var year = $(this).val().substring(6);
+ var res = "";
+ if (fixedHoliday[dateString]) res = fixedHoliday[dateString]
+ else if (dateString == easter(year)) res = "Easter";
+ else {
+ var holiday = getVariableHoliday(dateString,year);
+ if (holiday) res = holiday;
+ }
+ $('#tags').val(res);
+ });
+
+ });
+
+</script>
+<input id="holidayDate" type="text" />
+<input id="tags"></input>

0 comments on commit 832eb47

Please sign in to comment.