-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
280 lines (232 loc) · 9.2 KB
/
script.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
/*jslint es6 */
let locationData = {
latitude: 0,
longitude: 0
};
let riseSet = {
sunrise: "",
sunset: ""
};
let babylonianDay = {
lengthOfDayHour: "",
numberOfSecondsInDay: "",
dayHours: [],
nightHours: []
};
let locationError = false;
function round(value, decimals) {
"use strict";
return Number(Math.round(value + "e" + decimals) + "e-" + decimals);
}
let rad = 180 / Math.PI;
function sin(angle) {
return Math.sin(angle * rad);
}
function cos(angle) {
return Math.cos(angle * rad);
}
function calculateSunriseSunset(lat, long) {
console.log("Lat, long", lat, long);
// Pulled from https://en.wikipedia.org/wiki/Sunrise_equation
// Calculate current Julian Day
J_date = Math.floor((new Date().getTime() / 86400000) + 2440587.5);
n = J_date - 2451545.0 + 0.0008;
// Mean solar noon
J_star = n - long/360;
// Solar mean anomaly
M = (357.5291 + 0.98560028 * J_star) % 360;
// Equation of the Center
C = 1.9148*sin(M) + 0.0200*sin(2*M) + 0.0003*sin(3*M);
// Ecliptic longitude
L = (M + C + 180 + 102.9372) % 360;
// Solar transit
J_transit = 2451545 + J_star + 0.0053*sin(M) - 0.0069*sin(2*L);
// Declination of the Sun
D = sin(sin(L) * sin(23.44));
// Hour angle
w = cos((sin(-0.83) - sin(lat) * sin(D)) / (cos(lat) * cos(D)));
// Calculate sunrise and sunset
J_set = J_transit + (w / 360);
J_rise = J_transit - (w / 360);
}
function getGeoIp() {
"use strict";
let url = "https://freegeoip.app/json/";
fetch(url, {
headers: {}
})
.then(
function (response) {
if (response.status !== 200) {
console.log("Looks like there was a problem: " + response.status);
return;
}
//console.log(response);
response.json().then(function (data) {
console.log("Data for: " + data.city + ", " + data.country_name);
locationData.latitude = data.latitude;
locationData.longitude = data.longitude;
let d = new Date();
let date = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
getSunriseSunset(locationData.latitude, locationData.longitude, date);
});
}
)
.catch(function (err) {
console.log("Location Fetch Error", err);
locationError = true;
});
}
function getSunriseSunset(lat, long, date) {
let url = "https://api.sunrise-sunset.org/json";
url = url + "?lat=" + lat; //locationData.latitude;
url = url + "&lng=" + long; //locationData.longitude;
url = url + "&date=" + date;
url = url + "&formatted=0";
fetch(url, {
headers: {}
})
.then(
function (response) {
if (response.status !== 200) {
console.log("Looks like there was a problem: " + response.status);
return;
}
//console.log(response);
response.json().then(function (data) {
//console.log(data);
let sunrise = data.results.sunrise;
let sunset = data.results.sunset;
riseSet.sunrise = new Date(data.results.sunrise);
riseSet.sunset = new Date(data.results.sunset);
console.log("Sunrise: " + riseSet.sunrise);
console.log("Sunset: " + riseSet.sunset);
computeBabylonianTime();
});
}
)
.catch(function (err) {
console.log("Sunrise/Sunset Fetch Error", err);
});
}
function computeBabylonianTime() {
babylonianDay.lDay = (riseSet.sunset - riseSet.sunrise);
// Length of night is just number of milliseconds in a day minus length of daylight
babylonianDay.lNight = 1000 * 60 * 60 * 24 - babylonianDay.lDay;
babylonianDay.lDayHour = babylonianDay.lDay / 12;
babylonianDay.lNightHour = babylonianDay.lNight / 12;
let hour = riseSet.sunrise;
for (i = 0; i < 12; i++) {
let part = "BST";
if (i < 6) {
part = "ASR";
}
babylonianDay.dayHours[i] = {
realtime: hour,
hour: hour.getHours(),
minutes: hour.getMinutes(),
name: i.toString(),
// TODO: Move ush and gar up to babylonianDay, rather than in each hour
ush: Math.floor(babylonianDay.lDayHour / 240000),
gar: Math.floor(babylonianDay.lDayHour / 240000) * 60,
part: part
};
hour = new Date(hour.getTime() + babylonianDay.lDayHour);
}
hour = riseSet.sunset;
for (i = 0; i < 12; i++) {
let part = "BSR";
if (i < 6) {
part = "AST";
}
babylonianDay.nightHours[i] = {
realtime: hour,
hour: hour.getHours(),
minutes: hour.getMinutes(),
name: i.toString(),
ush: Math.floor(babylonianDay.lNightHour / 240000),
gar: Math.floor(babylonianDay.lNightHour / 240000) * 60,
part: part
};
hour = new Date(hour.getTime() + babylonianDay.lNightHour);
}
document.getElementById("lDayHour").innerHTML = round(babylonianDay.lDayHour / 60000, 2);
document.getElementById("lNightHour").innerHTML = round(babylonianDay.lNightHour / 60000, 2);
document.getElementById("numberOfUsh").innerHTML = babylonianDay.dayHours[0].ush;
document.getElementById("numberOfUsh2").innerHTML = babylonianDay.dayHours[0].ush;
document.getElementById("numberOfUsh3").innerHTML = babylonianDay.dayHours[0].ush;
console.log("Done setting up!");
}
function setup() {
getGeoIp();
}
setup();
window.onload = function () {
let hour = "00";
let ush = "00";
let gar = "00";
let part = "...";
window.setInterval(function () {
if (locationError === true) {
document.getElementById("error").innerHTML = "Location Error: Disable your Adblocker";
return;
}
// Get "now"
let d = new Date();
// Compute new times if we've reached the end of day or night
// We need to find out if it"s day or night
if (riseSet.sunrise - d < 0 && riseSet.sunset - d > 0) {
let daytime = true;
// Now we need to find the hour we"re in
for (i = 0; i < babylonianDay.dayHours.length; i++) {
if (d - babylonianDay.dayHours[i].realtime < babylonianDay.lDayHour) {
part = babylonianDay.dayHours[i].part;
ush = Math.floor((d - babylonianDay.dayHours[i].realtime) / (babylonianDay.lDayHour / babylonianDay.dayHours[i].ush));
gar = Math.floor((d - (babylonianDay.dayHours[i].realtime.getTime() + Math.floor(ush * babylonianDay.lDayHour / babylonianDay.dayHours[i].ush))) / (babylonianDay.lDayHour / babylonianDay.dayHours[i].gar))
hour = i;
if (hour < 10) {
hour = "0" + hour.toString();
}
if (ush < 10) {
ush = "0" + ush.toString();
}
if (gar < 10) {
gar = "0" + gar.toString();
}
break;
}
}
} else {
daytime = false;
// Now we need to find the hour we"re in
for (i = 0; i < babylonianDay.nightHours.length; i++) {
if (d - babylonianDay.nightHours[i].realtime < babylonianDay.lNightHour) {
part = babylonianDay.nightHours[i].part;
ush = Math.floor((d - babylonianDay.nightHours[i].realtime) / (babylonianDay.lNightHour / babylonianDay.nightHours[i].ush));
// (new Date() - (babylonianDay.dayHours[3].realtime.getTime() + Math.floor(ush * (babylonianDay.lDayHour / babylonianDay.dayHours[3].ush)))) / 4271
gar = Math.floor((d - (babylonianDay.nightHours[i].realtime.getTime() + Math.floor(ush * babylonianDay.lNightHour / babylonianDay.nightHours[i].ush))) / (babylonianDay.lNightHour / babylonianDay.nightHours[i].gar))
hour = i;
// Check if we're past midnight and have a negative ush
if (ush < 0) {
console.log("Need to grab yesterday's sunset information.");
getSunriseSunset(locationData.latitude, locationData.longitude, "yesterday")
}
if (hour < 10) {
hour = "0" + hour.toString();
}
if (ush < 10) {
ush = "0" + ush.toString();
}
if (gar < 10) {
gar = "0" + gar.toString();
}
break;
}
}
}
document.getElementById("hour").innerHTML = hour;
document.getElementById("ush").innerHTML = ush;
document.getElementById("gar").innerHTML = gar;
document.getElementById("part").innerHTML = part;
}, 500);
};