-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsun.min.js
More file actions
1 lines (1 loc) · 2.54 KB
/
sun.min.js
File metadata and controls
1 lines (1 loc) · 2.54 KB
1
const MinutesInDay=1440,SecondsInDay=60*MinutesInDay,J2000=2451545;function calculate(n,t,e,o,a,u){let r=n,i=t,d=julianCentury(gregorianToJulian(e,o,a,12,0,0,u)),c=geomMeanLongitudeSun(d),s=geomMeanAnomalySun(d),g=eccentricityEarthOrbit(d),M=apparentLongitudeSun(trueLongitudeSun(c,equationOfCenterSun(s,d)),d),h=obliquityCorrection(meanObliquityOfEcliptic(d),d),f=declinationSun(h,M),l=equationOfTime(h,c,g,s),T=hourAngleSunrise(r,f),R=solarNoon(i,l,u),y=sunrise(R,T),S=sunset(R,T);return{sunrise:toDate(e,o,a,y,u),sunset:toDate(e,o,a,S,u),solarnoon:toDate(e,o,a,R,u)}}function gregorianToJulian(n,t,e,o,a,u,r){t<=2&&(n-=1,t+=12);let i=Math.floor(n/100),d=2-i+Math.floor(i/4);return Math.floor(365.25*(n+4716))+Math.floor(30.6001*(t+1))+e+d-1524.5+(3600*o+60*a+u)/SecondsInDay-r/24}function toDate(n,t,e,o,a){return{year:n,month:t,day:e,hours:Math.floor(24*o),minutes:Math.floor(24*o*60%60),seconds:Math.floor(24*o*60*60%60),tz:a}}function julianCentury(n){return(n-J2000)/36525}function geomMeanAnomalySun(n){return 357.52911+n*(35999.05029-1537e-7*n)}function geomMeanLongitudeSun(n){return mod(280.46646+n*(36000.76983+3032e-7*n),0,360)}function eccentricityEarthOrbit(n){return.016708634-n*(42037e-9+1.267e-7*n)}function equationOfCenterSun(n,t){return Math.sin(degToRad(n))*(1.914602-t*(.004817+14e-6*t))+Math.sin(degToRad(2*n))*(.019993-101e-6*t)+289e-6*Math.sin(degToRad(3*n))}function trueLongitudeSun(n,t){return n+t}function apparentLongitudeSun(n,t){return n-.00569-.00478*Math.sin(degToRad(125.04-1934.136*t))}function meanObliquityOfEcliptic(n){return 23+(26+(21.448-n*(46.815+n*(59e-5-.001813*n)))/60)/60}function obliquityCorrection(n,t){return n+.00256*Math.cos(degToRad(125.04-1934.136*t))}function equationOfTime(n,t,e,o){let a=Math.tan(degToRad(n/2))*Math.tan(degToRad(n/2));return 4*radToDeg(a*Math.sin(2*degToRad(t))-2*e*Math.sin(degToRad(o))+4*e*a*Math.sin(degToRad(o))*Math.cos(2*degToRad(t))-.5*a*a*Math.sin(4*degToRad(t))-1.25*e*e*Math.sin(2*degToRad(o)))}function declinationSun(n,t){return radToDeg(Math.asin(Math.sin(degToRad(n))*Math.sin(degToRad(t))))}function hourAngleSunrise(n,t){return radToDeg(Math.acos(Math.cos(degToRad(90.833))/(Math.cos(degToRad(n))*Math.cos(degToRad(t)))-Math.tan(degToRad(n))*Math.tan(degToRad(t))))}function solarNoon(n,t,e){return(720-4*n-t+60*e)/MinutesInDay}function sunrise(n,t){return n-4*t/MinutesInDay}function sunset(n,t){return n+4*t/MinutesInDay}function mod(n,t,e){for(;n>e;)n-=e;for(;n<t;)n+=e;return n}function degToRad(n){return n*Math.PI/180}function radToDeg(n){return 180*n/Math.PI}module.exports={calculate:calculate};