-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added solar time. #156
base: master
Are you sure you want to change the base?
Added solar time. #156
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,6 +308,24 @@ SunCalc.getMoonTimes = function (date, lat, lng, inUTC) { | |
return result; | ||
}; | ||
|
||
// calculation for solar time based on https://www.pveducation.org/pvcdrom/properties-of-sunlight/solar-time | ||
|
||
SunCalc.getSolarTime = function (date, utcOffset, lng) { | ||
// calculate the day of year | ||
var start = new Date(date.getFullYear(), 0, 0); | ||
var diff = (date - start) + ((start.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000); | ||
var dayOfYear = Math.floor(diff / dayMs); | ||
|
||
var b = 360 / 365 * (dayOfYear - 81) * rad; | ||
var equationOfTime = 9.87 * sin(2 * b) - 7.53 * cos(b) - 1.5 * sin(b); | ||
var localSolarTimeMeridian = 15 * utcOffset; | ||
var timeCorrection = equationOfTime + 4 * (lng - localSolarTimeMeridian); | ||
var localSolarTime = date.getHours() + timeCorrection / 60 + date.getMinutes() / 60; | ||
|
||
var solarDate = new Date(0, 0); | ||
solarDate.setMinutes(+localSolarTime * 60); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better way to handle the date object when it crosses between days? Using 23:58 as a starting time would be a good test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend switching to a date object as the return. return new Date(date.getTime() + timeCorrection * 60 * 1000); That will handle the above issue and allow the user to format the date however they want. |
||
return solarDate; | ||
}; | ||
|
||
// export as Node module / AMD module / browser variable | ||
if (typeof exports === 'object' && typeof module !== 'undefined') module.exports = SunCalc; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the original source code, it appears this should use longitude.
Second parameter should be utcOffset and third parameter should be Longitude.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. I've been trying different ways to achieve the same. Missed the readme. Thanks.