Question
I have following code
ktm = timezone('Asia/Katmandu')
If I want to know zone of ktm, I can do like
ktm.zone
I know, Kathmandu is GMP+5:45. Is there any way to get this difference in pytz.
Thanks
Solution
import pytz
import datetime as dt
ktm = pytz.timezone('Asia/Katmandu')
utc = pytz.utc
now = dt.datetime.now()
now_utc = utc.localize(now)
now_ktm = now_utc.astimezone(ktm)
diff = now_ktm.replace(tzinfo=None) - now_utc.replace(tzinfo=None)
print(diff)
5:45:00
There is also:
print(now_ktm.strftime('%z'))
# +0545
though this gives the difference as a string.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow