Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

-1day when calculating time difference. python

I'm trying to create an uptime command for the bot e.g "the bot has been online for 10 hours 15 minutes 10 seconds"

It works the way as indented up until it hits 12am, I think it might be to do with the calculation but I'm unsure on why it would add -1day when its formatted in hours minutes seconds.

This is what I have at the moment. Any help would be appreciated :)

from datetime import datetime
import os
import time

folder = r"C:UsersRyan MclaughlinPycharmProjectsSnappingbot-V2SnappingbotV2ext files"
filename = "uptime.txt"
fp = os.path.join(folder, filename)

with open(fp, "w") as f:
    f.writelines(datetime.now().time().strftime('%H:%M:%S')) # on startup of bot write the time in a file
file = open(fp)

startup_time = (file.readline())  # reads the startup time

print(startup_time)
time.sleep(10)
time_now = datetime.now().time().strftime('%H:%M:%S')  # finds time now
print(time_now)

FMT = '%H:%M:%S'
tdelta = datetime.strptime(time_now, FMT) - datetime.strptime(startup_time,FMT)  # calculates the difference between the startup time and now
print(tdelta)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...