Python - 将 UTC 中的 12 小时时间转换为纪元格式

Python - Convert 12 hour time in UTC in epoch format

我有一个文本框以以下当地时间格式显示时间 - 04/03/2018 02:59:44 PM 我在 python 中使用 Selenium 来获取时间并将其(本地时间)转换为纪元时间 (UCT)。但它正在转换为早 11 小时 30 分钟的时间(2018 年 4 月 3 日 3:29:44 AM)。这是我的代码:

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 

calendar.timegm() 从 UTC 转换为纪元以来的秒数。

mktime() 将本地时间转换为纪元以来的秒数。

来源:https://docs.python.org/2/library/time.html

我根据上面的建议解决了这个问题

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