我的数据有一个UTC时间戳字段作为字符串格式(例如'1426402510')。我需要将此字段与当前时间进行比较,并以秒为单位发出持续时间。我不知道如何将此字符串转换为适当的日期时间格式进行转换 - 我试图在Python中使用不同的日期时间方法会产生错误,所以我非常感谢您的帮助。以下是我的部分代码:比较Python中的两个UTC时间戳
import datetime
# get current local time as a UTC timestamp
current_time = datetime.datetime.utcnow()
current_time.strftime("%s")
# convert the string representing the UTC timestamp in my data to datetime for comparison
time_val = '1426402510'
#utc_dt = ?? # how should I convert time_val to compare with current_time?
# the required output
diff = (current_time - utc_dt).total_seconds()
感谢您的帮助。