Reputation: 25
I am getting the error while giving the time zone as IST but when it is changed to GMT it is working can I know why it is happening and one more doubt can we compare two different time zones.Please check the code here which i have written.
from datetime import datetime
s1 = 'Oct 06 09:42:21 IST 2020'
d1 = datetime.strptime(s1, '%b %d %H:%M:%S %Z %Y')
print(d1)
s2 = 'Oct 06 09:42:26 2020 IST'
d2 = datetime.strptime(s2, '%b %d %H:%M:%S %Y %Z')
print(d2)
print(d1 < d2)
Upvotes: 0
Views: 32
Reputation: 3114
One of the other way to acheive the same is
from datetime import datetime
s1 = 'Oct 06 09:42:21 2020 +0530'
d1 = datetime.strptime(s1, '%b %d %H:%M:%S %Y %z')
print(d1)
print(d1.tzinfo)
s2 = 'Oct 06 09:42:26 2020 +0530'
d2 = datetime.strptime(s2, '%b %d %H:%M:%S %Y %z')
print(d2)
print(d2.tzinfo)
print(d1 < d2)
Output
2020-10-06 09:42:21+05:30
UTC+05:30
2020-10-06 09:42:26+05:30
UTC+05:30
True
The way you are trying to acheive this is not possible in my opinion, Here is a bug raised for the same
Upvotes: 1