Reputation: 281
I have a text box that displays time in following local time format - 04/03/2018 02:59:44 PM I'm using Selenium in python to get this time and convert it (local time) to epoch time (UCT). But it's converting to a time that is 11 hours 30 mins earlier (April 3, 2018 3:29:44 AM). here is my code:
next_chk_dt = myDriver.find_element_by_xpath("(//input[@id='dateIDVisible'])[6]").get_attribute('value')
# displays 04/03/2018 02:59:44 PM if you print the value
temp_Time2 = datetime.datetime.strptime(next_chk_dt, '%m/%d/%Y %H:%M:%S %p')
epoch_Time1 = calendar.timegm(temp_Time2.timetuple())
print (epoch_Time1)
# You get 1522726184, which is incorrect. it should be 1522781984
Upvotes: 0
Views: 1080
Reputation: 281
I was able to resolve this based on the suggestion above
from time import strptime
from datetime import datetime
mytm= ("04/03/2018 02:59:44 PM")
fmt = ("%m/%d/%Y %I:%M:%S %p")
epochDate = int(time.mktime(time.strptime(mytm, fmt)))
print (epochDate)
# ==> 1522781984
Upvotes: 0
Reputation: 1156
calendar.timegm() converts from UTC to seconds since epoch.
mktime() converts from local time to seconds since epoch.
source: https://docs.python.org/2/library/time.html
Upvotes: 1