From cf386d4d171bb5046ba00376612370184d9babfa Mon Sep 17 00:00:00 2001 From: Galen Reich <54807169+GalenReich@users.noreply.github.com> Date: Tue, 21 May 2024 12:33:02 +0100 Subject: [PATCH] Fix timezone handling (#12) * Refactor timezone handling to use correct pytz method * Bump version --- ShadowFinderColab.ipynb | 2 +- pyproject.toml | 2 +- shadowfinder/cli.py | 4 +--- shadowfinder/shadowfinder.py | 16 +++++++++++----- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ShadowFinderColab.ipynb b/ShadowFinderColab.ipynb index f2d3ad8..f67ee62 100644 --- a/ShadowFinderColab.ipynb +++ b/ShadowFinderColab.ipynb @@ -47,7 +47,7 @@ "datetime_time = datetime.datetime.strptime(time, \"%H:%M:%S\").time()\n", "\n", "date_time = datetime.datetime.combine(\n", - " datetime_date, datetime_time, tzinfo=datetime.timezone.utc\n", + " datetime_date, datetime_time\n", ") # Date and time of interest\n", "\n", "if \"finder\" not in locals():\n", diff --git a/pyproject.toml b/pyproject.toml index f203636..bf8e7ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ShadowFinder" -version = "0.2.1" +version = "0.2.2" description = "Find possible locations of shadows." authors = ["Bellingcat"] license = "MIT License" diff --git a/shadowfinder/cli.py b/shadowfinder/cli.py index 08cb3ba..a5d2bf4 100644 --- a/shadowfinder/cli.py +++ b/shadowfinder/cli.py @@ -38,9 +38,7 @@ def find( """ try: - date_time = datetime.strptime( - f"{date} {time}", "%Y-%m-%d %H:%M:%S" - ).replace(tzinfo=timezone.utc) + date_time = datetime.strptime(f"{date} {time}", "%Y-%m-%d %H:%M:%S") except Exception as e: raise ValueError(f"Invalid argument type or format: {e}") _validate_args(object_height, shadow_length, date_time) diff --git a/shadowfinder/shadowfinder.py b/shadowfinder/shadowfinder.py index aa8fadc..f470d3a 100644 --- a/shadowfinder/shadowfinder.py +++ b/shadowfinder/shadowfinder.py @@ -1,5 +1,4 @@ -import datetime -from pytz import timezone +from pytz import timezone, utc import pandas as pd from suncalc import get_position import numpy as np @@ -8,6 +7,7 @@ from mpl_toolkits.basemap import Basemap from timezonefinder import TimezoneFinder import json +from warnings import warn class ShadowFinder: @@ -35,6 +35,11 @@ def __init__( def set_details(self, object_height, shadow_length, date_time, time_format=None): self.object_height = object_height self.shadow_length = shadow_length + if date_time is not None and date_time.tzinfo is not None: + warn( + "date_time is expected to be timezone naive (i.e. tzinfo=None). Any timezone information will be ignored." + ) + date_time = date_time.replace(tzinfo=None) self.date_time = date_time if time_format is not None: @@ -100,7 +105,7 @@ def find_shadows(self): self.generate_timezone_grid() if self.time_format == "utc": - valid_datetimes = self.date_time + valid_datetimes = utc.localize(self.date_time) valid_lats = self.lats.flatten() valid_lons = self.lons.flatten() elif self.time_format == "local": @@ -109,8 +114,9 @@ def find_shadows(self): ( None if tz is None - else self.date_time.replace(tzinfo=timezone(tz)) - .astimezone(datetime.timezone.utc) + else timezone(tz) + .localize(self.date_time) + .astimezone(utc) .timestamp() ) for tz in self.timezones