This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
114 lines (104 loc) · 2.91 KB
/
utils.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
exports.getFirstDayInWeek = (date, weekOffset) => {
const d = new Date(date);
const firstDate = new Date(
d.setDate(d.getDate() - d.getDay() + (d.getDay() == 0 ? -6:1) + weekOffset * 7)
);
firstDate.setUTCHours(0, 0, 0, 1);
console.debug("FirstDate is", firstDate.toISOString());
return firstDate;
};
exports.getLastDayInWeek = (date, weekOffset) => {
const d = new Date(date);
const firstDate = exports.getFirstDayInWeek(date, weekOffset);
let lastDate = new Date(firstDate);
lastDate.setDate(lastDate.getDate() + 6);
lastDate.setUTCHours(23, 59, 59, 0);
console.debug("LastDate is", lastDate.toISOString());
return lastDate;
};
exports.getFirstAndLastDayInWeek = (date, weekOffset) => {
console.debug("WeekOffset is", weekOffset);
console.debug("Date is", date.toISOString());
return {
firstDate: exports.getFirstDayInWeek(date, weekOffset),
lastDate: exports.getLastDayInWeek(date, weekOffset)
};
};
exports.getFirstAndLastDayInMonth = (date, monthOffset) => {
console.debug("MonthOffset is", monthOffset);
console.debug("Date is", date.toISOString());
let d = new Date(date);
d.setUTCMonth(d.getUTCMonth() + monthOffset);
let firstDay = new Date();
firstDay.setUTCFullYear(d.getUTCFullYear(), d.getUTCMonth(), 1);
firstDay.setUTCHours(0, 0, 0, 0);
let lastDay = new Date();
lastDay.setUTCFullYear(d.getUTCFullYear(), d.getUTCMonth() + 1, 1);
lastDay.setUTCHours(23, 59, 59, 0);
lastDay.setUTCDate(lastDay.getUTCDate() - 1);
return {
firstDate: firstDay,
lastDate: lastDay
};
};
exports.jsDateToXMLObject = date => {
return {
year: date.getFullYear(),
month: date.getMonth() + 1, // months are 0-based
day: date.getDate(), // days are 1-based
hour: date.getUTCHours(), // hours are correct
minute: date.getMinutes()
};
};
exports.XMLDateToJSDate = (year, month, day, hour, minute) => {
return exports.XMLDateObjectToJSDate({
year,
month,
day,
hour,
minute
});
};
exports.XMLDateObjectToJSDate = dateObj => {
return new Date(
parseInt(dateObj.year),
parseInt(dateObj.month) - 1,
parseInt(dateObj.day),
parseInt(dateObj.hour),
parseInt(dateObj.minute)
);
};
exports.injectXSLT_DTD = (xml, xslt, root, dtd) => {
let slimXML = "";
if (xml.indexOf("<?xml version") >= 0) {
slimXML = xml.substring(xml.indexOf("\n") + 1);
} else {
slimXML = xml;
}
return (
'<?xml version="1.0" encoding="UTF-8" ?>\n<!DOCTYPE ' +
root +
' SYSTEM "dtd/' +
dtd +
'">\n<?xml-stylesheet type="text/xsl" href="xsl/' +
xslt +
'" ?>\n' +
slimXML
);
};
exports.injectDTD = (xml, root, dtd) => {
let slimXML = "";
if (xml.indexOf("<?xml version") >= 0) {
slimXML = xml.substring(xml.indexOf("\n") + 1);
} else {
slimXML = xml;
}
return (
'<?xml version="1.0" encoding="UTF-8" ?>\n<!DOCTYPE ' +
root +
' SYSTEM "dtd/' +
dtd +
'">\n' +
slimXML
);
};