-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
156 lines (131 loc) · 4.01 KB
/
common.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
/*
Fōrmulæ time package. Common definitions.
Copyright (C) 2015-2023 Laurence R. Ugalde
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, either version 3 of the
License, or (at your option) any later version.
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/>.
*/
'use strict';
export class Common {};
Common.monthTags = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
Common.mapMonthTags = {
"January" : 1,
"February" : 2,
"March" : 3,
"April" : 4,
"May" : 5,
"June" : 6,
"July" : 7,
"August" : 8,
"September" : 9,
"October" : 10,
"November" : 11,
"December" : 12
};
Common.weekDayTags = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
Common.mapWeekDayTags = {
"Sun": 0,
"Mon": 1,
"Tue": 2,
"Wed": 3,
"Thu": 4,
"Fri": 5,
"Sat": 6
};
Common.createMillis = (year, month, day, hour, minute, second, millisecond, timeZone) => {
if (year == 0) {
throw "There was no zero year";
}
if (year < 0) {
++year;
}
let originalYear = year < 1500 ? year : null;
if (originalYear != null) year = 1500;
let date = new Date();
date.setUTCFullYear(year);
date.setUTCMonth(month - 1);
date.setUTCDate(day);
date.setUTCHours(hour);
date.setUTCMinutes(minute);
date.setUTCSeconds(second);
date.setUTCMilliseconds(millisecond);
date.setUTCFullYear(year);
let utcDate = new Date(date.toLocaleString("en-US", { timeZone: "UTC" }));
let tzDate;
try {
tzDate = new Date(date.toLocaleString("en-US", { timeZone: timeZone }));
}
catch (error) {
throw "Invalid time zone";
}
let offset = utcDate.getTime() - tzDate.getTime();
if (originalYear !== null) {
date = new Date(date.setUTCFullYear(originalYear));
}
return date.getTime() + offset;
};
Common.getComponents = (millis, timeZone) => {
let str;
try {
str = new Date(millis).toLocaleString(
"en-US",
{
era: "short",
weekday: "short",
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
hour12: false,
minute: "2-digit",
second: "2-digit",
fractionalSecondDigits: 3,
timeZone: timeZone,
timeZoneName: "longOffset"
}
);
}
catch (error) {
throw "Invalid time zone";
}
// console.log(str);
// example 'Tue, 09 26, 2023 AD, 24:35:14.517 GMT-06:00'
let tokens = str.split(/[ ,:.]+/);
let year = Number(tokens[3]);
if (tokens[4] === "BC") year = -year;
let offset = 0;
if (tokens.length >= 11) {
offset = 60 * Number(tokens[9].substring(4)) + Number(tokens[10]);
if (tokens[9].charAt(3) === "-") offset = -offset;
}
return {
year: year,
month: Number(tokens[1]),
day: Number(tokens[2]),
weekDay: Common.mapWeekDayTags[tokens[0]],
hour: Number(tokens[5]) % 24,
minute: Number(tokens[6]),
second: Number(tokens[7]),
millisecond: Number(tokens[8]),
offset: offset
};
};
Common.inDaylightSavingTime = (millis, timeZone) => {
let components = Common.getComponents(millis, timeZone);
let januaryComponents = Common.getComponents(
Common.createMillis(components.year, 1, 1, 0, 0, 0, 0, timeZone, 0),
timeZone
);
let juneComponents = Common.getComponents(
Common.createMillis(components.year, 7, 1, 0, 0, 0, 0, timeZone, 0),
timeZone
);
return Math.min(januaryComponents.offset, juneComponents.offset) !== components.offset;
};