0
a = time.time()
b = time.time()
c = b - a

I know c is in seconds. Is there a mod out there (maybe datetime or time) where it can convert the seconds into any form of duration?

for example: I want to be able to say

print convert_time(c, days=True) 
print convert_time(c, hours=True) 

and so forth....

I know I can just convert it manually but I was wondering if there were anything out there I can use instead of writing my own func.

ealeon
  • 12,074
  • 24
  • 92
  • 173

2 Answers2

2

The answer has already been given Python Time Seconds to h:m:s

>>> import datetime
>>> str(datetime.timedelta(seconds=666))
'0:11:06'

I don't know how to mark the topic as duplicate. Forgive me for that.

Community
  • 1
  • 1
Lars de Bruijn
  • 1,430
  • 10
  • 15
1

You can use dateutil's relativedelta for this:

>>> from dateutil.relativedelta import relativedelta
>>> relativedelta(seconds=5000).hours
1
>>> relativedelta(seconds=5000).minutes
23
>>> relativedelta(seconds=5000).seconds
20

Plenty of other good options, too, but this has the kinds of conversion stuff built into it that I wish the stock datetime.timedelta had.

myersjustinc
  • 714
  • 1
  • 7
  • 15