Repository navigation
Expand file tree
/
Copy pathsun.js
More file actions
57 lines (50 loc) · 2.48 KB
/
Copy pathsun.js
File metadata and controls
57 lines (50 loc) · 2.48 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
'use strict';
const SunCalc = require('suncalc');
/**
* sunEvents(lat, lon, date)
*
* Returns key solar times for a given location and calendar date.
* All returned values are native JS Date objects (UTC).
*
* @param {number} lat – Latitude (decimal degrees, + = North)
* @param {number} lon – Longitude (decimal degrees, + = East)
* @param {Date} date – Any moment on the desired local date (midnight is fine)
* @returns {{
* sunrise: Date, // upper limb of sun crosses horizon (morning)
* sunriseEnd: Date, // upper limb fully above horizon
* goldenHourEnd: Date, // morning golden hour ends
* goldenHour: Date, // evening golden hour starts
* sunsetStart: Date, // upper limb starts touching horizon
* sunset: Date, // upper limb disappears (evening)
* solarNoon: Date, // sun at highest point
* }}
*/
function sunEvents(lat, lon, date) {
const times = SunCalc.getTimes(date, lat, lon);
return {
// ── Morning ──────────────────────────────────────────────────────────
sunrise: times.sunrise, // classic "sunrise" moment
sunriseEnd: times.sunriseEnd, // disc fully clear of horizon
goldenHourEnd: times.goldenHourEnd, // end of morning golden hour
// ── Midday ───────────────────────────────────────────────────────────
solarNoon: times.solarNoon,
// ── Evening ──────────────────────────────────────────────────────────
goldenHour: times.goldenHour, // start of evening golden hour
sunsetStart: times.sunsetStart, // disc starts touching horizon
sunset: times.sunset, // classic "sunset" moment
};
}
/**
* fireAt(eventDate, leadMinutes)
*
* Returns the UTC time at which a push notification should be sent,
* i.e. `leadMinutes` before the solar event.
*
* @param {Date} eventDate – The sunrise or sunset Date
* @param {number} leadMinutes – Minutes before the event (default 20)
* @returns {Date}
*/
function fireAt(eventDate, leadMinutes = 20) {
return new Date(eventDate.getTime() - leadMinutes * 60 * 1000);
}
module.exports = { sunEvents, fireAt };