Is there a public API to access sunrise/sunset data? As far as the sunrise time and sunset time for any and all future calendar days?
4 Answers
Since the sunrise and sunset times are determined by 3 factors, position on the earth in Lat/Long & Altitude, date plus possibly obscuring items like mountains and most services leave out the mountains you rarely need a public API rather than using an Ephemeris library.
Depending on which language you are using you will find libraries available for most of them. You will then need to specify the location and also the are a number of definitions for Sunrise/Sunset - Astronomical, Civil, Nautical and the old favorite "When you can tell a black thread from a white one". Factors such as temperature can also affect the apparent time of sunrise/set by changing the atmospheric refraction.
The good news is that once you have your ephemeris you can calculate rising and setting times for other bodies such as the moon for any date and without internet connectivity.
Some popular libraries:
- Python: PyEphem
- Java: jparseclibrary
- C/C++ ephemeris are mostly based on parsing the JPL data.
If you don't need other bodies then the Sunrise Equation will give you a good enough value resonably simply.
It is also worth taking a look at the excellent Photographers Ephemeris.
-
1It seems that jparseclibrary no longer exists and the project hosting website seems to be down as well...– joshumaxJan 26, 2015 at 2:22
-
Not just the 3 factors you mention, but also what definition of sunset? Astronomical is different from nautical/aviation, is different from most religion or common civilian use of the term.– ivanivanDec 19, 2017 at 14:16
-
@ivanivan - I do mention Astronomical, Nautical, Civil & "tell black thread from white" which is one of the religious ones in para 2. Dec 20, 2017 at 6:40
It's free to use. Just needs a credit link back to the website.
I know more APIs for Java programming language. Here a list (in alphabetical order):
- Caarmen/SunriseSunset (based on formula given on Wikipedia)
- mikereedell/sunrisesunsetlib-java (based on a simple algorithm by Ed Williams)
- My library Time4J which offers an API for calculating sunrise/sunset/moonrise/moonset and solstices/equinoxes and sun positions as well as moon positions and moon phases. Calculations are mainly based on the book "Astronomical Algorithms" by Jean Meeus, but also use some NOAA-algorithms. A sister project named Time4A exists for the Android platform.
The first two libraries use old standard date/time-API in Java (java.util.Calendar
and java.util.Date
) while Time4J uses its own types which can easily be converted to new java.time
-API (but also to old java-API).
This gets to be very complex - you have multiple definitions of the term sunset (and sunrise) - astronomical, nautical, and civilian - and then you have the latitude/longitude issue. Don't forget that in some parts of the world, the sun does not set for weeks at a time, and in 6 months, it won't be rising for weeks at a time, so there are some edge cases where FALSE should be returned.
PHP has a nice date_sunrise and date_sunset function defined - I'd either set up something to call via HTTP(S) or look at the actual source behind the latest version of PHP.
http://php.net/manual/en/function.date-sunrise.php
http://php.net/manual/en/function.date-sunset.php
Civil twilight 96 degrees - Conventionally used to signify twilight
Nautical twilight 102 degrees - the point at which the horizon stops being visible at sea.
Astronical twilight at 108 degrees - the point when Sun stops being a source of any illumination.