Skip to content

Commit

Permalink
Added the getTimesAtAltitude method
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasKunnen committed Jun 26, 2019
1 parent 00372d0 commit c794569
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ Adds a custom time when the sun reaches the given angle to results returned by `

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

```javascript
SunCalc.getTimesAtAltitude(/*Date*/ timeAndDate, /*Number*/ angleInDegrees, /*Number*/ latitude, /*Number*/ longitude)
```

Returns an object containing the times at which the sun will reach the given altitude at the given
location. The object contains the following properties (each is a `Date` object):

| Property | Description |
| --------------- | ------------------------------------------------------------------------ |
| `rise` | The time at which the sun will reach the altitude when it is rising |
| `set` | The time at which the sun will reach the altitude when it is setting |

### Sun position

Expand Down
24 changes: 24 additions & 0 deletions suncalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,30 @@ SunCalc.getTimes = function (date, lat, lng) {
return result;
};

SunCalc.getTimesAtAltitude = function (date, altitude, lat, lng) {

var lw = rad * -lng,
phi = rad * lat,

d = toDays(date),
n = julianCycle(d, lw),
ds = approxTransit(0, lw, n),

M = solarMeanAnomaly(ds),
L = eclipticLongitude(M),
dec = declination(L, 0),

Jnoon = solarTransitJ(ds, M, L),

Jset = getSetJ(altitude * rad, lw, phi, dec, n, M, L),
Jrise = Jnoon - (Jset - Jnoon);

return {
rise: fromJulian(Jrise),
set: fromJulian(Jset),
};
};


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

Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ t.test('getTimes returns sun phases for the given date and location', function (
t.end();
});

t.test('getTimesAtAltitude returns the correct time for the given date and location', function (t) {
var times = SunCalc.getTimesAtAltitude(date, -6, lat, lng);

t.equal(new Date(testTimes.dawn).toUTCString(), times.rise.toUTCString());
t.end();
});

t.test('getMoonPosition returns moon position data given time and location', function (t) {
var moonPos = SunCalc.getMoonPosition(date, lat, lng);

Expand Down

0 comments on commit c794569

Please sign in to comment.