Skip to content

Commit

Permalink
Type Location's tz and pytz attributes as advertised
Browse files Browse the repository at this point in the history
  • Loading branch information
markcampanelli committed Dec 23, 2024
1 parent 271fd97 commit 01263c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pvlib/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def __init__(self, latitude, longitude, tz='UTC', altitude=None,
self.pytz = pytz.UTC
elif isinstance(tz, datetime.tzinfo):
self.tz = tz.zone
self.pytz = tz
self.pytz = pytz.timezone(tz.zone)
elif isinstance(tz, (int, float)):
self.tz = tz
self.pytz = pytz.FixedOffset(tz*60)
self.tz = f"Etc/GMT{int(-tz):+d}"
self.pytz = pytz.timezone(self.tz)
else:
raise TypeError('Invalid tz specification')

Expand All @@ -89,8 +89,10 @@ def __init__(self, latitude, longitude, tz='UTC', altitude=None,

def __repr__(self):
attrs = ['name', 'latitude', 'longitude', 'altitude', 'tz']
# Use None as getattr default in case __repr__ is called during
# initialization before all attributes have been assigned.
return ('Location: \n ' + '\n '.join(
f'{attr}: {getattr(self, attr)}' for attr in attrs))
f'{attr}: {getattr(self, attr, None)}' for attr in attrs))

@classmethod
def from_tmy(cls, tmy_metadata, tmy_data=None, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pvlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def localize_to_utc(time, location):
"""
if isinstance(time, dt.datetime):
if time.tzinfo is None:
time = pytz.timezone(location.tz).localize(time)
time = location.pytz.localize(time)
time_utc = time.astimezone(pytz.utc)
else:
try:
Expand Down

0 comments on commit 01263c2

Please sign in to comment.