pytz - Converting UTC and timezone to local time

I agree with Tzach's answer. Just wanted to include that the is_dst parameter is not required:

pytz.utc.localize(datetime.utcnow()).astimezone(tz)

That code converts the current UTC time to a timezone aware current datetime.

Whereas the code below converts the current UTC time to a timezone aware datetime which is not necessarily current. The timezone is just appended into the UTC time value.

tz.localize(datetime.utcnow())

It's the exact purpose of fromutc function:

tz.fromutc(utc_time)

(astimezone function calls fromutc under the hood, but tries to convert to UTC first, which is unneeded in your case)


I think I got it:

pytz.utc.localize(utc_time, is_dst=None).astimezone(tz)

This line first converts the naive (time zone unaware) utc_time datetime object to a datetime object that contains a timezone (UTC). Then it uses the astimezone function to adjust the time according to the requested time zone.