Skip to content

Commit

Permalink
Fix timezone handling (#12)
Browse files Browse the repository at this point in the history
* Refactor timezone handling to use correct pytz method
* Bump version
  • Loading branch information
GalenReich authored May 21, 2024
1 parent e3df37e commit cf386d4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ShadowFinderColab.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 1 addition & 3 deletions shadowfinder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 11 additions & 5 deletions shadowfinder/shadowfinder.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,6 +7,7 @@
from mpl_toolkits.basemap import Basemap
from timezonefinder import TimezoneFinder
import json
from warnings import warn


class ShadowFinder:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand All @@ -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
Expand Down

0 comments on commit cf386d4

Please sign in to comment.