简体   繁体   中英

Converting time (secs) to days:hours:mins:seconds in python

I am trying to convert time(secs) to below format. This is the seconds "8596L", because of L not sure what that means. This is my code and output below which shows only 2 hours+ but the actual time should be way longer..as its 8596L. How do I convert this?

def format_time(time_in_seconds):
    min, sec = divmod(time_in_seconds, 60)
    hr, min = divmod(min, 60)
    day, hr = divmod(hr, 24)
    return "%02dd:%02dh:%02dm:%02ds" % (day, hr, min, sec)

print format_time(8596L)

Output: 00d:02h:23m:16s

Python 2 used L suffix to indicate a long integer, but was removed in Python 3 where all ints are long ints.

Python3 is treating 8596L as 8596, hence the 2hrs 23mins 16sec you are getting.

Your conversion is working, but the input is wrong. Look at where the input of 8596L is coming from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM