Fixed datetime/timezone problem that caused `OSError: [Errno 22] Invalid argument` on Windows.

This commit is contained in:
Geoff Taylor 2022-02-26 19:09:47 +00:00
parent 126fa02508
commit 0841490076
1 changed files with 6 additions and 1 deletions

View File

@ -33,5 +33,10 @@ def local_now() -> datetime:
return datetime.now()
# Getting a comparable UTC time using this line:
# return datetime.utcnow().astimezone(timezone.utc)
# seems to cause `OSError: [Errno 22] Invalid argument` on Windows (but is fine on Mac and
# Linux). Best to avoid it and use the following instead, which seems to work consistently
# on Windows as well as Mac and Linux.
def utc_now() -> datetime:
return datetime.utcnow().astimezone(timezone.utc)
return datetime.utcnow().replace(tzinfo=timezone.utc)