Skip to content

Commit

Permalink
Making dt timezone aware
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanFauble committed Dec 5, 2023
1 parent 3110a0c commit be8fcf1
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion synapseclient/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,18 @@ def to_unix_epoch_time(dt: typing.Union[datetime.date, datetime.datetime, str])
tzinfo=current_timezone
)
else:
# If the datetime is not timezone aware, assume it is in the local timezone.
# This is required in order for windows to work with the `astimezone` method.
if dt.tzinfo is None:
current_timezone = datetime.datetime.now().astimezone().tzinfo
dt = dt.replace(tzinfo=current_timezone)
datetime_utc = dt.astimezone(datetime.timezone.utc)
return int((datetime_utc - UNIX_EPOCH).total_seconds() * 1000)


def to_unix_epoch_time_secs(dt: typing.Union[datetime.date, datetime.datetime]) -> int:
def to_unix_epoch_time_secs(
dt: typing.Union[datetime.date, datetime.datetime]
) -> float:
"""
Convert either `datetime.date or datetime.datetime objects <http://docs.python.org/2/library/datetime.html>`_
to UNIX time.
Expand All @@ -439,6 +446,11 @@ def to_unix_epoch_time_secs(dt: typing.Union[datetime.date, datetime.datetime])
tzinfo=current_timezone
)
else:
# If the datetime is not timezone aware, assume it is in the local timezone.
# This is required in order for windows to work with the `astimezone` method.
if dt.tzinfo is None:
current_timezone = datetime.datetime.now().astimezone().tzinfo
dt = dt.replace(tzinfo=current_timezone)
datetime_utc = dt.astimezone(datetime.timezone.utc)
return (datetime_utc - UNIX_EPOCH).total_seconds()

Expand Down

0 comments on commit be8fcf1

Please sign in to comment.