-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix all-day events entered in Google Cal - Reduce duplication with Gt…
…asks
- Loading branch information
1 parent
edfbaf6
commit 3d41b14
Showing
6 changed files
with
88 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from __future__ import annotations | ||
|
||
import datetime | ||
from typing import TYPE_CHECKING, cast | ||
|
||
import dateutil | ||
import pytz | ||
from bubop import assume_local_tz_if_none | ||
|
||
if TYPE_CHECKING: | ||
from syncall.types import GoogleDateT | ||
|
||
|
||
def parse_google_datetime(dt: GoogleDateT) -> datetime.datetime: | ||
"""Parse datetime given in format(s) returned by the Google API: | ||
- string with ('T', 'Z' separators). | ||
- (dateTime, timeZone) dictionary | ||
- datetime object | ||
The output datetime is always in local timezone. | ||
Usage:: | ||
>>> parse_google_datetime("2019-03-05T00:03:09Z") | ||
datetime.datetime(2019, 3, 5, 0, 3, 9) | ||
>>> parse_google_datetime("2019-03-05") | ||
datetime.datetime(2019, 3, 5, 0, 0) | ||
>>> parse_google_datetime("2019-03-05T00:03:01.1234Z") | ||
datetime.datetime(2019, 3, 5, 0, 3, 1, 123400) | ||
>>> parse_google_datetime("2019-03-08T00:29:06.602Z") | ||
datetime.datetime(2019, 3, 8, 0, 29, 6, 602000) | ||
>>> from tzlocal import get_localzone_name | ||
>>> tz = get_localzone_name() | ||
>>> a = parse_google_datetime({"dateTime": "2021-11-14T22:07:49Z", "timeZone": tz}) | ||
>>> b = parse_google_datetime({"dateTime": "2021-11-14T22:07:49.000000Z"}) | ||
>>> b | ||
datetime.datetime(2021, 11, 14, 22, 7, 49) | ||
>>> from bubop.time import is_same_datetime | ||
>>> is_same_datetime(a, b) or (print(a) or print(b)) | ||
True | ||
>>> parse_google_datetime({"dateTime": "2021-11-14T22:07:49.123456"}) | ||
datetime.datetime(2021, 11, 14, 22, 7, 49, 123456) | ||
>>> a = parse_google_datetime({"dateTime": "2021-11-14T22:07:49Z", "timeZone": tz}) | ||
>>> parse_google_datetime(a).isoformat() == a.isoformat() | ||
True | ||
""" | ||
if isinstance(dt, str): | ||
return dateutil.parser.parse(dt).replace(tzinfo=None) # type: ignore | ||
|
||
if isinstance(dt, dict): | ||
for key in "dateTime", "date": | ||
if key in dt: | ||
date_time = cast(str, dt.get(key)) | ||
break | ||
else: | ||
raise RuntimeError(f"Invalid structure dict: {dt}") | ||
|
||
dt_dt = parse_google_datetime(date_time) | ||
time_zone = dt.get("timeZone") | ||
if time_zone is not None: | ||
timezone = pytz.timezone(time_zone) | ||
dt_dt = timezone.localize(dt_dt) | ||
|
||
return dt_dt | ||
|
||
if isinstance(dt, datetime.datetime): | ||
return assume_local_tz_if_none(dt) | ||
|
||
raise TypeError( | ||
f"Unexpected type of a given date item, type: {type(dt)}, contents: {dt}", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters