You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a job that runs each day to schedule events for sunrise and sunset, and ran into an issue where the sunrise and sunset calculations return the incorrect day for the event given a date.
The API for getTimes doesn't specify any special formatting or timezone considerations, so I assumed these would be handled in-library; this is not the case for all locales.
Below is a binary search for times.sunrise that I wrote for the America/Denver timezone for the coordinates of Salt Lake City, Utah, USA. I tried to do the same for Australia, and while it's clear there's a difference, the exact behavior eludes me and didn't fit into my test handle.
SunCalc computes sunrise in a way that is always off by 1730904ms / 5330904ms (DST). This presents an issue for writing a workaround, since it's unclear how each coordinate+TZ combination will be affected. Each dev with issues must figure out what they need for their locale, with and without DST (as applicable).
I made an attempt to investigate in more detail but did not have time to dig deeper; my thinking at this point is that the calculations in the library have errors related to:
Floating point precision/Rounding from numerical constants (e.g. to/from Julian dates)
Inherent differences between Julian Date calculations and modern calendars (or JS-specific handling)
Perhaps timezones and locales need be considered as primary parameters. For example, in Northern European countries there are many days where no sunrise or sunset occurs. I wonder what this library returns there...
I believe this is the case for several other reported issues in this repository, including those for Australia and DST. Some of those include:
You can see if sunrise-sunset-js works for you; in my testing so far, the underlying NREL calculation appears robust and reliable for local midnight (and other times).
Snippet to Reproduce:
Environment:
NodeJS: v24.12.0
suncalc: ^1.9.0
// TypeScriptimportSunCalcfrom'suncalc';importassertfrom'node:assert';process.env.TZ='America/Denver';constlatitude=40.7606;constlongitude=-111.8881;(async()=>{try{awaitsuncalcTestMain();}catch(e){console.error(e);throwe;}})();asyncfunctionsuncalcTestMain(){console.log('suncalcTestMain() ~');letstartDate=newDate('2025-12-31');letyears=10;letdays=years*365;letd=newDate(startDate.valueOf());for(leti=0;i<days;i++){/* expected behavior is getTimes() at local midnight should return sunrise and sunset on the same day, ahead in time _*/d.setDate(d.getDate()+1);d.setHours(0,0,0,0);letsc=getSc(d);if(sc.sunrise<d){/* binary search to find instant where sunrise returns expected value _*/letscMidnightMs=sunriseSearch(d);letscMidnight=newDate(scMidnightMs);letmidnightDiffMs=(scMidnight.valueOf()-d.valueOf());assert(midnightDiffMs===1730904||midnightDiffMs===5330904// DST);}}}/* binary search to find instant where sunrise returns expected value _*/functionsunriseSearch(_d: Date): number{letd=newDate(_d.valueOf());d.setHours(0,0,0,0);letsc=getSc(d);letlowerMs=d.valueOf();letupperMs=lowerMs+(1000*60*60*12);while(lowerMs<=upperMs){letmid=lowerMs+Math.floor((upperMs-lowerMs)/2);sc=getSc(newDate(mid));if(sc.sunrise<d){lowerMs=mid+1;}else{upperMs=mid-1;}}letdl=newDate(lowerMs);letdu=newDate(upperMs);letscl=getSc(dl);letscu=getSc(du);assert(scu.sunrise<scl.sunrise);returnlowerMs;}functiongetSc(date: Date){returnSunCalc.getTimes(date,latitude,longitude);};
I have a job that runs each day to schedule events for sunrise and sunset, and ran into an issue where the sunrise and sunset calculations return the incorrect day for the event given a date.
The API for
getTimesdoesn't specify any special formatting or timezone considerations, so I assumed these would be handled in-library; this is not the case for all locales.Below is a binary search for
times.sunrisethat I wrote for theAmerica/Denvertimezone for the coordinates of Salt Lake City, Utah, USA. I tried to do the same for Australia, and while it's clear there's a difference, the exact behavior eludes me and didn't fit into my test handle.SunCalc computes
sunrisein a way that is always off by1730904ms /5330904ms (DST). This presents an issue for writing a workaround, since it's unclear how each coordinate+TZ combination will be affected. Each dev with issues must figure out what they need for their locale, with and without DST (as applicable).I made an attempt to investigate in more detail but did not have time to dig deeper; my thinking at this point is that the calculations in the library have errors related to:
I believe this is the case for several other reported issues in this repository, including those for Australia and DST. Some of those include:
You can see if sunrise-sunset-js works for you; in my testing so far, the underlying NREL calculation appears robust and reliable for local midnight (and other times).
Snippet to Reproduce:
Environment: