Fixed datetime bug

This commit is contained in:
ashish committed 2024-01-07 20:17:06 -06:00
1 parent 4ec4531f86
commit fc63499bf0
2 files changed
+12 -13

No files matched your search

+9 -9
View File
@@ -4,7 +4,7 @@ import json
import time
import traceback
from threading import Thread
from datetime import datetime, time, timedelta
import datetime as dt
import requests
from flask import Blueprint, request, jsonify
@@ -14,23 +14,23 @@ import paho.mqtt.publish as mqtt_publish
blueprint = Blueprint('detection', __name__)
def get_sunset() -> datetime:
sunset_date = datetime.now().date()
def get_sunset() -> dt.datetime:
sunset_date = dt.datetime.now().date()
try:
IPGEOLOCATION_API_KEY = os.environ['IPGEOLOCATION_API_KEY']
ipgeolocation_api_response = requests.get(f'https://api.ipgeolocation.io/astronomy?apiKey={IPGEOLOCATION_API_KEY}&location=Winter+Haven,+FL')
ipgeolocation_api_response.raise_for_status()
astronomical_json = json.loads(ipgeolocation_api_response.content)
sunset_time = datetime.strptime(astronomical_json['sunset'], '%H:%M').time()
sunset_time = dt.datetime.strptime(astronomical_json['sunset'], '%H:%M').time()
except Exception:
traceback.print_exc()
sunset_time = time(20, 00, 00)
sunset_time = dt.time(20, 00, 00)
finally:
if sunset_time < datetime.now().time():
if sunset_time < dt.datetime.now().time():
# Sunset has already passed today
sunset_date += timedelta(days=1)
return datetime.combine(sunset_date, sunset_time)
sunset_date += dt.timedelta(days=1)
return dt.datetime.combine(sunset_date, sunset_time)
def reset_all_camera_detection_at_sunset() -> None:
@@ -43,7 +43,7 @@ def reset_all_camera_detection_at_sunset() -> None:
sunset = get_sunset()
print(f'Waiting until {sunset} to reset detection for all cameras...', file=sys.stderr)
seconds_until_sunset = (sunset - datetime.now()).total_seconds()
seconds_until_sunset = (sunset - dt.datetime.now()).total_seconds()
time.sleep(seconds_until_sunset)
for camera_name in frigate_camera_config: