Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sunrise-Sunset API returning unexpected responses

I am trying to send API calls to https://api.sunrise-sunset.org/json to get the sunset/sunrise time. This works fine however the problem is whenever I send an API call with lat and lng parameters it returns an unexpected response.

For example.

The below code returns a correct value of sunrise and sunset(Without lat/lng parameters)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import requests

response = requests.get(url = "https://api.sunrise-sunset.org/json")
data = response.json()
print(data)

API Response:

{'results': {'sunrise': '5:47:00 AM', 'sunset': '5:55:48 PM', 'solar_noon': '11:51:24 AM', 'day_length': '12:08:48', 'civil_twilight_begin': '5:27:24 AM', 'civil_twilight_end': '6:15:24 PM', 'nautical_twilight_begin': '5:03:23 AM', 'nautical_twilight_end': '6:39:25 PM', 'astronomical_twilight_begin': '4:39:23 AM', 'astronomical_twilight_end': '7:03:25 PM'}, 'status': 'OK'}

However, when I try to send the API call with the lat and lng parameters it returns a very weird response. My location is Pakistan.

For example, the below code returns 12:56:37 AM as a sunrise time which is really weird to me.

import requests
LAT = 33.648953
LNG = 73.177007

parameters = {
    "lat":LAT,
    "lng":LNG

}

response = requests.get(url = "https://api.sunrise-sunset.org/json", params=parameters)
response.raise_for_status()
data = response.json()
print(data)

API Response:

{'results': {'sunrise': '12:56:37 AM', 'sunset': '1:00:55 PM', 'solar_noon': '6:58:46 AM', 'day_length': '12:04:18', 'civil_twilight_begin': '12:33:03 AM', 'civil_twilight_end': '1:24:28 PM', 'nautical_twilight_begin': '12:04:07 AM', 'nautical_twilight_end': '1:53:24 PM', 'astronomical_twilight_begin': '11:34:57 PM', 'astronomical_twilight_end': '2:22:35 PM'}, 'status': 'OK'}

What could go wrong?

I tried:

1- Cross-checking the longitude and latitude values from https://www.latlong.net/and they are perfectly valid.

2- I tried getting a response from the browser directly and it also shows a weird sunset/sunrise time.

3-Changing the values of LAT/LNG but still the same issue.

https://api.sunrise-sunset.org/json?lat=33.6938118&lng=73.0651511

API response from the browser

>Solution :

According to the api documentation at sunrise-sunset.org:

NOTE: All times are in UTC and summer time adjustments are not included in the returned data.

So you have to transform it to UTC +5 for Pakistan.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading