Skip to content

Commit c794569

Browse files
Added the getTimesAtAltitude method
1 parent 00372d0 commit c794569

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ Adds a custom time when the sun reaches the given angle to results returned by `
7979

8080
`SunCalc.times` property contains all currently defined times.
8181

82+
```javascript
83+
SunCalc.getTimesAtAltitude(/*Date*/ timeAndDate, /*Number*/ angleInDegrees, /*Number*/ latitude, /*Number*/ longitude)
84+
```
85+
86+
Returns an object containing the times at which the sun will reach the given altitude at the given
87+
location. The object contains the following properties (each is a `Date` object):
88+
89+
| Property | Description |
90+
| --------------- | ------------------------------------------------------------------------ |
91+
| `rise` | The time at which the sun will reach the altitude when it is rising |
92+
| `set` | The time at which the sun will reach the altitude when it is setting |
8293

8394
### Sun position
8495

suncalc.js

+24
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ SunCalc.getTimes = function (date, lat, lng) {
173173
return result;
174174
};
175175

176+
SunCalc.getTimesAtAltitude = function (date, altitude, lat, lng) {
177+
178+
var lw = rad * -lng,
179+
phi = rad * lat,
180+
181+
d = toDays(date),
182+
n = julianCycle(d, lw),
183+
ds = approxTransit(0, lw, n),
184+
185+
M = solarMeanAnomaly(ds),
186+
L = eclipticLongitude(M),
187+
dec = declination(L, 0),
188+
189+
Jnoon = solarTransitJ(ds, M, L),
190+
191+
Jset = getSetJ(altitude * rad, lw, phi, dec, n, M, L),
192+
Jrise = Jnoon - (Jset - Jnoon);
193+
194+
return {
195+
rise: fromJulian(Jrise),
196+
set: fromJulian(Jset),
197+
};
198+
};
199+
176200

177201
// moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
178202

test.js

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ t.test('getTimes returns sun phases for the given date and location', function (
4444
t.end();
4545
});
4646

47+
t.test('getTimesAtAltitude returns the correct time for the given date and location', function (t) {
48+
var times = SunCalc.getTimesAtAltitude(date, -6, lat, lng);
49+
50+
t.equal(new Date(testTimes.dawn).toUTCString(), times.rise.toUTCString());
51+
t.end();
52+
});
53+
4754
t.test('getMoonPosition returns moon position data given time and location', function (t) {
4855
var moonPos = SunCalc.getMoonPosition(date, lat, lng);
4956

0 commit comments

Comments
 (0)