Sunrise and sunset times are important to know when walking, how early can you start, when do you need to finish by within daylight? What time does the sky become golden for stunning sunset photos?

To show the current sun times for each walk on BeyondTracks we use:

suncalc will, given a date and latitude/longitude return a set of sun times, we choose the show the sunrise, sunset, dawn and golden times.

However there’s a few caveats, you see suncalc will calculate sun times in the local time zone, which is not what we want. If I’m looking at a walk at Uluṟu, I want to know the sunset time in the local time zone, not in Sydney’s time zone.

That’s where Luxon and tz-lookup come into play, so

where

• DateTime is from Luxon
• tzLookup is from tz-lookup

given

• Location A is where the user is located (in time zone A)
• Location B is where the walk is located (and are wanting to calculate sun times in time zone B)

then

// obtain the time zone of Location B
const tzB = tzLookup(B.lat, B.lng)

// what time is it now, in time zone B
const currentTimeAtB = DateTime.fromISO(new Date().toISOString(), { zone: tzB })

// suntimes at B in time zone A
const suntimesAtB_in_tzA = SunCalc.getTimes(new Date(currentTimeAtB), B.lat, B.lng)

// suntimes at B in time zone B
const sunriseAtB_in_tzB = DateTime.fromISO(suntimesAtB_in_tzA.sunrise.toISOString(), { zone: tzB })


This is how we ensure that we always show sun times in the walk’s local time zone, regardless of where the user is.