Commit 252fa957 authored by Johannes Bill's avatar Johannes Bill

operators finished

parent ed95e60c
//gibt zu betrachtenden Zeitraum an. //gibt zu betrachtenden Zeitraum an.
var _epochStart = new Date(2014, 5, 24, 0); var _epochStart = new Date(2014, 5, 24, 0);
var _epochEnd = new Date(_epochStart.getTime() + 30 * 24 * 3600 * 1000); var _epochEnd = new Date(_epochStart.getTime() + 100 * 24 * 3600000);
console.log("epochStart", _epochStart);
console.log("epochEnd ", _epochEnd);
function _cyclicInterval(from, until, interval, epochStart, epochEnd) { function _cyclicInterval(from, until, interval, epochStart, epochEnd) {
if(from >= until) throw new Error("startDate cant be greater than endDate"); if(from >= until) throw new Error("startDate cant be greater than endDate");
...@@ -22,8 +25,9 @@ function _cyclicInterval(from, until, interval, epochStart, epochEnd) { ...@@ -22,8 +25,9 @@ function _cyclicInterval(from, until, interval, epochStart, epochEnd) {
* @param timeUntil.m * @param timeUntil.m
* @param epochStart * @param epochStart
* @param epochEnd * @param epochEnd
* @param distanceDays
*/ */
function standard(day, timeFrom, timeUntil, epochStart, epochEnd) { function standard(day, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) {
var dayDiff = day - epochStart.getDay(); var dayDiff = day - epochStart.getDay();
if(dayDiff < 0) if(dayDiff < 0)
dayDiff += 7; dayDiff += 7;
...@@ -32,8 +36,9 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd) { ...@@ -32,8 +36,9 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd) {
epochStart.getDate() + dayDiff, timeFrom.h, timeFrom.m); epochStart.getDate() + dayDiff, timeFrom.h, timeFrom.m);
var endDate = new Date(epochStart.getFullYear(), epochStart.getMonth(), var endDate = new Date(epochStart.getFullYear(), epochStart.getMonth(),
epochStart.getDate() + dayDiff, timeUntil.h, timeUntil.m); epochStart.getDate() + dayDiff, timeUntil.h, timeUntil.m);
return _cyclicInterval(startDate, endDate, 7 * 24 * 3600000, epochStart, epochEnd); return _cyclicInterval(startDate, endDate, distanceDays * 24 * 3600000, epochStart, epochEnd);
} }
/** /**
* *
* @param dayFrom * @param dayFrom
...@@ -47,29 +52,111 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd) { ...@@ -47,29 +52,111 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd) {
* @param epochStart {Date} * @param epochStart {Date}
* @param epochEnd {Date} * @param epochEnd {Date}
* @returns {Array} * @returns {Array}
* @param distanceDays
*/ */
function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd) { function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) {
var result = []; var result = [];
for(var i = dayFrom; i <= dayTill; i++) { for(var i = dayFrom; i <= dayTill; i++) {
result = result.concat(standard(i, timeFrom, timeUntil, epochStart, epochEnd)) result = result.concat(standard(i, timeFrom, timeUntil, epochStart, epochEnd, distanceDays))
} }
result = sortDateIntervals(result); result = sortDateIntervals(result);
return result; return result;
} }
function cyclic(dayFrom, dayTill, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) { function cyclic(day, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) {
var result = [];
epochStart = new Date(epochStart);
while(true) {
var startDate = _getNextNoInMonthOccurence(day, epochStart, noInMonth);
startDate.setHours(timeFrom.h);
startDate.setMinutes(timeFrom.m);
var endDate = new Date(startDate);
endDate.setHours(timeUntil.h);
endDate.setMinutes(timeUntil.m);
if(endDate > epochEnd)
break;
else {
result.push([startDate, endDate]);
epochStart = new Date(endDate);
epochStart.setDate(epochStart.getDate() + 1);
}
}
return result;
} }
function cyclicFromTillDay(dayFrom, dayTill, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) {
var result = [];
epochStart = new Date(epochStart);
function _getDateOccurenceInMonth(day, startDate, noInMonth) { while(true) {
var startDate = _getNextNoInMonthOccurence(dayFrom, epochStart, noInMonth);
startDate.setHours(timeFrom.h);
startDate.setMinutes(timeFrom.m);
var endDate = new Date(startDate);
endDate.setHours(timeUntil.h);
endDate.setMinutes(timeUntil.m);
if(endDate > epochEnd)
break;
else {
result.push([startDate, endDate]);
epochStart = new Date(endDate);
epochStart.setDate(epochStart.getDate() + 1);
for(var offset = 1; offset <= dayTill - dayFrom; offset++) {
startDate = new Date(startDate); startDate = new Date(startDate);
var dayDiff = day - startDate.getDay() + 7; endDate = new Date(endDate);
startDate.setDate(startDate.getDate() + 1);
endDate.setDate(endDate.getDate() + 1);
if(endDate <= epochEnd)
result.push([startDate, endDate]);
else break;
}
}
}
return result;
}
function _cyclicFromTillDayOld(dayFrom, dayTill, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) {
var firstDays = cyclic(dayFrom, noInMonth, timeFrom, timeUntil, epochStart, epochEnd);
var result = firstDays.slice();
for(var offset = 1; offset <= dayTill - dayFrom; offset++) {
for(var i = 0; i < firstDays.length; i++) {
var start = new Date(firstDays[i][0]);
var end = new Date(firstDays[i][1]);
start.setDate(start.getDate() + offset);
end.setDate(end.getDate() + offset);
if(end <= epochEnd)
result.push([start, end]);
}
}
sortDateIntervals(result);
return result;
}
function _getNextNoInMonthOccurence(day, startDate, noInMonth) {
startDate = new Date(startDate);
var _noInMonth;
if(noInMonth == -1) {
_noInMonth = 1;
startDate.setDate(startDate.getDate() + 7);
}
else {
_noInMonth = noInMonth;
}
var dayDiff = day - startDate.getDay();
if(dayDiff < 0) dayDiff += 7;
startDate.setDate(startDate.getDate() + dayDiff); startDate.setDate(startDate.getDate() + dayDiff);
var currentNoInMonth = Math.floor(startDate.getDate() / 7) + 1; var currentNoInMonth = Math.ceil(startDate.getDate() / 7);
while(currentNoInMonth != noInMonth) { while(currentNoInMonth != _noInMonth) {
startDate = new Date(startDate.getTime() + 7 * 24 * 3600000); startDate = new Date(startDate.getTime() + 7 * 24 * 3600000);
currentNoInMonth = Math.floor(startDate.getDate() / 7) + 1; currentNoInMonth = Math.ceil(startDate.getDate() / 7);
}
if(noInMonth == -1) {
startDate.setDate(startDate.getDate() - 7);
} }
return startDate; return startDate;
} }
...@@ -172,8 +259,8 @@ function andNotOperator(interval1, interval2) { ...@@ -172,8 +259,8 @@ function andNotOperator(interval1, interval2) {
//var y = andNotOperator(a, c); //var y = andNotOperator(a, c);
//console.log(y); //console.log(y);
//var workday = standardFromTillDay(1, 5, {h: 9, m: 0}, {h: 11, m: 45}, _epochStart, _epochEnd); var workday = standardFromTillDay(1, 2, {h: 9, m: 0}, {h: 11, m: 45}, _epochStart, _epochEnd, 14);
//console.log(workday); console.log(workday);
var x = _getDateOccurenceInMonth(1, new Date(), 5); //var t = cyclicFromTillDay(1, 7, -1, {h: 9, m: 0}, {h: 18, m: 29}, _epochStart, _epochEnd);
console.log(x); //console.log(t);
\ No newline at end of file \ No newline at end of file
var x = new Date(); var x = new Date();
var y = new Date(x);
y.setDate(1);
console.log(x); console.log(x);
x.setDate(-1); console.log(y);
console.log(x); \ No newline at end of file
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment