Home > Software design >  Converting seconds into days, hours, minutes & seconds in Python
Converting seconds into days, hours, minutes & seconds in Python

Time:10-24

I have a function that returns seconds into days, hours, mins and sec. But I need to However, not print if outputs are 0. For example, if I enter 176400 seconds I want output would be "2 day 1 hours" not "2 day, 2 hours, 0 minutes, 0 seconds".

I did so far:

sec = int(input("Enter time in Seconds: "))
temp = sec
day = sec // 86400
sec %= 86400
hour = sec // 3600
sec %= 3600
mins = sec // 60
sec %= 60

if day >= 1:
    print(f'time in minutes is {day}days {hour}hour {mins}min {sec}sec')

elif hour >= 1:
    if mins == 0 and sec == 0:
        print(f'time in minutes is {hour}hour')
    elif mins == 0:
        print(f'time in minutes is {hour}hour {sec}sec')
    elif sec == 0:
        print(f'time in minutes is {hour}hour {mins}min')
    else:
        print(f'time in minutes is {hour}hour {mins}min {sec}sec')

elif mins >= 1:
    if sec == 0:
        print(f'time in minutes is {mins}min')
    else:
        print(f'time in minutes is {mins}min {sec}sec')

elif sec >= 1:
    print(f'time sec == {sec} sec')

I could be continue This code using bunch of "if" statement, but is there shorter way to do this?

CodePudding user response:

It looks like you're trying to do something like:

result = "time in minutes is"
if days >0:
    result  = f" {days} days"
if hours > 0:
    result  = f" {hours} hours"
if mins > 0:
    result  = f" {mins} minutes"
if secs > 0:
    result  = f" {secs} seconds"

CodePudding user response:

IIUC, you want shorter way then you can use datetime.timedelta like below:

import datetime

sec = int(input('Enter the number of seconds: '))

print(datetime.timedelta(seconds=sec))

Output:

Enter the number of seconds: 86600
1 day, 0:03:20

You can add these lines to get what you want:

import datetime
sec = int(input('Enter the number of seconds: '))
str_tm = str(datetime.timedelta(seconds=sec))
day = str_tm.split(',')[0]
hour, minute, second = str_tm.split(',')[1].split(':')
print(f'{day}{hour} hour {minute} min {second} sec')

Output:

Enter the number of seconds: 176400
2 days 1 hour 00 min 00 sec

CodePudding user response:

You can assemble the non-zero parts in a list and join it at the end. You can also use the divmod function to extract the days,hours,minutes and seconds:

sec = int(input("Enter time in Seconds: "))

time  = []
days,sec = divmod(sec,86400) # sec will get seconds in partial day
if days:
    time.append(f"{days} day" "s"*(days>1))
    
hours,sec = divmod(sec,3600) # sec will get seconds in partial hour
if hours:
    time.append(f"{hours} hour" "s"*(hours>1))
    
minutes,sec = divmod(sec,60) # sec will get seconds in partial minute
if minutes:
    time.append(f"{minutes} minute" "s"*(minutes>1))
    
if sec:
    time.append(f"{sec} second" "s"*(sec>1))

Sample runs:

Enter time in Seconds: 176400
time is: 2 days, 1 hour

Enter time in Seconds: 1767671
time is: 20 days, 11 hours, 1 minute, 11 seconds

Enter time in Seconds: 259321
time is: 3 days, 2 minutes, 1 second

The whole thing could be simplified using a loop that goes through the divisors and time units:

sec = int(input("Enter time in Seconds: "))

time  = []
for d,u in [(86400,"day"),(3600,"hour"),(60,"minute"),(1,"second")]:
    n,sec = divmod(sec,d)
    if n: time.append(f"{n} {u}" "s"*(n>1))
    
print("time is:",", ".join(time))
  • Related