Openweather timestamp converstion incorrect

I have set up a timestamp conversion to get the correct sunrise/sunset for a lat/lon location.

Here is the code

        sunsetcalc1 = (sunset[i])
        sunsetcalc2 = (time_zone_offset-3600)#BST timezone adjustment
        sunsetcalc3 = (sunsetcalc1+sunsetcalc2)
        sunsetdatetime1 = (datetime.fromtimestamp(sunsetcalc3).strftime(" %H:%M:%S "))
        print(sunsetcalc3)
1649690094 7200
1649697294
<class 'int'>
 18:14:54 

and here is the console print statement output, with 18:14:54 as the result. I checked and the correct sunset for that location is 20:55. I have double checked the figures that come from openweather and the inputs below are correct. The Timezone Offset is stated as 3hrs 10800 but because we have BST its only 2, hence my 3600 adjustment.

My Question is: Am I doing the calculation wrong, or do I need to go back to OpenweatherMap and try and establish why their original output is different

I wouldn’t say your calculation is wrong, just that there are many python libraries that handle this problem using time-like objects (instead of unix time integer seconds) while covering all of the edge cases (like BST/DST, local time time zones, etc.).

There is definitely something wrong, because if your time is off by 40 minutes, it it most likely not a time zone error, I have only heard of time zones being sliced at increments of 1 hr or in some cases 30 minutes. Definitely not 40, so that is a big red flag to me that data is wrong, and not some conversion method.

I do not know the initial value of time_zone_offset though.

Hi Ian
I’ve done a bit more checking on that, I found a website where you can put in your longditude and latitude co-ordinates and get an exact location on a map.

There was a short distance from Openweathermaps co-ords to mine, but not so it should cause that much difference. I think I will have to query it with them and see what they say. I will update the answer from them here, so if anyone else has this issue in the future they can refer back.

Update: I have discovered the problem and its Openweathermap. They have switched their lon and lat.

At present I get the correct output if I switch the lon and lat: e.g. lat=lon and lon = lat
url = f"{root_url}lat={lon}&lon={lat}&appid={api_key}&units={units}&exclude={exclude}"

I have told them about this, so we will see what they do. In the meantime I will keep it switched!

2 Likes