3 ms·
[after reading the very first code example] There is a better way to write it: from datetime import datetime from pytz import timezone, utc EST = "US/E
by d0mine 14y ago
[after reading the very first code example]
There is a better way to write it:
from datetime import datetime
from pytz import timezone, utc
EST = "US/Eastern"
now = datetime.utcnow().replace(tzinfo=utc) # now in UTC
return now.astimezone(timezone(EST)) # now in EST
There is no DST transitions in UTC so localize()/normalize() are unnecessary here.
The code still is not pretty and error-prone in general but there are already libraries that wrap it into easy to use interface e.g., times, arrow. They are better target to compare itself against.