Commit a3d232ce authored by Johannes Bill's avatar Johannes Bill

refactored

parent fd4c0558
...@@ -2,14 +2,12 @@ var utils = require('./timeUtils'); ...@@ -2,14 +2,12 @@ var utils = require('./timeUtils');
//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() + 100 * 24 * 3600000); var _epochEnd = new Date(_epochStart.getTime() + 60 * 24 * 3600000);
console.log("epochStart", _epochStart); console.log("epochStart", _epochStart);
console.log("epochEnd ", _epochEnd); console.log("epochEnd ", _epochEnd);
//test //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 c = [[new Date(2014, 5, 23, 13), new Date(2014, 6, 6, 10)]];
//var x = andNotOperator(a, b); //var x = andNotOperator(a, b);
//var y = andNotOperator(a, c); //var y = andNotOperator(a, c);
...@@ -19,8 +17,13 @@ console.log("epochEnd ", _epochEnd); ...@@ -19,8 +17,13 @@ console.log("epochEnd ", _epochEnd);
//console.log(workday); //console.log(workday);
var t = utils.cyclicFromTillDay(1, 5, -1, {h: 9, m: 0}, {h: 18, m: 29}, _epochStart, _epochEnd); var t1 = utils.cyclicFromTillDay(1, 1, 1, {h: 9, m: 0}, {h: 0, m: 0}, _epochStart, _epochEnd);
var t2 = utils.cyclicFromTillDay(2, 2, -1, {h: 9, m: 0}, {h: 18, m: 29}, _epochStart, _epochEnd); var t2 = utils.cyclicFromTillDay(2, 2, 1, {h: 0, m: 0}, {h: 18, m: 29}, _epochStart, _epochEnd);
var res = utils.andNotOperator(t, t2);
console.log(res);
\ No newline at end of file console.log(t1);
console.log(t2);
var t = utils.orOperator([t1, t2]);
console.log(t);
console.log(utils.mergeOverlaps(t));
//console.log(utils.orOperator([t,t2]));
\ No newline at end of file
function compareFT(from, till) {
if(from.h == till.h)
return from.m - till.m;
else
return from.h - till.h
}
function transformTill(from, till) {
if(compareFT(from, till) > 0) {
return {h: till.h + 24, m: till.m}
}
else return till;
}
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");
var resultArray = []; var resultArray = [];
...@@ -7,7 +21,7 @@ function _cyclicInterval(from, until, interval, epochStart, epochEnd) { ...@@ -7,7 +21,7 @@ function _cyclicInterval(from, until, interval, epochStart, epochEnd) {
return resultArray; return resultArray;
} }
function standard(day, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) { 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;
...@@ -21,7 +35,9 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) ...@@ -21,7 +35,9 @@ function standard(day, timeFrom, timeUntil, epochStart, epochEnd, distanceDays)
} }
function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) { function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) {
var firstDays = standard(dayFrom, timeFrom, timeUntil, epochStart, epochEnd, distanceDays); timeUntil = transformTill(timeFrom, timeUntil);
var firstDays = _standard(dayFrom, timeFrom, timeUntil, epochStart, epochEnd, distanceDays);
var result = firstDays.slice(); var result = firstDays.slice();
for(var offset = 1; offset <= dayTill - dayFrom; offset++) { for(var offset = 1; offset <= dayTill - dayFrom; offset++) {
...@@ -60,7 +76,9 @@ function cyclic(day, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) { ...@@ -60,7 +76,9 @@ function cyclic(day, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) {
} }
return result; return result;
} }
function cyclicFromTillDay(dayFrom, dayTill, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) { function cyclicFromTillDay(dayFrom, dayTill, noInMonth, timeFrom, timeUntil, epochStart, epochEnd) {
timeUntil = transformTill(timeFrom, timeUntil);
var result = []; var result = [];
epochStart = new Date(epochStart); epochStart = new Date(epochStart);
...@@ -154,13 +172,13 @@ function sortDateIntervals(intervals) { ...@@ -154,13 +172,13 @@ function sortDateIntervals(intervals) {
return intervals; return intervals;
} }
function removeOverlaps(interval) { function mergeOverlaps(interval) {
var i = 0; var i = 0;
while(i < interval.length - 1) { while(i < interval.length - 1) {
var endFirst = interval[i][1]; var endFirst = interval[i][1];
var startSecond = interval[i + 1][0]; var startSecond = interval[i + 1][0];
var endSecond = interval[i + 1][1]; var endSecond = interval[i + 1][1];
if(endFirst > startSecond) { if(endFirst >= startSecond) {
interval.splice(i + 1, 1); interval.splice(i + 1, 1);
interval[i][1] = endSecond; interval[i][1] = endSecond;
} }
...@@ -227,10 +245,10 @@ function andNotOperator(interval1, interval2) { ...@@ -227,10 +245,10 @@ function andNotOperator(interval1, interval2) {
return andOperator(interval1, interval2); return andOperator(interval1, interval2);
} }
module.exports.standard = standard;
module.exports.standardFromTillDay = standardFromTillDay; module.exports.standardFromTillDay = standardFromTillDay;
module.exports.cyclic = cyclic; module.exports.cyclic = cyclic;
module.exports.cyclicFromTillDay = cyclicFromTillDay; module.exports.cyclicFromTillDay = cyclicFromTillDay;
module.exports.orOperator = orOperator; module.exports.orOperator = orOperator;
module.exports.andOperator = andOperator; module.exports.andOperator = andOperator;
module.exports.andNotOperator = andNotOperator; module.exports.andNotOperator = andNotOperator;
module.exports.mergeOverlaps = mergeOverlaps;
\ 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