Skip to content

Commit 0554c85

Browse files
committed
Updated date methods to use named timezone instead of offset
1 parent 5db21c0 commit 0554c85

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

osxphotos/photosdb/photosdb.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,10 @@ def _process_database5(self):
20772077
info["imageTimeZoneName"] = row[8]
20782078
info["imageDate_timestamp"] = row[5]
20792079
info["imageDate"] = photos_datetime(
2080-
row[5], info["imageTimeZoneOffsetSeconds"] or 0, default=True
2080+
timestamp=row[5],
2081+
tzoffset=info["imageTimeZoneOffsetSeconds"] or 0,
2082+
tzname=info["imageTimeZoneName"],
2083+
default=True,
20812084
)
20822085
info["hidden"] = row[9]
20832086
info["favorite"] = row[10]

osxphotos/timezones.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def __init__(self, tz: Union[str, int]):
3131

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

3638
class Timezone:
3739
"""Create Timezone object from either name (str) or offset from GMT (int)"""
@@ -40,7 +42,10 @@ def __init__(self, tz: Union[str, int]):
4042
with objc.autorelease_pool():
4143
self._from_offset = False
4244
if isinstance(tz, str):
43-
self.timezone = Foundation.NSTimeZone.timeZoneWithName_(tz)
45+
# the NSTimeZone methods return nil if the timezone is invalid
46+
self.timezone = Foundation.NSTimeZone.timeZoneWithAbbreviation_(
47+
tz
48+
) or Foundation.NSTimeZone.timeZoneWithName_(tz)
4449
if not self.timezone:
4550
raise ValueError(f"Invalid timezone: {tz}")
4651
elif isinstance(tz, int):

0 commit comments

Comments
 (0)