Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,19 @@ Returns an object with the following properties:
By default, it will search for moon rise and set during local user's day (frou 0 to 24 hours).
If `inUTC` is set to true, it will instead search the specified date from 0 to 24 UTC hours.

### Solar time

```js
SunCalc.getSolarTime(/*Date*/ date, /*Number*/ latitude, /*Number*/ utcOffset)
Copy link

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.

Copy link
Author

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.

```

Returns the solar time of the given date in the given latitude and UTC offset.

## Changelog

#### 1.9.0 — Jan 22, 2022
- Added solar time.

#### 1.8.0 — Dec 22, 2016

- Improved precision of moonrise/moonset calculations.
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "suncalc",
"version": "1.8.0",
"version": "1.9.0",
"description": "A tiny JavaScript library for calculating sun/moon positions and phases.",
"homepage": "https://github.com/mourner/suncalc",
"keywords": [
Expand All @@ -12,7 +12,8 @@
"sunset",
"twilight",
"moon",
"illumination"
"illumination",
"solar"
],
"author": "Vladimir Agafonkin",
"repository": {
Expand Down
18 changes: 18 additions & 0 deletions suncalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The 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.

Copy link

@thkruz thkruz Jul 27, 2022

Choose a reason for hiding this comment

The 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;
Expand Down