-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update libtime to accept time before 1970 (#504)
- Loading branch information
1 parent
205dc3b
commit 18cb6e2
Showing
11 changed files
with
508 additions
and
145 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
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,40 @@ | ||
from __future__ import annotations | ||
|
||
from tzlocal import get_localzone | ||
from datetime import timezone | ||
from datetime import tzinfo as dt_tzinfo | ||
from datetime import datetime as std_datetime | ||
from hightime import datetime as ht_datetime | ||
from typing import Optional, Union | ||
try: | ||
import zoneinfo | ||
except ImportError: | ||
from backports import zoneinfo | ||
|
||
# theoretically the same as astimezone(), but with support for dates before 1970 | ||
def _convert_to_desired_timezone(expected_time_utc: Union[std_datetime, ht_datetime], tzinfo: Optional[dt_tzinfo] = None) -> Union[std_datetime, ht_datetime]: | ||
# if timezone matches, no need to do conversion | ||
if expected_time_utc.tzinfo is tzinfo: | ||
return expected_time_utc | ||
|
||
# if timezone is not defined, use system timezone | ||
if tzinfo is None: | ||
tzinfo = get_localzone() | ||
|
||
# use ZoneInfo here to account for daylight savings | ||
if isinstance(tzinfo, zoneinfo.ZoneInfo): | ||
localized_time = expected_time_utc.replace(tzinfo=tzinfo) | ||
desired_expected_time = tzinfo.fromutc(localized_time) | ||
return(desired_expected_time) | ||
|
||
# if the tzinfo passed in is a timedelta function, then we don't need to consider daylight savings | ||
elif tzinfo.utcoffset(None) is not None: | ||
current_time_utc = ht_datetime.now(timezone.utc) | ||
desired_timezone_offset = current_time_utc.astimezone(tz=tzinfo).utcoffset() | ||
desired_expected_time = expected_time_utc + desired_timezone_offset | ||
new_datetime = desired_expected_time.replace(tzinfo=tzinfo) | ||
return new_datetime | ||
|
||
# if the tzinfo passed in is none of the above, fall back to original astimezone() | ||
else: | ||
return expected_time_utc.astimezone(tzinfo) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import annotations | ||
|
||
from tzlocal import get_localzone | ||
from datetime import timezone | ||
from datetime import tzinfo as dt_tzinfo | ||
from datetime import datetime as std_datetime | ||
from hightime import datetime as ht_datetime | ||
from typing import Optional, Union | ||
try: | ||
import zoneinfo | ||
except ImportError: | ||
from backports import zoneinfo | ||
|
||
# theoretically the same as astimezone(), but with support for dates before 1970 | ||
def _convert_to_desired_timezone(expected_time_utc: Union[std_datetime, ht_datetime], tzinfo: Optional[dt_tzinfo] = None) -> Union[std_datetime, ht_datetime]: | ||
# if timezone matches, no need to do conversion | ||
if expected_time_utc.tzinfo is tzinfo: | ||
return expected_time_utc | ||
|
||
# if timezone is not defined, use system timezone | ||
if tzinfo is None: | ||
tzinfo = get_localzone() | ||
|
||
# use ZoneInfo here to account for daylight savings | ||
if isinstance(tzinfo, zoneinfo.ZoneInfo): | ||
localized_time = expected_time_utc.replace(tzinfo=tzinfo) | ||
desired_expected_time = tzinfo.fromutc(localized_time) | ||
return(desired_expected_time) | ||
|
||
# if the tzinfo passed in is a timedelta function, then we don't need to consider daylight savings | ||
elif tzinfo.utcoffset(None) is not None: | ||
current_time_utc = ht_datetime.now(timezone.utc) | ||
desired_timezone_offset = current_time_utc.astimezone(tz=tzinfo).utcoffset() | ||
desired_expected_time = expected_time_utc + desired_timezone_offset | ||
new_datetime = desired_expected_time.replace(tzinfo=tzinfo) | ||
return new_datetime | ||
|
||
# if the tzinfo passed in is none of the above, fall back to original astimezone() | ||
else: | ||
return expected_time_utc.astimezone(tzinfo) |
Oops, something went wrong.