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

operators finished

parent ed95e60c
//gibt zu betrachtenden Zeitraum an.
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) {
if(from >= until) throw new Error("startDate cant be greater than endDate");
......@@ -22,8 +25,9 @@ function _cyclicInterval(from, until, interval, epochStart, epochEnd) {
* @param timeUntil.m
* @param epochStart
* @param epochEnd
* @param distanceDays
*/
function standard(day, timeFrom, timeUntil, epochStart, epochEnd) {
function standard(day, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) {
var dayDiff = day - epochStart.getDay();
if(dayDiff < 0)
dayDiff += 7;
......@@ -32,8 +36,9 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd) {
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);
return _cyclicInterval(startDate, endDate, distanceDays * 24 * 3600000, epochStart, epochEnd);
}
/**
*
* @param dayFrom
......@@ -47,29 +52,111 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd) {
* @param epochStart {Date}
* @param epochEnd {Date}
* @returns {Array}
* @param distanceDays
*/
function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd) {
function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) {
var result = [];
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);
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);
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);
var currentNoInMonth = Math.floor(startDate.getDate() / 7) + 1;
while(currentNoInMonth != noInMonth) {
var currentNoInMonth = Math.ceil(startDate.getDate() / 7);
while(currentNoInMonth != _noInMonth) {
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;
}
......@@ -172,8 +259,8 @@ function andNotOperator(interval1, interval2) {
//var y = andNotOperator(a, c);
//console.log(y);
//var workday = standardFromTillDay(1, 5, {h: 9, m: 0}, {h: 11, m: 45}, _epochStart, _epochEnd);
//console.log(workday);
var workday = standardFromTillDay(1, 2, {h: 9, m: 0}, {h: 11, m: 45}, _epochStart, _epochEnd, 14);
console.log(workday);
var x = _getDateOccurenceInMonth(1, new Date(), 5);
console.log(x);
\ No newline at end of file
//var t = cyclicFromTillDay(1, 7, -1, {h: 9, m: 0}, {h: 18, m: 29}, _epochStart, _epochEnd);
//console.log(t);
\ No newline at end of file
var x = new Date();
var y = new Date(x);
y.setDate(1);
console.log(x);
x.setDate(-1);
console.log(x);
\ No newline at end of file
console.log(y);
\ 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