from datetime import datetime, timedelta, timezone
from dateutil import tz
# Add or Replace timezone information
print(dt.replace(tzinfo=timezone.utc))
# create timezone of +5:30 UTC
IST = timezone(timedelta(hours=5, minutes=30))
# Timezone-aware datetime
dt = datetime(2017, 12, 30, 15, 9, 3, tzinfo = IST)
# Most useful function "tz.gettz()" when dealing with daylight saving situations
et = tz.gettz('America/New_York') # Another timezone
# Convert corresponding time to another timezone
print(dt.astimezone(et))
###### Ambiguous time #########
eastern = tz.gettz('US/Eastern')
first_1am = datetime(2017, 11, 5, 1, 0, 0, tzinfo = eastern)
# Check if there are multiple timespans (End of daylight saving time)
tz.datetime_ambiguous(first_1am)
second_1am = datetime(2017, 11, 5, 1, 0, 0, tzinfo = eastern)
# The folded timespan that recurs
second_1am = tz.enfold(second_1am)
# Duration in local time zone
(first_1am - second_1am).total_seconds()
# Duration in universal time
first_1am = first_1am.astimezone(tz.UTC)
second_1am = second_1am.astimezone(tz.UTC)
(second_1am - first_1am).total_seconds()