Commit 2704d42b authored by Johannes Bill's avatar Johannes Bill

parse json

parent ff194ce6
{
"0": {
"option": {
"0": "default"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"1": {
"option": {
"0": "default"
},
"dayoption": {
"0": "2"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "12:00"
},
"2": {
"option": {
"0": "default"
},
"dayoption": {
"0": "2"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "13:00",
"opening_to": "18:00"
},
"3": {
"option": {
"0": "default"
},
"dayoption": {
"0": "8"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"4": {
"option": {
"0": "repeatable"
},
"dayoption": {
"0": "3"
},
"repeatoption": {
"0": "3"
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"5": {
"option": {
"0": "repeatable"
},
"dayoption": {
"0": "4"
},
"repeatoption": {
"0": "14"
},
"holidayoption": {
"0": ""
},
"opening_day_from": "02.01",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"6": {
"option": {
"0": "season"
},
"dayoption": {
"0": "5"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "25.05",
"opening_day_to": "30.11",
"opening_from": "9:00",
"opening_to": "18:00"
},
"7": {
"option": {
"0": "holiday"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": "0"
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"8": {
"option": {
"0": "vacation"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "25.08",
"opening_day_to": "10.09",
"opening_from": "9:00",
"opening_to": "18:00"
}
}
\ No newline at end of file
var tmUtils = require('./timeUtils');
var now = new Date();
var _epochStart = new Date();
_epochStart.setDate(_epochStart.getDate() - 14);
var _epochEnd = new Date(_epochStart.getTime() + 60 * 24 * 3600000);
function calculateOpeningHours(json) {
var jsonIncl = json["include"];
for(var key in jsonIncl) {
var dateA = getOpeningHours(jsonIncl[key]);
console.log(key, dateA);
}
}
function getDaySpan(dayoption) {
if(dayoption < 7) return [dayoption, dayoption];
switch(dayoption) {
case 7:
return [0, 0];
case 8:
return [0, 7];
case 9:
return [1, 5];
case 10:
return [1, 6];
default:
throw "Invalid day option";
}
}
function parseTime(time) {
var split = time.split(":");
return {
h: parseInt(split[0]),
m: parseInt(split[1])
}
}
function parseDate(date) {
if(date.length == 0)
return null;
var year = now.getFullYear();
var split = date.split(".");
var month = parseInt(split[1]);
var day = parseInt(split[0]);
return new Date(year, month, day);
}
function getOpeningHours(json) {
var dayoption = parseInt(json["dayoption"]["0"]);
var daySpan = getDaySpan(dayoption);
var option = json["option"]["0"];
var startDate = parseDate(json["opening_day_from"]) || _epochStart;
var endDate = parseDate(json["opening_day_to"]) || _epochEnd;
var timeFrom = parseTime(json["opening_from"]);
var timeTill = parseTime(json["opening_to"]);
switch(option) {
case "default":
return tmUtils.standardFromTillDay(daySpan[0], daySpan[1], timeFrom, timeTill, _epochStart, _epochEnd, 7);
case "season":
return tmUtils.standardFromTillDay(daySpan[0], daySpan[1], timeFrom, timeTill, startDate, endDate, 7);
case "repeatable":
var repeatOption = parseInt(json["repeatoption"]["0"]);
var noInMonth;
if(repeatOption <= 4) noInMonth = repeatOption;
else if(repeatOption == 30) noInMonth = -1;
if(noInMonth)
return tmUtils.cyclicFromTillDay(daySpan[0], daySpan[1], noInMonth,timeFrom, timeTill, startDate, endDate);
switch (repeatOption) {
case 14:
case 21:
case 28:
return tmUtils.standardFromTillDay(daySpan[0], daySpan[1], timeFrom, timeTill, startDate, endDate, repeatOption);
default:
throw "invalid repeat option";
}
}
}
calculateOpeningHours(require('./testData/data.json'));
\ No newline at end of file
var utils = require('./timeUtils'); var tmUtils = 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);
...@@ -17,16 +17,16 @@ console.log("epochEnd ", _epochEnd); ...@@ -17,16 +17,16 @@ console.log("epochEnd ", _epochEnd);
//console.log(workday); //console.log(workday);
var t1 = utils.cyclicFromTillDay(1, 1, 1, {h: 9, m: 0}, {h: 0, m: 0}, _epochStart, _epochEnd); var t1 = tmUtils.cyclicFromTillDay(1, 1, 1, {h: 9, m: 0}, {h: 0, m: 0}, _epochStart, _epochEnd);
var t2 = utils.cyclicFromTillDay(2, 2, 1, {h: 0, m: 0}, {h: 18, m: 29}, _epochStart, _epochEnd); var t2 = tmUtils.cyclicFromTillDay(2, 2, 1, {h: 0, m: 0}, {h: 18, m: 29}, _epochStart, _epochEnd);
t1 = utils.standardFromTillDay(3,3, {h: 21, m: 0}, {h: 0, m: 0}, _epochStart, _epochEnd, 7); t1 = tmUtils.standardFromTillDay(3,3, {h: 21, m: 0}, {h: 0, m: 0}, _epochStart, _epochEnd, 7);
t2 = utils.standardFromTillDay(4,4, {h: 0, m: 0}, {h: 18, m: 0}, _epochStart, _epochEnd, 7); t2 = tmUtils.standardFromTillDay(4,4, {h: 0, m: 0}, {h: 18, m: 0}, _epochStart, _epochEnd, 7);
console.log(t1); console.log(t1);
console.log(t2); console.log(t2);
var t = utils.orOperator([t1, t2]); var t = tmUtils.orOperator([t1, t2, tmUtils.singleDay(new Date())]);
console.log(t); console.log(t);
console.log(utils.mergeOverlaps(t)); console.log(tmUtils.mergeOverlaps(t));
//console.log(utils.orOperator([t,t2])); //console.log(tmUtils.orOperator([t,t2]));
\ No newline at end of file \ No newline at end of file
{
"include": {
"0": {
"option": {
"0": "default"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"1": {
"option": {
"0": "default"
},
"dayoption": {
"0": "2"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "12:00"
},
"2": {
"option": {
"0": "default"
},
"dayoption": {
"0": "2"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "13:00",
"opening_to": "18:00"
},
"3": {
"option": {
"0": "default"
},
"dayoption": {
"0": "8"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"4": {
"option": {
"0": "repeatable"
},
"dayoption": {
"0": "3"
},
"repeatoption": {
"0": "3"
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"5": {
"option": {
"0": "repeatable"
},
"dayoption": {
"0": "4"
},
"repeatoption": {
"0": "14"
},
"holidayoption": {
"0": ""
},
"opening_day_from": "02.01",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"6": {
"option": {
"0": "season"
},
"dayoption": {
"0": "5"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "25.05",
"opening_day_to": "30.11",
"opening_from": "9:00",
"opening_to": "18:00"
},
"7": {
"option": {
"0": "holiday"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": "0"
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
},
"8": {
"option": {
"0": "vacation"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "25.08",
"opening_day_to": "10.09",
"opening_from": "9:00",
"opening_to": "18:00"
}
},
"exclude": {
"0": {
"option": {
"0": "default"
},
"dayoption": {
"0": "0"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "01.01",
"opening_day_to": "",
"opening_from": "",
"opening_to": ""
},
"1": {
"option": {
"0": "repeatable"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": "3"
},
"holidayoption": {
"0": ""
},
"opening_day_from": "01.01",
"opening_day_to": "",
"opening_from": "",
"opening_to": ""
},
"2": {
"option": {
"0": "season"
},
"dayoption": {
"0": "8"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "01.01",
"opening_day_to": "01.02",
"opening_from": "",
"opening_to": ""
},
"3": {
"option": {
"0": "holiday"
},
"dayoption": {
"0": "0"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": "121"
},
"opening_day_from": "01.01",
"opening_day_to": "",
"opening_from": "",
"opening_to": ""
},
"4": {
"option": {
"0": "holiday"
},
"dayoption": {
"0": "0"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": "122"
},
"opening_day_from": "01.01",
"opening_day_to": "",
"opening_from": "",
"opening_to": ""
},
"5": {
"option": {
"0": "holiday"
},
"dayoption": {
"0": "0"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": "123"
},
"opening_day_from": "01.01",
"opening_day_to": "",
"opening_from": "",
"opening_to": ""
},
"6": {
"option": {
"0": "default"
},
"dayoption": {
"0": "0"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "01.01",
"opening_day_to": "",
"opening_from": "",
"opening_to": ""
},
"7": {
"option": {
"0": "vacation"
},
"dayoption": {
"0": "0"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "01.10",
"opening_day_to": "09.10",
"opening_from": "",
"opening_to": ""
}
}
}
This diff is collapsed.
...@@ -34,10 +34,10 @@ function _standard(day, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) ...@@ -34,10 +34,10 @@ function _standard(day, timeFrom, timeUntil, epochStart, epochEnd, distanceDays)
return _cyclicInterval(startDate, endDate, distanceDays * 24 * 3600000, epochStart, epochEnd); return _cyclicInterval(startDate, endDate, distanceDays * 24 * 3600000, epochStart, epochEnd);
} }
function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd, distanceDays) { function standardFromTillDay(dayFrom, dayTill, timeFrom, timeUntil, epochStart, epochEnd, daysDistance) {
timeUntil = transformTill(timeFrom, timeUntil); timeUntil = transformTill(timeFrom, timeUntil);
var firstDays = _standard(dayFrom, timeFrom, timeUntil, epochStart, epochEnd, distanceDays); var firstDays = _standard(dayFrom, timeFrom, timeUntil, epochStart, epochEnd, daysDistance);
var result = firstDays.slice(); var result = firstDays.slice();
for(var offset = 1; offset <= dayTill - dayFrom; offset++) { for(var offset = 1; offset <= dayTill - dayFrom; offset++) {
...@@ -155,6 +155,13 @@ function _getNextNoInMonthOccurence(day, startDate, noInMonth) { ...@@ -155,6 +155,13 @@ function _getNextNoInMonthOccurence(day, startDate, noInMonth) {
return startDate; return startDate;
} }
function singleDay(date) {
var startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 1);
return [[startDate, endDate]];
}
function orOperator(intervals) { function orOperator(intervals) {
var result = []; var result = [];
...@@ -246,8 +253,8 @@ function andNotOperator(interval1, interval2) { ...@@ -246,8 +253,8 @@ function andNotOperator(interval1, interval2) {
} }
module.exports.standardFromTillDay = standardFromTillDay; module.exports.standardFromTillDay = standardFromTillDay;
module.exports.cyclic = cyclic;
module.exports.cyclicFromTillDay = cyclicFromTillDay; module.exports.cyclicFromTillDay = cyclicFromTillDay;
module.exports.singleDay = singleDay;
module.exports.orOperator = orOperator; module.exports.orOperator = orOperator;
module.exports.andOperator = andOperator; module.exports.andOperator = andOperator;
module.exports.andNotOperator = andNotOperator; module.exports.andNotOperator = andNotOperator;
......
endzeit kleiner anfangszeit ==> endzeit + 24 feiertage defaultmaessig abziehen
\ No newline at end of file
verbindung feiertag - region
\ 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