Created
April 17, 2011 21:53
-
-
Save fbuchinger/924512 to your computer and use it in GitHub Desktop.
Mockup Implementation of a HolidayTagger with sample holiday data from the United States. Doesn't yet follow the pictagger api spec. Requires underscore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function holidayTagger(date) { | |
var dateYear = date.getFullYear(); | |
var dateMonth = date.getMonth(); | |
var dateDay = date.getDate(); | |
var getEasterDate = _.memoize(function (year) { | |
var Y = year; | |
var C = Math.floor(Y / 100); | |
var N = Y - 19 * Math.floor(Y / 19); | |
var K = Math.floor((C - 17) / 25); | |
var I = C - Math.floor(C / 4) - Math.floor((C - K) / 3) + 19 * N + 15; | |
I = I - 30 * Math.floor((I / 30)); | |
I = I - Math.floor(I / 28) * (1 - Math.floor(I / 28) * Math.floor(29 / (I + 1)) * Math.floor((21 - N) / 11)); | |
var J = Y + Math.floor(Y / 4) + I + 2 - C + Math.floor(C / 4); | |
J = J - 7 * Math.floor(J / 7); | |
var L = I - J; | |
var M = 3 + Math.floor((L + 40) / 44); | |
var D = L + 28 - 31 * Math.floor(M / 4); | |
return new Date(Y, M - 1, D); | |
}); | |
//month indicies are zero-based in JS: 0 = January ... 11 = December | |
var holidaysUS = [{ | |
name: 'all saints', | |
day: 1, | |
month: 10, | |
category: 'christian' | |
}, { | |
name: 'all souls', | |
day: 2, | |
month: 10, | |
category: 'christian' | |
}, { | |
name: 'christmas eve', | |
day: 24, | |
month: 11, | |
category: 'christian' | |
}, { | |
name: 'christmas day', | |
day: 25, | |
month: 11, | |
category: 'christian' | |
}, { | |
name: 'epiphany', | |
day: 6, | |
month: 1, | |
category: 'christian' | |
}, { | |
name: 'shrove tuesday', | |
daysFromEaster: -47, | |
category: 'christian' | |
}, { | |
name: 'ash wednesday', | |
daysFromEaster: -46, | |
category: 'christian' | |
}, { | |
name: 'holy thursday', | |
daysFromEaster: -3, | |
category: 'christian' | |
}, { | |
name: 'good friday', | |
daysFromEaster: -2, | |
category: 'christian' | |
}, { | |
name: 'easter vigil', | |
daysFromEaster: -1, | |
category: 'christian' | |
}, { | |
name: 'easter', | |
daysFromEaster: 0, | |
category: 'christian' | |
}, { | |
name: 'easter monday', | |
daysFromEaster: 1, | |
category: 'christian' | |
}, { | |
name: 'pentecost', | |
daysFromEaster: 50, | |
category: 'christian' | |
}, { | |
name: 'corpus christi', | |
daysFromEaster: 60, | |
category: 'christian' | |
}, { | |
name: 'assumption of mary', | |
day: 15, | |
month: 7, | |
category: 'christian' | |
}, { | |
name: 'immaculate conception', | |
day: 8, | |
month: 11, | |
category: 'christian' | |
}, | |
//saecular holidays | |
{ | |
name: 'new years day', | |
day: 1, | |
month: 0, | |
category: 'federal' | |
}, { | |
name: "Martin Luther King's day", | |
day: 17, | |
month: 0, | |
category: 'federal' | |
}, { | |
name: "Washington's Birthday", | |
day: 21, | |
month: 1, | |
category: 'federal' | |
}, { | |
name: "Independence Day", | |
day: 4, | |
month: 6, | |
category: 'federal' | |
}, { | |
name: 'labor day', | |
weekDayInMonth: ['first', 'monday', 'september'], | |
category: 'federal' | |
}, { | |
name: 'columbus day', | |
day: 10, | |
month: 9, | |
category: 'federal' | |
}, { | |
name: 'veterans day', | |
day: 11, | |
month: 10, | |
category: 'federal' | |
}]; | |
var easterDate = getEasterDate(dateYear); | |
function daysFromEaster(days) { | |
return (new Date(dateYear, easterDate.getMonth(), easterDate.getDate() + days)) | |
} | |
var matchedHolidays = _(holidaysUS).map(function (holiday) { | |
if (!holiday.daysFromEaster) { | |
if (holiday.day === dateDay && holiday.month === dateMonth) { | |
return { | |
'name': holiday.name, | |
'category': holiday.category | |
} | |
} | |
} else { | |
var holidayDate = daysFromEaster(holiday.daysFromEaster); | |
if (holidayDate.getDate() === dateDay && holidayDate.getMonth() === dateMonth) { | |
return { | |
'name': holiday.name, | |
'category': holiday.category | |
} | |
} | |
} | |
}); | |
return _.compact(matchedHolidays); //remove falsy values | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment