我需要将
HH:MM:SS
格式转换为 time.time()
对象。有什么办法可以做到吗?
这是我的
HH:MM:SS
格式化时间:
a = '14:37:29'
我想将其转换为
time.time()
对象,例如:
a = 1600256249
这可以实现吗?如果不是,我应该尝试什么?希望你能帮忙。
要获取 UNIX 时间,您需要添加日期。您可以将时间字符串与日期结合起来。例如:
from datetime import datetime, timezone
s = '14:37:29'
today = datetime.today().date() # use appropriate date here...
# make a datetime object with today's date and the time from s:
dt = datetime.combine(today, datetime.strptime(s, '%H:%M:%S').time())
# set UTC so that Python doesn't assume local time
dt = dt.replace(tzinfo=timezone.utc)
# get the timestamp
ts = dt.timestamp()
print(ts)
# (some Unix time, with ts % 86400 representing the time in seconds of day)
这个可以实现吗?
我会说不。
a = '14:37:29'
仅保存时-分-秒,而 time.time()
确实返回自纪元开始以来的秒数,即您还需要知道时、分、秒之外的日、月和年,以创建与 time.time()
返回的等效内容.