forked from opening-hours/opening_hours.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PH_SH_exporter.js
executable file
·136 lines (117 loc) · 4.52 KB
/
PH_SH_exporter.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
#!/usr/bin/env nodejs
/* Info, license and author {{{
* @license AGPLv3 <https://www.gnu.org/licenses/agpl-3.0.html>
* @author Copyright (C) 2015 Robin Schneider <[email protected]>
*
* Written for: https://github.com/anschuetz/linuxmuster/issues/1#issuecomment-110888829
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* }}} */
/* Required modules {{{ */
var opening_hours = require('./opening_hours.js');
var fs = require('fs');
/* }}} */
/* Constants {{{ */
var nominatim_object = require('./js/nominatim_definitions.js').for_loc;
/* }}} */
/* Parameter handling {{{ */
var optimist = require('optimist')
.usage('Usage: $0 export_list.conf')
.describe('h', 'Display the usage')
.describe('v', 'Verbose output')
.describe('f', 'From year (including)')
.demand('f')
.describe('t', 'Until year (including)')
.demand('t')
.describe('p', 'Export public holidays. Can not be used togehter with --school-holidays.')
// .default('p', true)
.describe('s', 'Export school holidays. Can not be used together with --public-holidays.')
.describe('c', 'Country (for which the holidays apply). Defaults to Germany.')
.default('c', 'de')
.describe('r', 'Region (for which the holidays apply). Defaults to Baden-Württemberg.')
.default('r', 'bw')
.boolean(['p', 's'])
.alias('h', 'help')
.alias('v', 'verbose')
.alias('f', 'from')
.alias('t', 'to')
.alias('p', 'public-holidays')
.alias('s', 'school-holidays')
.alias('c', 'country')
.alias('r', 'region');
var argv = optimist.argv;
if (argv.help || argv._.length === 0) {
optimist.showHelp();
process.exit(0);
}
/* Error handling {{{ */
if (argv['public-holidays'] && argv['school-holidays']) {
console.error("--school-holidays and --public-holidays can not be used together.");
process.exit(0);
}
if (typeof nominatim_object[argv.country] !== 'object' || typeof nominatim_object[argv.country][argv.region] !== 'object') {
console.error(argv.country + ", " + argv.region + " is currently not supported.");
process.exit(0);
}
/* }}} */
/* }}} */
var filepath = argv._[0];
var oh_value = argv['public-holidays'] ? 'PH' : 'SH';
write_config_file(filepath, oh_value, nominatim_object[argv.country][argv.region], new Date(argv.from, 0, 1), new Date(argv.to + 1, 0, 1));
function write_config_file(filepath, oh_value, nominatim_object, from_date, to_date) {
try {
oh = new opening_hours(oh_value, nominatim_object);
} catch (err) {
console.error('Something went wrong. Please file issue at https://github.com/ypid/opening_hours.js/issues');
process.exit(0);
}
var intervals = oh.getOpenIntervals(from_date, to_date);
var stream = fs.createWriteStream(filepath);
stream.once('open', function(fd) {
for (var i = 0; i < intervals.length; i++) {
var holiday_entry = intervals[i];
var output_line = [
getISODate(holiday_entry[0]),
];
if (oh_value === 'SH') { /* Add end date */
output_line[0] += '-' + getISODate(holiday_entry[1], -1);
}
output_line.push(holiday_entry[3]);
if (argv.verbose) {
console.log(output_line.join(' '))
}
stream.write(output_line.join(' ') + "\n");
}
stream.end();
});
}
/* Helper functions {{{ */
// https://stackoverflow.com/a/2998822
function pad(num, size) {
var s = String(num);
while (s.length < size) {
s = "0" + s;
}
return s;
}
function getISODate(date, day_offset) { /* Is a valid ISO 8601 date, but not so nice. */
/* Returns date as 20151231 */
if (typeof day_offset !== 'number') {
day_offset = 0;
}
date.setDate(date.getDate() + day_offset);
// Could use strftime node module but then I would have to make an own repository for this script :)
return String(date.getFullYear()) + pad(date.getMonth() + 1, 2) + pad(date.getDate(), 2);
}
/* }}} */