Python Forum
Information about api.sunrise-sunset please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Information about api.sunrise-sunset please
#7
Not able to figure out anything about pendulum, I kept on trying to make something work (work arounds).

And, I ran into another snag that I wasn't able to overcome, so, I'm asking for some help.

Even if it seems obvious, please allow me to say that I'm self taught in Python (never been to a class) and I find it difficult to know what variables I can subscript and not. Also, I don't know what I don't know much of the time.

With that said, after I execute datetime.strptime, the sunset value has a '-1 day, ' value in it that I can't use or need when comparing sunset to current local time. And, because sunset is the result of datetime.strptime, it can't be subscripted (I can't figure how). Yes, I do know why '-1 day, ' is there but, again, I don't need it. The sunset time would be correct if the '-1 day, ' were not there.

What do I do to end up with my local sunset time?


# return sunrise and sunset local time for this longitude and latitude
import requests
from datetime import datetime

url = r"https://api.sunrise-sunset.org/json?lat=28.262222&lng=-96.747165&formatted=0&date=today"
format   = '%H:%M:%S'
six_hours  = "06:00:00"
eighteen_hours = "18:00:00"

response = requests.get(url)
resp_json = response.json()
sunrise_date_time = resp_json["results"]["sunrise"]
sunset_date_time  = resp_json["results"]["sunset"]
print("sunrise_date_time ", sunrise_date_time)
print("sunset_date_time  ",  sunset_date_time)
sunrise_time = sunrise_date_time[11:-6]
sunset_time  =  sunset_date_time[11:-6]
print("sunrise_time ", sunrise_time)
print("sunset_time  ",  sunset_time)
sunrise = datetime.strptime(sunrise_time, format) - datetime.strptime(six_hours, format)
sunset  = datetime.strptime(sunset_time,  format) - datetime.strptime(six_hours, format)
print("sunrise ", sunrise)
print("sunset  ",  sunset)
Output:
pi@raspberrypi:~ $ sudo python3 sunrise.py sunrise_date_time 2022-01-29T13:14:31+00:00 sunset_date_time 2022-01-30T00:05:45+00:00 sunrise_time 13:14:31 sunset_time 00:05:45 sunrise 7:14:31 sunset -1 day, 18:05:45 <<-------- Notice this pi@raspberrypi:~ $
Reply


Messages In This Thread
RE: Information about api.sunrise-sunset please - by bill_z - Jan-29-2022, 08:03 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020