Commit ed95e60c authored by Johannes Bill's avatar Johannes Bill

operators finished

parent 0ada255d
//gibt zu betrachtenden Zeitraum an. //gibt zu betrachtenden Zeitraum an.
var epocheStart = new Date(2014, 5, 23, 0); var _epochStart = new Date(2014, 5, 24, 0);
var epocheEnd = new Date(epocheStart.getTime() + 30 * 24 * 3600 * 1000); var _epochEnd = new Date(_epochStart.getTime() + 30 * 24 * 3600 * 1000);
function cyclicInterval(from, until, interval) { function _cyclicInterval(from, until, interval, epochStart, epochEnd) {
if(from >= until) throw new Error("startDate cant be greater than endDate");
var resultArray = []; var resultArray = [];
for(var offset = 0; until.getTime() + offset < epochEnd.getTime(); offset += interval) {
for(var offset = 0; until.getTime() + offset < epocheEnd.getTime(); offset += interval) {
resultArray.push([new Date(from.getTime() + offset), new Date(until.getTime() + offset)]); resultArray.push([new Date(from.getTime() + offset), new Date(until.getTime() + offset)]);
} }
return resultArray; return resultArray;
} }
/**
*
* @param day weekday 0=sunday
* @param timeFrom
* @param timeFrom.h
* @param timeFrom.m
* @param timeUntil
* @param timeUntil.h
* @param timeUntil.m
* @param epochStart
* @param epochEnd
*/
function standard(day, timeFrom, timeUntil, epochStart, epochEnd) {
var dayDiff = day - epochStart.getDay();
if(dayDiff < 0)
dayDiff += 7;
var startDate = new Date(epochStart.getFullYear(), epochStart.getMonth(),
epochStart.getDate() + dayDiff, timeFrom.h, timeFrom.m);
var endDate = new Date(epochStart.getFullYear(), epochStart.getMonth(),
epochStart.getDate() + dayDiff, timeUntil.h, timeUntil.m);
return _cyclicInterval(startDate, endDate, 7 * 24 * 3600000, epochStart, epochEnd);
}
/**
*
* @param dayFrom
* @param dayTill
* @param timeFrom
* @param timeFrom.h
* @param timeFrom.m
* @param timeUntil
* @param timeUntil.h
* @param timeUntil.m
* @param epochStart {Date}
* @param epochEnd {Date}
* @returns {Array}
*/
function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd) {
var result = [];
for(var i = dayFrom; i <= dayTill; i++) {
result = result.concat(standard(i, timeFrom, timeUntil, epochStart, epochEnd))
}
result = sortDateIntervals(result);
return result;
}
function cyclic(dayFrom, dayTill, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) {
}
function _getDateOccurenceInMonth(day, startDate, noInMonth) {
startDate = new Date(startDate);
var dayDiff = day - startDate.getDay() + 7;
startDate.setDate(startDate.getDate() + dayDiff);
var currentNoInMonth = Math.floor(startDate.getDate() / 7) + 1;
while(currentNoInMonth != noInMonth) {
startDate = new Date(startDate.getTime() + 7 * 24 * 3600000);
currentNoInMonth = Math.floor(startDate.getDate() / 7) + 1;
}
return startDate;
}
function orOperator(intervals) { function orOperator(intervals) {
var result = []; var result = [];
for(var i = 0; i < intervals.length; i++) { for(var i = 0; i < intervals.length; i++) {
result = result.concat(intervals[i]); result = result.concat(intervals[i]);
} }
result.sort(function(a, b) { sortDateIntervals(result);
return result;
}
function sortDateIntervals(intervals) {
intervals.sort(function(a, b) {
return a[0] - b[0]; return a[0] - b[0];
}); });
return result; return intervals;
} }
function removeOverlaps(interval) { function removeOverlaps(interval) {
...@@ -47,12 +116,14 @@ function andOperator(interval1, interval2) { ...@@ -47,12 +116,14 @@ function andOperator(interval1, interval2) {
var end1 = interval1[i][1]; var end1 = interval1[i][1];
var start2 = interval2[j][0]; var start2 = interval2[j][0];
var end2 = interval2[j][1]; var end2 = interval2[j][1];
//detect overlap
//no overlap
if(end2 <= start1) if(end2 <= start1)
j++; j++;
else if(end1 <= start2) else if(end1 <= start2)
i++; i++;
//overlap
else { else {
var newStart; var newStart;
var newEnd; var newEnd;
...@@ -71,31 +142,38 @@ function andOperator(interval1, interval2) { ...@@ -71,31 +142,38 @@ function andOperator(interval1, interval2) {
return result; return result;
} }
function invert(interval, epochStart, epochEnd) {
function invert(interval) {
var result = []; var result = [];
var start=epocheStart; var start = epochStart;
for(var i = 0; i < interval.length; i++) { for(var i = 0; i < interval.length; i++) {
var end = interval[i][0]; var end = interval[i][0];
if(start<end) if(start < end)
result.push([start, end]); result.push([start, end]);
start = interval[i][1]; start = interval[i][1];
} }
if(start < epocheEnd) if(start < epochEnd)
result.push([start, epocheEnd]); result.push([start, epochEnd]);
return result; return result;
} }
function andNotOperator(interval1, interval2) { function andNotOperator(interval1, interval2) {
interval2 = invert(interval2); var epochStart = new Date(Math.min(interval1[0][0], interval2[0][0]));
var epochEnd = new Date(Math.min(interval1[interval1.length - 1][1], interval2[interval2.length - 1][1]));
interval2 = invert(interval2, epochStart, epochEnd);
return andOperator(interval1, interval2); return andOperator(interval1, interval2);
} }
//test
//var a = _cyclicInterval(new Date(2014, 5, 23, 9), new Date(2014, 5, 23, 14), 7 * 24 * 3600 * 1000);
//var b = _cyclicInterval(new Date(2014, 5, 23, 10), new Date(2014, 5, 23, 18), 7 * 24 * 3600 * 1000);
//var c = [[new Date(2014, 5, 23, 13), new Date(2014, 6, 6, 10)]];
//var x = andNotOperator(a, b);
//var y = andNotOperator(a, c);
//console.log(y);
var a = cyclicInterval(new Date(2014, 5, 23, 9), new Date(2014, 5, 23, 14), 7 * 24 * 3600 * 1000); //var workday = standardFromTillDay(1, 5, {h: 9, m: 0}, {h: 11, m: 45}, _epochStart, _epochEnd);
var b = cyclicInterval(new Date(2014, 5, 23, 13), new Date(2014, 5, 23, 18), 7 * 24 * 3600 * 1000); //console.log(workday);
var c = [[new Date(2014, 5, 23, 9), new Date(2014, 5, 30, 10)]];
var x = andNotOperator(a, b);
var x = _getDateOccurenceInMonth(1, new Date(), 5);
console.log(x); console.log(x);
\ No newline at end of file
var x = new Date();
console.log(x);
x.setDate(-1);
console.log(x);
\ 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