Skip to content

Instantly share code, notes, and snippets.

@vdavez
Last active January 1, 2016 12:09
Show Gist options
  • Save vdavez/8142448 to your computer and use it in GitHub Desktop.
Save vdavez/8142448 to your computer and use it in GitHub Desktop.
Automatically calculate the effective date of a DC Law! To see it in action: http://jsfiddle.net/g5mYh/2/
/*Known Bugs
[x] It's probably better to build a function "inRecess" to test whether a House is in recess for more than three days (ex. Aug. 12)
[ ]
*/
var _holidays = {
'M': {//Month, Day
'01/01': "New Year's Day",
'07/04': "Independence Day",
'11/11': "Veteran's Day",
'12/25': "Christmas Day",
'12/31': "New Year's Eve"
},
'W': {//Month, Week of Month, Day of Week
'1/3/1': "Martin Luther King Jr. Day",
'2/3/1': "Washington's Birthday",
'5/5/1': "Memorial Day",
'9/1/1': "Labor Day",
'10/2/1': "Columbus Day",
'11/4/4': "Thanksgiving Day"
}
};
moment.fn.holiday = function() {
var diff = 1+ (0 | (this._d.getDate() - 1) / 7),
memorial = (this._d.getDay() === 1 && (this._d.getDate() + 7) > 30) ? "5" : null;
return (_holidays['M'][this.format('MM/DD')] || _holidays['W'][this.format('M/'+ (memorial || diff) +'/d')]);
};
// Get the list of days in session. This is from a JSON file.
var out = [];
var i = 0;
//var s = $.getJSON(session_days.json);
var s = {
"house": [
"2013-1-1",
"2013-1-2",
"2013-1-3",
"2013-1-4",
"2013-1-14",
"2013-1-15",
"2013-1-18",
"2013-1-21",
"2013-1-22",
"2013-1-23",
"2013-1-25",
"2013-1-29",
"2013-2-1",
"2013-2-4",
"2013-2-5",
"2013-2-6",
"2013-2-8",
"2013-2-12",
"2013-2-13",
"2013-2-14",
"2013-2-15",
"2013-2-25",
"2013-2-26",
"2013-2-27",
"2013-2-28",
"2013-3-4",
"2013-3-5",
"2013-3-6",
"2013-3-7",
"2013-3-11",
"2013-3-12",
"2013-3-13",
"2013-3-14",
"2013-3-15",
"2013-3-18",
"2013-3-19",
"2013-3-20",
"2013-3-21",
"2013-3-25",
"2013-4-9",
"2013-4-10",
"2013-4-11",
"2013-4-12",
"2013-4-15",
"2013-4-16",
"2013-4-17",
"2013-4-18",
"2013-4-19",
"2013-4-23",
"2013-4-24",
"2013-4-25",
"2013-4-26",
"2013-4-30",
"2013-5-3",
"2013-5-6",
"2013-5-7",
"2013-5-8",
"2013-5-9",
"2013-5-13",
"2013-5-14",
"2013-5-15",
"2013-5-16",
"2013-5-17",
"2013-5-20",
"2013-5-21",
"2013-5-22",
"2013-5-23",
"2013-5-24",
"2013-6-3",
"2013-6-4",
"2013-6-5",
"2013-6-6",
"2013-6-10",
"2013-6-11",
"2013-6-12",
"2013-6-13",
"2013-6-14",
"2013-6-17",
"2013-6-18",
"2013-6-19",
"2013-6-20",
"2013-6-24",
"2013-6-25",
"2013-6-26",
"2013-6-27",
"2013-6-28",
"2013-7-8",
"2013-7-9",
"2013-7-10",
"2013-7-11",
"2013-7-15",
"2013-7-16",
"2013-7-17",
"2013-7-18",
"2013-7-19",
"2013-7-22",
"2013-7-23",
"2013-7-24",
"2013-7-25",
"2013-7-26",
"2013-7-30",
"2013-7-31",
"2013-8-1",
"2013-8-2",
"2013-9-6",
"2013-9-9",
"2013-9-10",
"2013-9-11",
"2013-9-12",
"2013-9-16",
"2013-9-17",
"2013-9-18",
"2013-9-19",
"2013-9-20",
"2013-9-23",
"2013-9-25",
"2013-9-26",
"2013-9-27",
"2013-9-28",
"2013-9-29",
"2013-9-30",
"2013-10-1",
"2013-10-2",
"2013-10-3",
"2013-10-4",
"2013-10-5",
"2013-10-7",
"2013-10-8",
"2013-10-9",
"2013-10-10",
"2013-10-11",
"2013-10-12",
"2013-10-14",
"2013-10-15",
"2013-10-16",
"2013-10-22",
"2013-10-23",
"2013-10-24",
"2013-10-28",
"2013-10-29",
"2013-10-30",
"2013-11-12",
"2013-11-13",
"2013-11-14",
"2013-11-15",
"2013-11-18",
"2013-11-19",
"2013-11-20",
"2013-11-21",
"2013-11-22",
"2013-12-2",
"2013-12-3",
"2013-12-4",
"2013-12-5",
"2013-12-9",
"2013-12-10",
"2013-12-11",
"2013-12-12",
"2013-12-16",
"2013-12-19",
"2013-12-23",
"2013-12-26"
],
"senate": [
"2013-1-1",
"2013-1-2",
"2013-1-3",
"2013-1-4",
"2013-1-21",
"2013-1-22",
"2013-1-23",
"2013-1-24",
"2013-1-28",
"2013-1-29",
"2013-1-30",
"2013-1-31",
"2013-2-4",
"2013-2-7",
"2013-2-11",
"2013-2-12",
"2013-2-13",
"2013-2-14",
"2013-2-15",
"2013-2-25",
"2013-2-26",
"2013-2-27",
"2013-2-28",
"2013-3-4",
"2013-3-5",
"2013-3-6",
"2013-3-7",
"2013-3-11",
"2013-3-12",
"2013-3-13",
"2013-3-14",
"2013-3-18",
"2013-3-19",
"2013-3-20",
"2013-3-21",
"2013-3-22",
"2013-4-8",
"2013-4-9",
"2013-4-10",
"2013-4-11",
"2013-4-15",
"2013-4-16",
"2013-4-17",
"2013-4-18",
"2013-4-22",
"2013-4-23",
"2013-4-24",
"2013-4-25",
"2013-4-26",
"2013-4-30",
"2013-5-3",
"2013-5-6",
"2013-5-7",
"2013-5-8",
"2013-5-9",
"2013-5-13",
"2013-5-14",
"2013-5-15",
"2013-5-16",
"2013-5-20",
"2013-5-21",
"2013-5-22",
"2013-5-23",
"2013-6-3",
"2013-6-4",
"2013-6-6",
"2013-6-7",
"2013-6-10",
"2013-6-11",
"2013-6-12",
"2013-6-13",
"2013-6-17",
"2013-6-18",
"2013-6-19",
"2013-6-20",
"2013-6-21",
"2013-6-24",
"2013-6-25",
"2013-6-26",
"2013-6-27",
"2013-7-9",
"2013-7-10",
"2013-7-11",
"2013-7-15",
"2013-7-16",
"2013-7-17",
"2013-7-18",
"2013-7-19",
"2013-7-23",
"2013-7-24",
"2013-7-25",
"2013-7-29",
"2013-7-30",
"2013-7-31",
"2013-8-1",
"2013-8-2",
"2013-8-12",
"2013-9-9",
"2013-9-6",
"2013-9-10",
"2013-9-11",
"2013-9-12",
"2013-9-16",
"2013-9-17",
"2013-9-18",
"2013-9-19",
"2013-9-23",
"2013-9-24",
"2013-9-26",
"2013-9-27",
"2013-9-30",
"2013-10-1",
"2013-10-2",
"2013-10-3",
"2013-10-4",
"2013-10-5",
"2013-10-7",
"2013-10-8",
"2013-10-9",
"2013-10-10",
"2013-10-11",
"2013-10-12",
"2013-10-13",
"2013-10-14",
"2013-10-15",
"2013-10-16",
"2013-10-28",
"2013-10-29",
"2013-10-30",
"2013-10-31",
"2013-11-4",
"2013-11-5",
"2013-11-6",
"2013-11-7",
"2013-11-8",
"2013-11-12",
"2013-11-13",
"2013-11-14",
"2013-11-18",
"2013-11-19",
"2013-11-20",
"2013-11-21",
"2013-12-9",
"2013-12-10",
"2013-12-11",
"2013-12-15",
"2013-12-16",
"2013-12-17",
"2013-12-18",
"2013-12-19",
"2013-12-20",
"2013-12-24"
]
};
var sDays = _.union(s.house, s.senate);
//This converts the strings into Date objects to be handled by momentjs
sDays = _.map(sDays,function(d){return moment(d.replace(/(-)(\d(?!\d))/g,"-0$2"))});
//This gets the transmittal date
var transmittal = prompt("start? [form 2013-12-25]").split(/[-\/]/g);
$("#transmittal").text(transmittal.join("/"));
transmittal[1] = transmittal[1]-1;
var dayOne = moment(transmittal);
if (!dayOne.isValid()) {(alert("date's invalid"))}
else {
var c = moment(dayOne);
while (i < 30) {
//Count the day?
if (isCountedDay(sDays,c)) {out[i] = c.format("YYYY/MM/DD"); i++;}
c = getNextDay(sDays,c);
}
}
$("#counter").text(JSON.stringify(out));
console.log(JSON.stringify(out));
$("#effective").text(c.format("dddd, MMM. DD, YYYY"));
function checkMomentArray (arr, val) {
for (var i=0; i < arr.length; i++) {
if (arr[i].isSame(val)) return true;
}
}
function getNextSessionDay (arr, val) {
for (var i=0;i < arr.length; i++) {
if (arr[i].isAfter(val)) return arr[i];
}
}
function isCountedDay (arr, val) {
//Check for count
// if (val.weekday() == 0 || val.weekday() == 6 || typeof(val.holiday()) != "undefined" || (withinThreeDays(arr,val) != true) || inRecess(val) == true) {return false}
if (val.weekday() == 0 || val.weekday() == 6 || typeof(val.holiday()) != "undefined" || inRecess(val)) {return false}
else {return true}
}
function withinThreeDays (arr, val) {
for (var i=0; i < 3; i++)
{
var duration = moment.duration({'days' : parseInt(i)});
var nextDay = moment(val).add(duration);
if (nextDay.weekday() == 0) {nextDay = moment(nextDay).add('days',1)}
if (checkMomentArray(arr,nextDay)==true) {return true}
}
}
function getNextDay(arr,val) {
var duration = moment.duration({'days' : 1});
var nextDay = moment(val).add(duration);
return nextDay;
}
function inRecess (val) {
var _recess = [{"begin":"2013/01/07","end":"2013/01/11"},{"begin":"2013/02/18","end":"2013/02/22"},{"begin":"2013/03/25","end":"2013/04/05"},{"begin":"2013/05/27","end":"2013/05/31"},{"begin":"2013/07/01","end":"2013/07/05"},{"begin":"2013/08/05","end":"2013/08/09"},{"begin":"2013/08/13","end":"2013/09/05"},{"begin":"2013/10/17","end":"2013/10/21"},{"begin":"2013/11/25","end":"2013/11/29"}];
//iterate to check if in range
for (var i = 0; i < _recess.length; i++) {
var begin = moment(_recess[i].begin);
var end = moment(_recess[i].end);
// console.log("B:" + begin.toDate() + "\nE:" + end.toDate() + "\nC:" + val.toDate());
if (val >= begin && val <= end) return true;
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment