Convert seconds to hours/minutes/seconds and pretty print
up vote
5
down vote
favorite
I'm self taught in python 3, and I frequently keep track of how long my scripts have been running using the time library.
import time
startTime = time.time()
#The main body of script...
print('The script finished in {0} seconds'.format(time.time() - startTime)
However this can be unhelpful if the script has been running for some time, an example output being:
'The script has been running for 4323.580279111862 seconds'
I wrote a function which takes a float variable and prints the time in a more readable way. I convert the seconds to integer to get rid of the 12 decimal places, and I can pass a boolean value which changes the wording of the print to indicate that the script is still running or has finished.
def printRuntime(seconds,finished=False):
seconds = int(seconds)
status = 'has been running for'
if finished == True:
status = 'finished in'
if seconds < 60:
print('The script {} {} seconds'.format(status,seconds))
return
elif seconds < 3600:
minutes = seconds // 60
seconds = seconds - 60*minutes
print('The script {} {} minutes & {} seconds'.format(status,minutes,seconds))
return
else:
hours = seconds // 3600
minutes = (seconds - 3600*hours) // 60
seconds = seconds - 3600*hours - 60*minutes
print('The script {} {} hours, {} minutes & {} seconds'.format(status,hours,minutes,seconds))
return
import time
startTime = time.time()
#The main body of script...
printRuntime(time.time() - startTime)
Since I'm self taught I have no real idea about best practice. I feel like there must be a more concise way of doing this, what mistakes am I making?
python python-3.x datetime formatting
add a comment |
up vote
5
down vote
favorite
I'm self taught in python 3, and I frequently keep track of how long my scripts have been running using the time library.
import time
startTime = time.time()
#The main body of script...
print('The script finished in {0} seconds'.format(time.time() - startTime)
However this can be unhelpful if the script has been running for some time, an example output being:
'The script has been running for 4323.580279111862 seconds'
I wrote a function which takes a float variable and prints the time in a more readable way. I convert the seconds to integer to get rid of the 12 decimal places, and I can pass a boolean value which changes the wording of the print to indicate that the script is still running or has finished.
def printRuntime(seconds,finished=False):
seconds = int(seconds)
status = 'has been running for'
if finished == True:
status = 'finished in'
if seconds < 60:
print('The script {} {} seconds'.format(status,seconds))
return
elif seconds < 3600:
minutes = seconds // 60
seconds = seconds - 60*minutes
print('The script {} {} minutes & {} seconds'.format(status,minutes,seconds))
return
else:
hours = seconds // 3600
minutes = (seconds - 3600*hours) // 60
seconds = seconds - 3600*hours - 60*minutes
print('The script {} {} hours, {} minutes & {} seconds'.format(status,hours,minutes,seconds))
return
import time
startTime = time.time()
#The main body of script...
printRuntime(time.time() - startTime)
Since I'm self taught I have no real idea about best practice. I feel like there must be a more concise way of doing this, what mistakes am I making?
python python-3.x datetime formatting
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I'm self taught in python 3, and I frequently keep track of how long my scripts have been running using the time library.
import time
startTime = time.time()
#The main body of script...
print('The script finished in {0} seconds'.format(time.time() - startTime)
However this can be unhelpful if the script has been running for some time, an example output being:
'The script has been running for 4323.580279111862 seconds'
I wrote a function which takes a float variable and prints the time in a more readable way. I convert the seconds to integer to get rid of the 12 decimal places, and I can pass a boolean value which changes the wording of the print to indicate that the script is still running or has finished.
def printRuntime(seconds,finished=False):
seconds = int(seconds)
status = 'has been running for'
if finished == True:
status = 'finished in'
if seconds < 60:
print('The script {} {} seconds'.format(status,seconds))
return
elif seconds < 3600:
minutes = seconds // 60
seconds = seconds - 60*minutes
print('The script {} {} minutes & {} seconds'.format(status,minutes,seconds))
return
else:
hours = seconds // 3600
minutes = (seconds - 3600*hours) // 60
seconds = seconds - 3600*hours - 60*minutes
print('The script {} {} hours, {} minutes & {} seconds'.format(status,hours,minutes,seconds))
return
import time
startTime = time.time()
#The main body of script...
printRuntime(time.time() - startTime)
Since I'm self taught I have no real idea about best practice. I feel like there must be a more concise way of doing this, what mistakes am I making?
python python-3.x datetime formatting
I'm self taught in python 3, and I frequently keep track of how long my scripts have been running using the time library.
import time
startTime = time.time()
#The main body of script...
print('The script finished in {0} seconds'.format(time.time() - startTime)
However this can be unhelpful if the script has been running for some time, an example output being:
'The script has been running for 4323.580279111862 seconds'
I wrote a function which takes a float variable and prints the time in a more readable way. I convert the seconds to integer to get rid of the 12 decimal places, and I can pass a boolean value which changes the wording of the print to indicate that the script is still running or has finished.
def printRuntime(seconds,finished=False):
seconds = int(seconds)
status = 'has been running for'
if finished == True:
status = 'finished in'
if seconds < 60:
print('The script {} {} seconds'.format(status,seconds))
return
elif seconds < 3600:
minutes = seconds // 60
seconds = seconds - 60*minutes
print('The script {} {} minutes & {} seconds'.format(status,minutes,seconds))
return
else:
hours = seconds // 3600
minutes = (seconds - 3600*hours) // 60
seconds = seconds - 3600*hours - 60*minutes
print('The script {} {} hours, {} minutes & {} seconds'.format(status,hours,minutes,seconds))
return
import time
startTime = time.time()
#The main body of script...
printRuntime(time.time() - startTime)
Since I'm self taught I have no real idea about best practice. I feel like there must be a more concise way of doing this, what mistakes am I making?
python python-3.x datetime formatting
python python-3.x datetime formatting
edited Sep 4 '17 at 15:52
200_success
127k15148410
127k15148410
asked Sep 4 '17 at 15:45
Joshua Kidd
296311
296311
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
There is a built-in timeit
module which is quite convenient for measuring execution times, but, if we were to improve this particular solution, I would switch to using divmod
as suggested by Brandon here, utilize short if/else version for the status
definition and join the time value parts with a comma filtering non-zero values only.
Something along these lines:
seconds = int(seconds)
status = 'has been running for' if not finished else 'finished in'
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
periods = [('hours', hours), ('minutes', minutes), ('seconds', seconds)]
time_string = ', '.join('{} {}'.format(value, name)
for name, value in periods
if value)
print('The script {} {}'.format(status, time_string))
add a comment |
up vote
-1
down vote
I am also self-taught and do it much the same as you.
For a different reason (wanted to periodically run functions) I was looking for "time until execute" logging, anyway, I codged this together after reading several examples (including this page).
def secs_to_dhms(seconds):
from datetime import datetime, timedelta
d = datetime(1,1,1) + timedelta(seconds=int(seconds))
output = "{:02}:{:02}:{:02}:{:02}" .format(d.day-1, d.hour, d.minute, d.second)
return output
Not exactly what you wanted, but maybe of use to you, anyway.
New contributor
2
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
– Toby Speight
Nov 13 at 16:38
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
There is a built-in timeit
module which is quite convenient for measuring execution times, but, if we were to improve this particular solution, I would switch to using divmod
as suggested by Brandon here, utilize short if/else version for the status
definition and join the time value parts with a comma filtering non-zero values only.
Something along these lines:
seconds = int(seconds)
status = 'has been running for' if not finished else 'finished in'
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
periods = [('hours', hours), ('minutes', minutes), ('seconds', seconds)]
time_string = ', '.join('{} {}'.format(value, name)
for name, value in periods
if value)
print('The script {} {}'.format(status, time_string))
add a comment |
up vote
5
down vote
accepted
There is a built-in timeit
module which is quite convenient for measuring execution times, but, if we were to improve this particular solution, I would switch to using divmod
as suggested by Brandon here, utilize short if/else version for the status
definition and join the time value parts with a comma filtering non-zero values only.
Something along these lines:
seconds = int(seconds)
status = 'has been running for' if not finished else 'finished in'
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
periods = [('hours', hours), ('minutes', minutes), ('seconds', seconds)]
time_string = ', '.join('{} {}'.format(value, name)
for name, value in periods
if value)
print('The script {} {}'.format(status, time_string))
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
There is a built-in timeit
module which is quite convenient for measuring execution times, but, if we were to improve this particular solution, I would switch to using divmod
as suggested by Brandon here, utilize short if/else version for the status
definition and join the time value parts with a comma filtering non-zero values only.
Something along these lines:
seconds = int(seconds)
status = 'has been running for' if not finished else 'finished in'
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
periods = [('hours', hours), ('minutes', minutes), ('seconds', seconds)]
time_string = ', '.join('{} {}'.format(value, name)
for name, value in periods
if value)
print('The script {} {}'.format(status, time_string))
There is a built-in timeit
module which is quite convenient for measuring execution times, but, if we were to improve this particular solution, I would switch to using divmod
as suggested by Brandon here, utilize short if/else version for the status
definition and join the time value parts with a comma filtering non-zero values only.
Something along these lines:
seconds = int(seconds)
status = 'has been running for' if not finished else 'finished in'
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
periods = [('hours', hours), ('minutes', minutes), ('seconds', seconds)]
time_string = ', '.join('{} {}'.format(value, name)
for name, value in periods
if value)
print('The script {} {}'.format(status, time_string))
answered Sep 4 '17 at 16:09
alecxe
14.4k53176
14.4k53176
add a comment |
add a comment |
up vote
-1
down vote
I am also self-taught and do it much the same as you.
For a different reason (wanted to periodically run functions) I was looking for "time until execute" logging, anyway, I codged this together after reading several examples (including this page).
def secs_to_dhms(seconds):
from datetime import datetime, timedelta
d = datetime(1,1,1) + timedelta(seconds=int(seconds))
output = "{:02}:{:02}:{:02}:{:02}" .format(d.day-1, d.hour, d.minute, d.second)
return output
Not exactly what you wanted, but maybe of use to you, anyway.
New contributor
2
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
– Toby Speight
Nov 13 at 16:38
add a comment |
up vote
-1
down vote
I am also self-taught and do it much the same as you.
For a different reason (wanted to periodically run functions) I was looking for "time until execute" logging, anyway, I codged this together after reading several examples (including this page).
def secs_to_dhms(seconds):
from datetime import datetime, timedelta
d = datetime(1,1,1) + timedelta(seconds=int(seconds))
output = "{:02}:{:02}:{:02}:{:02}" .format(d.day-1, d.hour, d.minute, d.second)
return output
Not exactly what you wanted, but maybe of use to you, anyway.
New contributor
2
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
– Toby Speight
Nov 13 at 16:38
add a comment |
up vote
-1
down vote
up vote
-1
down vote
I am also self-taught and do it much the same as you.
For a different reason (wanted to periodically run functions) I was looking for "time until execute" logging, anyway, I codged this together after reading several examples (including this page).
def secs_to_dhms(seconds):
from datetime import datetime, timedelta
d = datetime(1,1,1) + timedelta(seconds=int(seconds))
output = "{:02}:{:02}:{:02}:{:02}" .format(d.day-1, d.hour, d.minute, d.second)
return output
Not exactly what you wanted, but maybe of use to you, anyway.
New contributor
I am also self-taught and do it much the same as you.
For a different reason (wanted to periodically run functions) I was looking for "time until execute" logging, anyway, I codged this together after reading several examples (including this page).
def secs_to_dhms(seconds):
from datetime import datetime, timedelta
d = datetime(1,1,1) + timedelta(seconds=int(seconds))
output = "{:02}:{:02}:{:02}:{:02}" .format(d.day-1, d.hour, d.minute, d.second)
return output
Not exactly what you wanted, but maybe of use to you, anyway.
New contributor
New contributor
answered Nov 13 at 14:10
Johan
1
1
New contributor
New contributor
2
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
– Toby Speight
Nov 13 at 16:38
add a comment |
2
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
– Toby Speight
Nov 13 at 16:38
2
2
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
– Toby Speight
Nov 13 at 16:38
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
– Toby Speight
Nov 13 at 16:38
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f174796%2fconvert-seconds-to-hours-minutes-seconds-and-pretty-print%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown