0

I am completely new to Python and need help to convert my output from seconds to hhmmss format. So instead of the output saying 182.79569892473117 seconds, I would like the output to be 0 hours, 3 minutes, 3 seconds.

def main():

#closest distance

c = 34000000

#farthest distance

f = 249000000

#average distance

a = 139000000

#time = distance/speed

closest = c / 186000*1
farthest = f / 186000*1
average = a / 186000*1

print("The photo will take ",closest,"seconds to reach home.")
print("The photo will take ",farthest," seconds to reach home.")
print("The photo will take ",average," seconds to reach home.")

main()

RoG
  • 3
  • 1
  • Look here, https://stackoverflow.com/questions/775049/how-do-i-convert-seconds-to-hours-minutes-and-seconds, and use the appropriate formatting. – mcmayer Sep 13 '20 at 17:01

4 Answers4

2
import datetime

a = datetime.timedelta(seconds=182.79569892473117)
print(a)
Zephyr
  • 11,891
  • 53
  • 45
  • 80
GHOST5454
  • 69
  • 2
1

you can use the time module to do this. this answer will help

here is an example

import time 
print(time.strftime("%H hours %M minutes %S secs",time.gmtime(186)))

fayez
  • 370
  • 1
  • 5
1

Here is an example of the calculation and also how you would format the result.

def gethms(secs):
    h = int(secs / 3600)
    secs -= h * 3600
    m = int(secs / 60)
    secs -= m * 60
    return h, m, secs

def format_seconds(secs):
    h, m, s = gethms(secs)
    return f"{h:d}:{m:02d}:{s:05.2f}"


def main():

    #closest distance
    c = 34000000
    
    #farthest distance
    f = 249000000

    #average distance
    a = 139000000

    #time = distance/speed
    closest = c / 186000*1
    farthest = f / 186000*1
    average = a / 186000*1

    print("The photo will take",format_seconds(closest),
          "to reach home.")

    print("The photo will take",format_seconds(farthest),
          "to reach home.")

    print("The photo will take",format_seconds(average),
          "to reach home.")

main()

Which gives:

The photo will take 0:03:02.80 to reach home.
The photo will take 0:22:18.71 to reach home.
The photo will take 0:12:27.31 to reach home.

Or if as the format string in the format_seconds function you use:

    return f"{h} hours, {m} minutes, {round(s)} seconds"

then you will get:

The photo will take 0 hours, 3 minutes, 3 seconds to reach home.
The photo will take 0 hours, 22 minutes, 19 seconds to reach home.
The photo will take 0 hours, 12 minutes, 27 seconds to reach home.
alani
  • 12,573
  • 2
  • 13
  • 23
  • In the second part of the def format_seconds, the f after return is just telling python how to format the output based upon the first def gethms? – RoG Sep 13 '20 at 17:02
  • @Rob Yes that's right, I thought it would be helpful to separate out the calculation (getting hours, minutes and seconds) from the output formatting. Note that in the calculation I am returning ints for hours and minutes, but a float for seconds so that it doesn't lose the fractional part of the seconds, although in the second suggested formatting I am using `round` to then round to the nearest integer to match what the question suggests. – alani Sep 13 '20 at 17:05
  • So the f in {s:05.2f} means float then does the d in {h:d} mean defined? – RoG Sep 13 '20 at 17:21
  • I think it's short for "decimal", but it's for an integer. In fact, s (for string) would probably work also. – alani Sep 13 '20 at 17:22
0
c = 34000000
f = 249000000
a = 139000000
closest = c / 186000*1
farthest = f / 186000*1
average = a / 186000*1

import time

def gettime(sec):
    return time.strftime('%H hours, %M minutes, %S seconds', time.gmtime(sec))


print("The photo will take ",gettime(closest)," to reach home.")
print("The photo will take ",gettime(farthest)," to reach home.")
print("The photo will take ",gettime(average)," to reach home.")

and the result will be:

The photo will take  00 hours, 03 minutes, 02 seconds  to reach home.
The photo will take  00 hours, 22 minutes, 18 seconds  to reach home.
The photo will take  00 hours, 12 minutes, 27 seconds  to reach home.