-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (164 loc) · 5.84 KB
/
index.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
const regex = {
dateRegexDotde: /^\d{2}\.\d{2}\.\d{4}$/,
dateRegexde: /\b(\d{1,2})\.(\d{1,2})\.(\d{4})\b/,
dateRegexDotdeLazy: /^\d{1,2}.\d{1,2}.\d{2,4}$/,
dateRegexDotLazyDayMonth: /^\d{1}$/,
dateRegexDotLazyYear: /^\d{2}$/,
dateRegexdeShort: /\b(\d{1,2})\.(\d{1,2})\.(\d{2})\b/,
dateRegexSlash: /^\d{2}\/\d{2}\/\d{4}$/,
dateRegexMinus: /^\d{2}-\d{2}-\d{4}$/,
isoDateRegex: /^(\d{4})-(\d{2})-(\d{2}).*$/,
zuluTimeRegex: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
}
module.exports = class {
/**
* Set country for time Requests
* @param {string} [country] - The country code ('us' for United States, 'de' for Germany). Default is 'de'.
*/
constructor(country){
this.country = country || 'de'
}
/**
* Get the current Zulu time (UTC) with an optional country-specific offset.
* @returns {Date} The current Zulu time.
*/
zuluTimeNow = () => {
try {
let utcDate
switch (this.country) {
case 'us':
utcDate = new Date()
break;
default: //de
const bTimeZone = 'Europe/Berlin';
const bDate = new Date().toLocaleString('en-US', { timeZone: bTimeZone });
utcDate = new Date(bDate);
}
const offset = utcDate.getTimezoneOffset()
const absOffset = Math.abs(offset) / 60
const newTime = offset < 0
? utcDate.getTime() + absOffset * 60 * 60 * 1000
: utcDate.getTime() - absOffset * 60 * 60 * 1000
return new Date(newTime)
} catch (error) {
console.error(error)
return new Date()
}
}
/** convertToISOFill
* @param {string} dateString - The input parameters.
*/
convertToISOFill = (dateString) => {//de
try {
/**
* @typedef {Object} ConversionResult
* @property {string|false} valDateString - The converted date string in ISO format ('yyyy-mm-dd'),
* or false if the input date is invalid.
* @property {Date|false} dateStringISO - The Date object representing the ISO date,
* or false if the input date is invalid.
*/
/** @type {ConversionResult} */
const res = { valDateString: false, dateStringISO: false }
if (regex.dateRegexDotdeLazy.test(dateString)) {
const parts = dateString.split('.')
let day = regex.dateRegexDotLazyDayMonth.test(parts[0]) ? '0' + parts[0] : parts[0]
let month = regex.dateRegexDotLazyDayMonth.test(parts[1]) ? '0' + parts[1] : parts[1]
const year = regex.dateRegexDotLazyYear.test(parts[2]) ? '20' + parts[2] : parts[2]
if (parseInt(month) > 12) {
[day, month] = [month, day]
}
dateString = `${day}.${month}.${year}`
}
if (regex.dateRegexDotde.test(dateString)) {
const newDateString = new Date(dateString.split('.').reverse().join('-'))
if (this.isValidISODate(newDateString)) {
res.valDateString = dateString
res.dateStringISO = newDateString
} else {
res.valDateString = false
res.dateStringISO = false
}
}
return res
} catch (error) {
console.error(error)
return { valDateString: false, dateStringISO: false }
}
}
isValidISODate(date) {
return date instanceof Date && !isNaN(date)
}
/** isValidDateFormat
* @param {string} dateString - The input parameters.
*/
isValidDateFormat = (dateString) => {
switch (this.country) {
case 'us':
break;
default: //de
return regex.dateRegexDotde.test(dateString)
}
}
/** isValidDateFormat
* @param {string} dateString - The input parameters.
*/
convertToZulu = (dateString) => {
if (regex.zuluTimeRegex.test(dateString)) return dateString
switch (this.country) {
case 'us':
break;
default: //de
const { dateStringISO } = this.convertToISOFill(dateString)
return dateStringISO
}
}
/** isValidDateFormat
* @param {string} dateString - The input parameters.
*/
convertFromZulu = (dateString) => {
try {
const date = new Date(dateString)
const day = date.getUTCDate()
const month = date.getUTCMonth() + 1
const year = date.getUTCFullYear()
const dayFormatted = ('0' + day).slice(-2)
const monthFormatted = ('0' + month).slice(-2)
switch (this.country) {
case 'us':
break;
default: //de
return `${dayFormatted}.${monthFormatted}.${year}`
}
} catch (error) {
console.error(error)
}
}
/**
* Calculate a date in the past relative to the current Zulu time (UTC).
* @param {{ seconds?: number, minutes?: number, days?: number, months?: number }} input - An object specifying the time difference.
* @returns {Date} The calculated date in the past.
*/
dbRefTimeAgo(input) {
try {
const { seconds = 0, minutes = 0, days = 0, months = 0 } = input;
const now = this.zuluTimeNow();
let timeAgo = new Date(now);
if (months) {
timeAgo.setMonth(now.getMonth() - months);
}
if (days) {
timeAgo = new Date(timeAgo.getTime() - days * 24 * 60 * 60 * 1000);
}
if (minutes) {
timeAgo = new Date(timeAgo.getTime() - minutes * 60 * 1000);
}
if (seconds) {
timeAgo = new Date(timeAgo.getTime() - seconds * 1000);
}
return timeAgo;
} catch (error) {
console.error(error)
return new Date();
}
}
}