Understanding timedelta

前端 未结 2 457
名媛妹妹
名媛妹妹 2020-12-04 12:13

Given the python code below, please help me understand what is happening there.

start_time = time.time()
time.sleep(42)
end_time = time.time()

uptime = end_         


        
2条回答
  •  忘掉有多难
    2020-12-04 12:35

    why do I have to pass seconds = uptime to timedelta

    Because timedelta objects can be passed seconds, milliseconds, days, etc... so you need to specify what are you passing in (this is why you use the explicit key). Typecasting to int is superfluous as they could also accept floats.

    and why does the string casting works so nicely that I get HH:MM:SS ?

    It's not the typecasting that formats, is the internal __str__ method of the object. In fact you will achieve the same result if you write:

    print datetime.timedelta(seconds=int(uptime))
    

提交回复
热议问题