-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsun.js
More file actions
211 lines (174 loc) · 5.87 KB
/
sun.js
File metadata and controls
211 lines (174 loc) · 5.87 KB
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
/**
*
* Modified by Hollow Man so that the specified time is always between sunrise and sunset time.
*
* Copyright © 2020 hollowman6 from Lanzhou University (兰州大学).
*
*/
/**
*
* Sunrise/sunset script. By Matt Kane.
*
* Based loosely and indirectly on Kevin Boone's SunTimes Java implementation
* of the US Naval Observatory's algorithm.
*
* Copyright © 2012 Triggertrap Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA,
* or connect to: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
*/
Date.prototype.sunrise = function (latitude, longitude, zenith) {
return this.sunriseSet(latitude, longitude, true, zenith);
};
Date.prototype.sunset = function (latitude, longitude, zenith) {
return this.sunriseSet(latitude, longitude, false, zenith);
};
Date.prototype.sunriseSet = function (latitude, longitude, sunrise, zenith) {
if (!zenith) {
zenith = 90.8333;
}
var hoursFromMeridian = longitude / Date.DEGREES_PER_HOUR,
dayOfYear = this.getDayOfYear(),
approxTimeOfEventInDays,
sunMeanAnomaly,
sunTrueLongitude,
ascension,
rightAscension,
lQuadrant,
raQuadrant,
sinDec,
cosDec,
localHourAngle,
localHour,
localMeanTime,
time;
if (sunrise) {
approxTimeOfEventInDays = dayOfYear + (6 - hoursFromMeridian) / 24;
} else {
approxTimeOfEventInDays = dayOfYear + (18.0 - hoursFromMeridian) / 24;
}
sunMeanAnomaly = 0.9856 * approxTimeOfEventInDays - 3.289;
sunTrueLongitude =
sunMeanAnomaly +
1.916 * Math.sinDeg(sunMeanAnomaly) +
0.02 * Math.sinDeg(2 * sunMeanAnomaly) +
282.634;
sunTrueLongitude = Math.mod(sunTrueLongitude, 360);
ascension = 0.91764 * Math.tanDeg(sunTrueLongitude);
rightAscension = (360 / (2 * Math.PI)) * Math.atan(ascension);
rightAscension = Math.mod(rightAscension, 360);
lQuadrant = Math.floor(sunTrueLongitude / 90) * 90;
raQuadrant = Math.floor(rightAscension / 90) * 90;
rightAscension = rightAscension + (lQuadrant - raQuadrant);
rightAscension /= Date.DEGREES_PER_HOUR;
sinDec = 0.39782 * Math.sinDeg(sunTrueLongitude);
cosDec = Math.cosDeg(Math.asinDeg(sinDec));
var cosLocalHourAngle =
(Math.cosDeg(zenith) - sinDec * Math.sinDeg(latitude)) /
(cosDec * Math.cosDeg(latitude));
localHourAngle = Math.acosDeg(cosLocalHourAngle);
if (sunrise) {
localHourAngle = 360 - localHourAngle;
}
localHour = localHourAngle / Date.DEGREES_PER_HOUR;
localMeanTime =
localHour + rightAscension - 0.06571 * approxTimeOfEventInDays - 6.622;
time = localMeanTime - longitude / Date.DEGREES_PER_HOUR;
time = Math.mod(time, 24);
var localHourAngleReverse = 360 - localHourAngle;
var localHourReverse = localHourAngleReverse / Date.DEGREES_PER_HOUR;
var localMeanTimeReverse =
localHourReverse +
rightAscension -
0.06571 * approxTimeOfEventInDays -
6.622;
var timeReverse = localMeanTimeReverse - longitude / Date.DEGREES_PER_HOUR;
timeReverse = Math.mod(timeReverse, 24);
var midnight = new Date(0);
midnight.setUTCFullYear(this.getUTCFullYear());
midnight.setUTCMonth(this.getUTCMonth());
midnight.setUTCDate(this.getUTCDate());
var milli = midnight.getTime() + time * 60 * 60 * 1000;
var caculatedTime = new Date(milli);
var caculatedTimeReverse = new Date(
midnight.getTime() + timeReverse * 60 * 60 * 1000
);
var caculatedTimeTest = caculatedTime;
var caculatedTimeReverseTest = caculatedTimeReverse;
for (var i = 0; i < 3; i++) {
if (
(caculatedTimeTest > this && this > caculatedTimeReverseTest) ||
(caculatedTimeTest < this && this < caculatedTimeReverseTest)
) {
caculatedTime = caculatedTimeTest;
break;
} else if (i == 0) {
caculatedTimeTest = new Date(
caculatedTime.getTime() - 24 * 60 * 60 * 1000
);
if (
Math.abs(
caculatedTimeReverseTest.getTime() - caculatedTimeTest.getTime()
) >
24 * 60 * 60 * 1000
) {
caculatedTimeTest = caculatedTime;
}
} else if (i == 1) {
caculatedTimeTest = new Date(
caculatedTime.getTime() + 24 * 60 * 60 * 1000
);
if (
Math.abs(
caculatedTimeReverseTest.getTime() - caculatedTimeTest.getTime()
) >
24 * 60 * 60 * 1000
) {
caculatedTimeTest = caculatedTime;
}
}
}
return caculatedTime;
};
Date.DEGREES_PER_HOUR = 360 / 24;
// Utility functions
Date.prototype.getDayOfYear = function () {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((this - onejan) / 86400000);
};
Math.degToRad = function (num) {
return (num * Math.PI) / 180;
};
Math.radToDeg = function (radians) {
return (radians * 180.0) / Math.PI;
};
Math.sinDeg = function (deg) {
return Math.sin((deg * 2.0 * Math.PI) / 360.0);
};
Math.acosDeg = function (x) {
return (Math.acos(x) * 360.0) / (2 * Math.PI);
};
Math.asinDeg = function (x) {
return (Math.asin(x) * 360.0) / (2 * Math.PI);
};
Math.tanDeg = function (deg) {
return Math.tan((deg * 2.0 * Math.PI) / 360.0);
};
Math.cosDeg = function (deg) {
return Math.cos((deg * 2.0 * Math.PI) / 360.0);
};
Math.mod = function (a, b) {
var result = a % b;
if (result < 0) {
result += b;
}
return result;
};