-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprazniki.js
349 lines (278 loc) · 12.4 KB
/
prazniki.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
var myAppName=__filename;
var app_host = process.env.APP_HOST;
var app_port = process.env.APP_PORT;
// if environment variables are not present then assume its development mode
var host = app_host ? app_host : '127.0.0.1';
var port = app_port ? app_port : 8007;
var myURL = 'http://' + host + ':' + port;
// counters
var apiRequests=0;
var counterGet=0;
var counterPutPost=0;
var counterDelete=0;
// set to 1 to enable debug messages
var debug = 1;
require("console-stamp")(console, "yyyy-mm-dd HH:MM:ss.l");
console.info('PID: ' + process.pid)
console.info('Application name: ' + myAppName)
var express = require('express');
var app = express();
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', myURL);
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
// Log request and populate counters
app.use(function (req, res, next) {
apiRequests++;
if (req.method == 'GET') {
counterGet++;
} else if ( (req.method == 'POST') || (req.method == 'PUT') ) {
counterPutPost++;
} else if ( req.method == 'DELETE' ) {
counterDelete++;
}
req.mylogger = req.method + ' ' + req.url + ' ' + JSON.stringify(req.params) + ' ' + JSON.stringify(req.signedCookies);
console.log(req.mylogger);
next();
});
var mydebug = function(message) {
if (debug) {
console.debug(message);
}
};
var getEasterDate = function(year) {
var a = year % 19;
var b = Math.floor(year / 100);
var c = year % 100;
var d = Math.floor(b / 4);
var e = b % 4;
var f = Math.floor((b + 8) / 25);
var g = Math.floor((b - f + 1) / 3);
var h = (19 * a + b - d - g + 15) % 30;
var i = Math.floor(c / 4);
var k = c % 4;
var l = (32 + 2 * e + 2 * i - h - k) % 7;
var m = Math.floor((a + 11 * h + 22 * l) / 451);
var n0 = (h + l + 7 * m + 114)
var n = Math.floor(n0 / 31) - 1;
var p = n0 % 31 + 1;
var date = new Date(year,n,p);
return date;
}
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
var getTariff2024 = function(isBusinessDay) {
const currentHour = new Date().getHours();
const currentDayOfWeek = new Date().getDay(); // 0 (Sunday) - 6 (Saturday)
const currentMonth = new Date().getMonth() + 1; // 0-indexed, so we add 1
const isHighSeason = [11, 12, 1, 2].includes(currentMonth);
const isLowSeason = [3, 4, 5, 6, 7, 8, 9, 10].includes(currentMonth);
//const isWorkingDay = currentDayOfWeek >= 1 && currentDayOfWeek <= 5; // 1 to 5 represent Monday to Friday
const isWeekend = currentDayOfWeek === 0 || currentDayOfWeek === 6; // 0 and 6 represent Sunday and Saturday
let tariff;
if (currentHour >= 0 && currentHour < 6) { tariff = isHighSeason && isBusinessDay ? 3 : (isLowSeason && isWeekend ? 5 : 4); }
else if (currentHour == 6) { tariff = isHighSeason && isBusinessDay ? 2 : (isLowSeason && isWeekend ? 4 : 3); }
else if (currentHour > 6 && currentHour < 14) { tariff = isHighSeason && isBusinessDay ? 1 : (isLowSeason && isWeekend ? 3 : 2); }
else if (currentHour > 13 && currentHour < 16) { tariff = isHighSeason && isBusinessDay ? 2 : (isLowSeason && isWeekend ? 4 : 3); }
else if (currentHour > 15 && currentHour < 20) { tariff = isHighSeason && isBusinessDay ? 1 : (isLowSeason && isWeekend ? 3 : 2); }
else if (currentHour > 19 && currentHour < 22) { tariff = isHighSeason && isBusinessDay ? 2 : (isLowSeason && isWeekend ? 4 : 3); }
else if (currentHour > 21 && currentHour < 24) { tariff = isHighSeason && isBusinessDay ? 3 : (isLowSeason && isWeekend ? 5 : 4); }
else { tariff = 0; }
return tariff;
}
var getDateInfo = function(dateInput) {
var resp = {};
var date = new Date(dateInput);
if (date == "Invalid Date") {
resp['error']=date;
return resp;
}
if (date.getFullYear() < 2013) {
resp['error'] = "Samo datumi od 1.1.2013 dalje";
return resp;
}
var isHoliday=false;
var holidayString = "";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var weekday = date.getDay();
if (weekday == 0) { weekday = 7; }
// SUNDAY OR SATURDAY
var isWeekend = (weekday > 5);
var isBusinessDay = ! isWeekend;
// EASTER SUNDAY
var easterDate = getEasterDate(year);
var ed1 = easterDate.getDate();
var em1 = easterDate.getMonth() + 1;
// EASTER MONDAY
var easterMonday = easterDate.addDays(1);
var ed2 = easterMonday.getDate();
var em2 = easterMonday.getMonth() + 1;
// BINKOSTNA NEDELJA = (EASTER SUNDAY + 49) - 50 DAN PO VELIKI NOCI
var binkost1 = easterDate.addDays(49);
var bd1 = binkost1.getDate();
var bm1 = binkost1.getMonth() + 1;
// BINKOSTNI PONEDELJEK
var binkost2 = easterDate.addDays(50);
var bd2 = binkost2.getDate();
var bm2 = binkost2.getMonth() + 1;
var isLeapYear = new Date(year, 1, 29).getMonth() == 1
if ( (month==1) && (day==1) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Novo leto"; }
else if ( (month==1) && (day==2) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Novo leto"; }
else if ( (month==2) && (day==8) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Prešernov dan"; }
else if ( (month==em1) && (day==ed1) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Velika noč"; }
else if ( (month==em2) && (day==ed2) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Velikončni ponedeljek"; }
else if ( (month==4) && (day==27) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Dan boja proti okupatorju"; }
else if ( (month==5) && (day==1) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Praznik dela"; }
else if ( (month==5) && (day==2) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Praznik dela"; }
else if ( (month==bm1) && (day==bd1) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Binkoštna nedelja"; }
else if ( (month==bm2) && (day==bd2) ) { isHoliday=true; isBusinessDay=isBusinessDay && true; holidayString = "Binkoštni ponedeljek"; }
else if ( (month==6) && (day==8) ) { isHoliday=true; isBusinessDay=isBusinessDay && true; holidayString = "Dan Primoža Trubarja";}
else if ( (month==6) && (day==25) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Dan državnosti"; }
else if ( (month==8) && (day==15) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Marijino vnebovzetje"; }
else if ( (month==8) && (day==17) ) { isHoliday=true; isBusinessDay=isBusinessDay && true; holidayString = "združitev prekmurskih Slovencev z matičnim narodom";}
else if ( (month==9) && (day==15) ) { isHoliday=true; isBusinessDay=isBusinessDay && true; holidayString = "vrnitev Primorske k matični domovini";}
else if ( (month==10) && (day==25) ) { isHoliday=true; isBusinessDay=isBusinessDay && true; holidayString = "Dan suverenosti"; }
else if ( (month==10) && (day==31) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Dan reformacije"; }
else if ( (month==11) && (day==1) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Dan spomina na mrtve"; }
else if ( (month==11) && (day==23) ) { isHoliday=true; isBusinessDay=isBusinessDay && true; holidayString = "Dan Rudolfa Maistra"; }
else if ( (month==12) && (day==25) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Božič"; }
else if ( (month==12) && (day==26) ) { isHoliday=true; isBusinessDay=isBusinessDay && false; holidayString = "Dan samostojnosti in enotnosti"; }
resp['datum']=date.getDate() + '.' + (date.getMonth()+1) + '.' + date.getFullYear();
resp['holiday']=isHoliday;
resp['businessday']=isBusinessDay;
resp['weekend']=isWeekend;
resp['weekday']=weekday;
resp['leapyear']=isLeapYear;
if (holidayString.length > 0) {
resp['holiday_name']=holidayString;
}
return resp;
};
app.get('/infonow', function(req, res) {
var resp = {};
var date = new Date();
resp = getDateInfo(date);
res.send(resp);
});
app.get('/getTime/:cmd', function(req, res) {
var resp = {};
var date = new Date();
var cmd = req.params.cmd;
if (cmd == 'epoch') { resp = date.getTime() / 1000; }
else if (cmd == 'local') { resp = date.getDate() + '.' + (date.getMonth()+1) + '.' + date.getFullYear() + ' ' + date.toTimeString(); }
else if (cmd == 'utc') { resp = date.toUTCString(); }
res.send('' + resp);
return;
});
app.get('/:cmd/now', function(req, res) {
var cmd = req.params.cmd;
var resp={};
var date = new Date();
resp = getDateInfo(date);
if (cmd == 'info') {
res.send(resp);
} else if (cmd == 'tariff') {
resp[cmd] = 'MT';
// VT every business day between 6:00 and 22:00
if ( ( resp['businessday'] == true ) && ( (date.getHours() > 5) || (date.getHours() < 22) ) ) {
resp[cmd] = 'VT';
}
res.send(resp[cmd]);
return;
} else if (cmd == 'tariff2024') {
var isBusinessDay = resp['businessday'];
resp[cmd] = getTariff2024(isBusinessDay);
res.send(resp);
return;
} else {
if (typeof resp[cmd] === 'undefined') {
res.send('ERROR: ' + cmd + ' undefined');
return;
}
res.send('' + resp[cmd]);
return;
}
});
app.get('/:cmd/:year(\\d+)/:month(\\d+)/:day(\\d+)', function(req, res) {
var year = req.params.year;
var month = req.params.month;
var day = req.params.day;
var cmd = req.params.cmd;
var resp = {};
var date = new Date(year, month-1, day);
resp = getDateInfo(date);
if (cmd == 'info') {
res.send(resp);
} else {
if (typeof resp[cmd] === 'undefined') {
res.send('ERROR: ' + cmd + ' undefined');
return;
}
res.send('' + resp[cmd]);
return;
}
});
/*
app.get('/businessday/:year(\\d+)/:month(\\d+)/:day(\\d+)', function(req, res) {
var year = req.params.year;
var month = req.params.month;
var day = req.params.day;
var resp = {};
var date = new Date(year, month-1, day);
resp = getDateInfo(date);
res.send(resp['businessday']);
});
*/
// get some stats
var printStatistics = function() {
console.info('API requests: ' + apiRequests);
console.info('GET requests: ' + counterGet);
console.info('PUT/POST requests: ' + counterPutPost);
console.info('DELETE requests: ' + counterDelete);
};
var signalHandler = function() {
console.log('We-should-cleanup signal catched.. shutting down');
console.log('closing application socket');
try {
server.close();
}
catch(e) {
console.error('server.close() error: ' + e);
}
console.log('Statistics');
printStatistics();
}
var server = app.listen(port, host, function() {
console.log('Listening at http://%s:%s', host, port)
});
// catch some signals
//process.on('exit', signalHandler);
process.on('SIGINT', signalHandler);
process.on('SIGHUP', signalHandler);
process.on('SIGTERM', signalHandler);
// process.on('SIGUSR2', printStatistics);
// uncomment this if you use nodemon
process.on('SIGUSR2', signalHandler);
process.on('uncaughtException', function(err) {
if (err.code == 'EADDRINUSE') {
console.error('unable to listen on %s:%s , %s', host, port, err);
} else {
console.error(err);
}
});