Over the holidays, I created Good Morning, Milwaukee! (a Mastodon bot that posts a “Good Morning” toot every morning). The problem is that it hasn’t been behaving correctly since shortly after I finished it. I want to simplify it a bit, so I can make it a little less fragile. The first component of the message that I decided to focus on is the bit that gets today’s sunrise and sunset.
The bot does it by using a web service, and then adjusting the resulting the times from UTC to local time. I went looking for a function that can calculate the sunrise and sunset mathmatically. The only issue with it is that the function needs the user’s latitude, longitude, and timezone. Getting the user’s latitude and longitude is something that we have done previously. The timezone is a little more difficult. I ended up calculating the time offset by writing
let offset = -(new Date().getTimezoneOffset() / 60). The time offset isn’t really the same thing but it should generally get you there.
Here is the result …
This example should output your latitude, longitude, timezone, and what time sunrise and sunrise is today, for where you are in the world.
Have a suggestion for a tweak, a comment, or a question? Feel free to drop a comment, below.