-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime-of-day.js
47 lines (41 loc) · 1.17 KB
/
time-of-day.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
const suncalc = require('suncalc');
// Thanks to https://github.com/kcharwood/homebridge-suncalc/blob/master/index.js
const timeOfDay = (latitude, longitude) => {
const nowDate = new Date();
const now = nowDate.getTime();
const sunDates = suncalc.getTimes(
nowDate,
latitude,
longitude,
);
const times = {
dawn: sunDates.dawn.getTime(),
sunrise: sunDates.sunrise.getTime(),
sunriseEnd: sunDates.sunriseEnd.getTime() + (1000 * 60),
sunsetStart: sunDates.sunsetStart.getTime() + (1000 * 60),
sunset: sunDates.sunset.getTime(),
dusk: sunDates.dusk.getTime(),
};
if (now < times.dawn) {
// Nighttime
return 6;
} else if (now >= times.dawn && now < times.sunrise) {
// Morning Twilight
return 1;
} else if (now >= times.sunrise && now < times.sunriseEnd) {
// Sunrise
return 2;
} else if (now >= times.sunriseEnd && now < times.sunsetStart) {
// Daytime
return 3;
} else if (now >= times.sunsetStart && now < times.sunset) {
// Sunset
return 4;
} else if (now >= times.sunset && now < times.dusk) {
// Evening Twilight
return 5;
}
// Nighttime
return 6;
};
module.exports = timeOfDay;