Skip to content

Commit

Permalink
Updated date methods to use named timezone instead of offset
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Oct 26, 2024
1 parent 5db21c0 commit 0554c85
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 4 additions & 1 deletion osxphotos/photosdb/photosdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,10 @@ def _process_database5(self):
info["imageTimeZoneName"] = row[8]
info["imageDate_timestamp"] = row[5]
info["imageDate"] = photos_datetime(
row[5], info["imageTimeZoneOffsetSeconds"] or 0, default=True
timestamp=row[5],
tzoffset=info["imageTimeZoneOffsetSeconds"] or 0,
tzname=info["imageTimeZoneName"],
default=True,
)
info["hidden"] = row[9]
info["favorite"] = row[10]
Expand Down
9 changes: 7 additions & 2 deletions osxphotos/timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def __init__(self, tz: Union[str, int]):

def known_timezone_names() -> list[str]:
"""Get list of valid timezones on macOS"""
return sorted(list(Foundation.NSTimeZone.knownTimeZoneNames()))
# sort by shortest length then alphabetically
timezones = list(Foundation.NSTimeZone.knownTimeZoneNames())
return sorted(timezones, key=lambda x: (len(x), x))

class Timezone:
"""Create Timezone object from either name (str) or offset from GMT (int)"""
Expand All @@ -40,7 +42,10 @@ def __init__(self, tz: Union[str, int]):
with objc.autorelease_pool():
self._from_offset = False
if isinstance(tz, str):
self.timezone = Foundation.NSTimeZone.timeZoneWithName_(tz)
# the NSTimeZone methods return nil if the timezone is invalid
self.timezone = Foundation.NSTimeZone.timeZoneWithAbbreviation_(
tz
) or Foundation.NSTimeZone.timeZoneWithName_(tz)
if not self.timezone:
raise ValueError(f"Invalid timezone: {tz}")
elif isinstance(tz, int):
Expand Down

0 comments on commit 0554c85

Please sign in to comment.